lombok
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@Test
void selectById(){
IPage page = new Page(2,2);
bookDao.selectPage(page,null);
System.out.println("当前页码值:"+page.getCurrent());
System.out.println("每页显示数:"+page.getSize());
System.out.println("一共多少页:"+page.getPages());
System.out.println("一共多少数据:"+page.getTotal());
System.out.println("数据:"+page.getRecords());
}
条件查询
方法一:
@Test
void selectById(){
QueryWrapper qw = new QueryWrapper();
qw.lt("id",3);
List<Book> list = bookDao.selectList(qw);
System.out.println(list);
}
查询id小于3的数据
方法二:lam表达式
@Test
void selectById(){
QueryWrapper<Book> qw = new QueryWrapper();
qw.lambda().lt(Book::getId,3);
List<Book> list = bookDao.selectList(qw);
System.out.println(list);
}
方法三:
@Test
void selectById(){
LambdaQueryWrapper<Book> lwq = new LambdaQueryWrapper<Book>();
lwq.lt(Book::getId,3);
List<Book> list = bookDao.selectList(lwq);
System.out.println(list);
}
或
@Test
void selectById(){
LambdaQueryWrapper<Book> lwq = new LambdaQueryWrapper<Book>();
lwq.lt(Book::getId,3).or().gt(Book::getId,1);
List<Book> list = bookDao.selectList(lwq);
System.out.println(list);
}
且
@Test
void selectById(){
LambdaQueryWrapper<Book> lwq = new LambdaQueryWrapper<Book>();
lwq.lt(Book::getId,3).gt(Book::getId,1);
List<Book> list = bookDao.selectList(lwq);
System.out.println(list);
}
查询投影
@Test
void selectById(){
BookQuery bookQuery = new BookQuery();
//bookQuery.setId(1L);
bookQuery.setId2(3L);
LambdaQueryWrapper<Book> lwq = new LambdaQueryWrapper<Book>();
lwq.lt(null!=bookQuery.getId2(),Book::getId,bookQuery.getId2())
.gt(null!=bookQuery.getId(),Book::getId,bookQuery.getId());
List<Book> list = bookDao.selectList(lwq);
System.out.println(list);
}
相当于将查询参数提出到一个类里,并通过condition参数判断是否满足条件
@Test
void selectById(){
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
lqw.select(Book::getId,Book::getName,Book::getDescription);
List<Book> list = bookDao.selectList(lqw);
System.out.println(list);
}
@Test
void selectById(){
QueryWrapper<Book> lqw = new QueryWrapper<>();
lqw.select("count(*) as count");
List<Map<String, Object>> list = bookDao.selectMaps(lqw);
System.out.println(list);
}
使用map集合来存储非lambda表达式查询结果
@Test
void selectById(){
QueryWrapper<Book> lqw = new QueryWrapper<>();
lqw.select("count(*) as count,name");
lqw.groupBy("name");
List<Map<String, Object>> list = bookDao.selectMaps(lqw);
System.out.println(list);
}
对name进行分组查询
查询条件
@Test
void selectById(){
QueryWrapper<Book> lqw = new QueryWrapper<>();
lqw.between("id",1,2);
List<Book> list = bookDao.selectList(lqw);
System.out.println(list);
}
@Test
void selectById(){
LambdaQueryWrapper<Book> lwq = new LambdaQueryWrapper<>();
lwq.like(Book::getName,"t");
List<Book> list = bookDao.selectList(lwq);
System.out.println(list);
}
@TableName("tb_book")
@Data
public class Book {
private Long id;
private String name;
private String type;
@TableField("description")
private String description;
@TableField(exist = false)
private String online;
}
@TableName与数据库表名对应
@TableField可以与表数据项对应
id生成策略
因为就算数据库的id字段设置自增后,由于idea,必须在对应的实体类中的id字段设置@TableId(type = IdType.AUTO),才可以实现id自增,不然就算默认的长字符串
@TableName("tb_book")
@Data
public class Book {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private String type;
@TableField("description")
private String description;
@TableField(exist = false)
private String online;
}
用来对tablename进行全局配置
用
mybatis-plus:
global-config:
db-config:
id-type: assign_id
来对idtype(id生成策略进行全局配置)
逻辑删除
@TableLogic(value = "0",delval = "1")
private Integer deleted;
可以直接配置 @TableLogic注解来配置逻辑删除,value表示没有删除,delval表示删除
也可以通过
mybatis-plus:
global-config:
db-config:
id-type: assign_id
logic-delete-field: deleted
logic-delete-value: 1
logic-not-delete-value: 0
来配置全局逻辑删除
logic-delete-field:表示逻辑删除字段
logic-not-delete-value:表示未删除的值
logic-delete-value:表示已经删除的值
乐观锁
1.使用注解定义变量
@Version
private Integer version;
2.配置拦截器
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
3.直接把找到的pojo查询再修改
代码生成器