import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map; import javax.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import cn.hutool.core.date.DateUtil; import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ebc.numen.Application;
import com.ebc.numen.entity.SysUser;
import com.ebc.numen.service.SysUserService;
/**
* 单表操作例子
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class TestSingle {
@Resource
private SysUserService sysUserService;
/**
* 添加
* <pre>
* 1.不需要手工设置id
* 2.不需要手工设置version、create_datetime、update_datetime(因为已自动填充)
* 3.delete不填,数据库允许空
* </pre>
*/
@Test
public void insert() {
for (int i = 0; i < 30; i++) {
SysUser user = new SysUser();
user.setOrgId(1L);
user.setAge(10);
user.setName("遥远"+i);
user.setScope(BigDecimal.valueOf(1.23));
boolean isSucc = sysUserService.insert(user);
log.info("{},主键id:{}",isSucc,user.getId());
}
}
/**
* 修改~不获取对象
* <pre>
* 1.没设置version,导致数据库version值不变,乐观锁没生效。
* 如果知道当前version值,也可以手工设置.
* 2.update_datetime自动填充
* </pre>
*/
@Test
public void update1() {
SysUser user = new SysUser();
user.setId(1024613506376708097L);
user.setName("www");
//user.setVersion(7);
boolean isSucc = sysUserService.updateById(user);
log.info("{}",isSucc);
}
/**
* 修改~获取对象
* <pre>
* 乐观锁生效。而且不需要自行+1,数据库当前值即可。
* </pre>
*/
@Test
public void update2() {
SysUser user = sysUserService.selectById(1024613506376708097L);
user.setName("eee"); boolean isSucc = sysUserService.updateById(user);
if (isSucc) {
log.info("修改成功");
} else {
log.info("修改失败,由于乐观锁,可能已被别人修改过了。如果想在修改,请重新获取该条记录后修改。");
}
}
/**
* <pre>
* 如果将某个字段置空,用updateAllColumnById方法
* 如果部分字段修改,用updateById方法
* </pre>
*/
@Test
public void update3() {
SysUser user = sysUserService.selectById(1024613506376708097L);
user.setName(null); boolean isSucc = sysUserService.updateAllColumnById(user);
if (isSucc) {
log.info("修改成功");
} else {
log.info("修改失败,由于乐观锁,可能已被别人修改过了。如果想在修改,请重新获取该条记录后修改。");
}
}
/**
* 物理删除
* <pre>
* DELETE FROM t_sys_user WHERE id=?
* </pre>
*/
@Test
public void delete() {
boolean isSucc = sysUserService.deleteById(1024613254420688898L);
log.info("{}",isSucc);
}
/**
* 逻辑删除
* <pre>
* 需在SysUser类中的deleted上边的注解放开
* 即:<code>@TableLogic</code>
*
* 执行后结果:
* UPDATE t_sys_user SET deleted=1 WHERE id=?
* </pre>
*/
@Test
public void deleteLogic() {
boolean isSucc = sysUserService.deleteById(1024839982808219649L);
log.info("{}",isSucc);
}
/**
* 指定条件删除
* <pre>
* 执行sql:
* DELETE FROM t_sys_user WHERE (age = ? AND create_datetime >= ? AND name LIKE ?)
* 10(Integer), 2018-08-02 09:37:06.0(Timestamp), 遥远%(String)
* </pre>
*/
@Test
public void deleteByWrapper() {
String dateStr = "2018-08-02 09:37:06";
Date date = DateUtil.parse(dateStr);
EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
ew.eq("age", 10);
ew.ge("create_datetime", date);//字符串日期也可。
ew.like("name", "遥远", SqlLike.RIGHT);
boolean isSucc = sysUserService.delete(ew);//ew为null,则全部删除
log.info("{}",isSucc);
}
/**
* 分页查询
* <pre>
* Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (age = ? AND create_datetime >= ? AND update_datetime NOT BETWEEN ? AND ? AND name LIKE ?) LIMIT 0,10
* Parameters: 10(Integer), 2018-08-02 09:37:06(String), 2018-08-01 19:11:10(String), 2018-08-02 09:37:06(String), 遥远%(String)
* </pre>
*/
@Test
public void selectPage() {
Page<SysUser> page = new Page<SysUser>(1,10);
EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
ew.eq("age", 10);
ew.ge("create_datetime", "2018-08-02 09:37:06");//Date类型 也可以。
ew.notBetween("update_datetime", "2018-08-01 19:11:10","2018-08-02 09:37:06");
ew.like("name", "遥远", SqlLike.RIGHT);
Page<SysUser> p = sysUserService.selectPage(page,ew);
log.info("{}",p);
log.info("{}",p.getRecords());
}
/**
* <pre>
* 查询多个id的对象集合
* 执行sql:
* Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE id IN ( ? , ? )
* Parameters: 1024839982728527874(Long), 1024831423877894145(Long)
* </pre>
*/
@Test
public void selectBatchIds() {
List<Long> ids = Arrays.asList(1024839982728527874L, 1024831423877894145L);
List<SysUser> list = sysUserService.selectBatchIds(ids);
list.stream().forEach(s -> System.out.println(s));
}
/**
* 获取符合条件的第1条
* <pre>
* Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (name LIKE ?)
* Parameters: 遥远%(String)
* </pre>
*/
@Test
public void selectOne() {
EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
ew.like("name", "遥远", SqlLike.RIGHT);
SysUser user = sysUserService.selectOne(ew);
log.info("{}",user);
}
/**
* 获取符合条件的所有记录
* <pre>
* Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (name LIKE ?)
* Parameters: 遥远%(String)
* </pre>
*/
@Test
public void selectList() {
EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
ew.like("name", "遥远", SqlLike.RIGHT);
List<SysUser> list = sysUserService.selectList(ew);
list.stream().forEach(s -> System.out.println(s));
}
/**
* 操作符:in
* <pre>
* Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (name IN (?,?,?) AND org_id NOT IN (?,?))
* Parameters: 遥远0(String), 遥远2(String), 遥远3(String), 2(String), 3(String)
* </pre>
*/
@Test
public void operate_in() {
EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
ew.in("name", "遥远0,遥远2,遥远3");//字符串
ew.notIn("org_id", "2,3");//整数
List<SysUser> list = sysUserService.selectList(ew);
list.stream().forEach(s -> System.out.println(s));
}
/**
* 操作符:between
* <pre>
* Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (create_datetime BETWEEN ? AND ?)
* Parameters: 2018-08-01 19:11:10(String), 2018-08-02 09:37:06(String)
* </pre>
*/
@Test
public void operate_between() {
EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
ew.between("create_datetime", "2018-08-01 19:11:10","2018-08-02 09:37:06");//字符串或者Date均可
List<SysUser> list = sysUserService.selectList(ew);
list.stream().forEach(s -> System.out.println(s));
}
/**
* 将查询结果封装到Map<String, Object>
* <pre>
* Preparing: SELECT id AS id,org_id AS orgId,`name`,age,`scope`,create_datetime AS createDatetime,update_datetime AS updateDatetime,deleted,version FROM t_sys_user WHERE (create_datetime BETWEEN ? AND ?)
* Parameters: 2018-08-01 19:11:10(String), 2018-08-02 09:37:06(String)
*
* </pre>
*/
@Test
public void selectMaps() {
EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
ew.between("create_datetime", "2018-08-01 19:11:10","2018-08-02 09:37:06");//字符串或者Date均可
List<Map<String, Object>> list = sysUserService.selectMaps(ew);
//打印如下:
//{createDatetime=2018-08-01 19:11:10.0, updateDatetime=2018-08-01 19:11:10.0, scope=1.23, name=22, id=1024613506376708097, version=0, orgId=1, age=10}
//{createDatetime=2018-08-02 09:37:06.0, updateDatetime=2018-08-02 09:37:06.0, scope=1.23, name=11, id=1024831423877894145, version=0, orgId=1, age=10}
list.stream().forEach(s -> System.out.println(s));
}
/**
* 只返回第一个字段值
* <pre>
* Preparing: SELECT id AS id FROM t_sys_user WHERE (create_datetime BETWEEN ? AND ?)
* Parameters: 2018-08-01 19:11:10(String), 2018-08-02 09:37:06(String)
* </pre>
*/
@Test
public void selectObjs() {
EntityWrapper<SysUser> ew = new EntityWrapper<SysUser>();
ew.between("create_datetime", "2018-08-01 19:11:10","2018-08-02 09:37:06");//字符串或者Date均可
List<Object> list = sysUserService.selectObjs(ew);
//打印如下:
//1024613506376708097
//
list.stream().forEach(s -> System.out.println(s));
}
}

mybaits-plus总结的更多相关文章

  1. MyBaits使用小结

      Mybatis用like进行模糊查询的时候,配置与所用的数据库有关系,总结了下,具体如下: 1.MySQL :LIKE CONCAT('%',#{empname},'%' ) 或者 LIKE CO ...

  2. MyBaits学习

    一:配置MyBaits的开发环境 1.1.核心配置文件 正如hibernate一样,MyBaits也有一个核心的配置文件,它包含着数据源地址,用户名,密码等,还有着各个实体类的配置文件,配置文件是xm ...

  3. MyBaits一对一的查询方法

    MyBaits一对一的查询方法 一:表数据与表结构 CREATE TABLE teacher( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name ) ); CRE ...

  4. (原创)mybaits学习三,springMVC和mybatis融合

    上一节,总计了spring和mybaits的融合,这一节,我们来学习springmvc和mybatis融合 最近在弄一个SSM的项目,然后在网上找资料,将资料总结如下 一,开发环境的配置 MyEcli ...

  5. mybaits入门

    1.回顾jdbc开发 orm概述 orm是一种解决持久层对象关系映射的规则,而不是一种具体技术.jdbc/dbutils/springdao,hibernate/springorm,mybaits同属 ...

  6. Mybaits学习总结2

    http://www.cnblogs.com/xdp-gacl/p/4262895.html 继续参考这篇文章写Mybaits学习总结 上一章,我修改了编码,统一为UTF8之后,便没有编码错误 < ...

  7. Mybaits学习总结1

    http://www.cnblogs.com/xdp-gacl/p/4261895.html 参考了这篇文章搭建了Mybaits环境,原作者有些地方没有标注使用某种编码,我是自学SQL的,所以深知编码 ...

  8. mybaits 学习

    mybaits  学习(一) 注意:如果建的是web项目,run as java application 参考http://www.cnblogs.com/xdp-gacl/p/4261895.htm ...

  9. Mybaits入门之起航

    前言 Mybaits技术现在很多公司都在使用,它提供了简单的API可以快速进行数据库操作,所以不管是自己做系统还是找工作都有必要了解一下. 学习一门技术如果是入门的话要么买书要么就是阅读官方的文档,而 ...

  10. mybaits使用存储过程

    如何使用Mybaits调用数据库存储过程,按以下顺序Oracle案例库: 1.在数据库中创建以下存储过程: create or replace procedure pro_hello(p_result ...

随机推荐

  1. rails work

    4.2 Say "Hello", Rails problem weppack not install solve run the command rails webpacker:i ...

  2. 学习笔记(16)- InsuranceQA_zh

    仓库地址:https://github.com/l11x0m7/InsuranceQA_zh Convolutional Neural Network for Chinese InsuranceQA ...

  3. checkbox全选/取消全选

    //checkbox全选/取消全选 $(function() { $("#checkAll").click(function() { if(this.checked){ $(&qu ...

  4. A easy and simple way to establish Oracle ADG

    Yes, thanks to Then, I can give simple and reasy way to make it. Suppose hosts and IPs like that: 15 ...

  5. Codeforces1307C. Cow and Message

    读题,我们知道选1个.选2个的时候是没有冲突的,大于2个的时候就有限制,那么我们观察是否可以把大于2个的情况都转换一下,可以发现,如果有一个串的长度大于2,且出现的次数大于2,那么,组成这个串里必定有 ...

  6. IE 样式属性前后缀兼容写法略统计

    总结 IE 兼容写法: \9: IE6 IE7 IE8*: IE6 IE7_: IE6*+: IE7 ---------------------------------- 书写位置: backgrou ...

  7. Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全

    通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决 ...

  8. mabatisplus-update

    /** * <p> * 根据 whereEntity 条件,更新记录 * </p> * * @param entity 实体对象 (set 条件值,不能为 null) * @p ...

  9. js 字符串 常用处理方式(检索、截取、拼接、批量替换)

    // 检索(字符串中判断是否包含某个字符) 字符串.search('检索的内容');// 返回-1,不包含: 返回非-1,包含 字符串.indexOf("待判断的内容"); // ...

  10. css 瀑布流

    瀑布流 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...