service 继承IService<>
实现类中要继承IService的实现类ServiceImpl
【1】分页
参数1是分页起始位置,一般是1,参数2:结束位置, 一般都是前端传过来的,查询的数量
Page page1 = new Page(参数1,参数2);
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
条件 eq 指的是相等
queryWrapper.eq(LandLossAllowance::getCountrySide,landLossAllowanceOption.getCountrySide());
执行page方法 page(参数1,参数2)
参数1:上面的new的分页对象,参数2是lambda表达式查询条件
IPage iPage = page(page1, queryWrapper);
使用方法:
PageHelper.startPage()然后后边写sql就可以。 紧接着的一个sql起作用。
IPage则需要在dao层传入IPage的实现类Page对象,该对象实现了IPage。
区别:
PageHelper内部原理是将传入的页码和条数赋值给了Page对象,保存到了一个本地线程ThreadLoacl中,然后会进入Mybatis的拦截器中。
然后再拦截器中获取本地线程中保存的分页的参数。最后再将这写分页参数和原本的sql以及内部定义好的sql进行拼接完成sql的分页处理。
中间会进行判断该sql 的类型是查询还是修改操作。如果是查询才会进入分页的逻辑并判断封装好的Page对象是否是null,null则不分页,否则分页。IPage内部原理也是基于拦截器,但是这个拦截的是方法以及方法中的参数,这个也会判断是否是查询操作。如果是查询操作,才会进入分页的处理逻辑。
进入分页逻辑处理后,拦截器会通过反射获取该方法的参数进行判断是否存在IPage对象的实现类。如果不存在则不进行分页,存在则将该参数赋值给IPage对象。
然后进行拼接sql的处理完成分页操作。
但是使用IPage需要注入一个bean拦截器交给spring进行管理。如下。否则不会进行拦截。
使用Ipage之后,需要注入一些配置:
@Configuration
@MapperScan(value={"com.XX.**.mapper*"})
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
// 设置sql的limit为无限制,默认是500
return new PaginationInterceptor().setLimit(-1);
}
}
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper是MybatisPlus框架构造查询条件常用的对象
使用lambda表达式写法更为清晰,简洁:
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(User::getName,"aa");
queryWrapper.lambda().ge(User::getAge,20);
默认情况下多个条件是and连接:上述写法对应的SQL为:where name like '%aa%' and age >= 20
如果要使用or连接的话,方法如下:
queryWrapper.lambda().and(wq->{
wq.like(User::getName,"aa");
wq.or().like(User::getName,"bb);
});
这里引申下我遇到的一个场景:需要or连接的是一个集合内的所有元素:
List nameList = Lists.newArrayList("aa","bb","cc");
String firstName = nameList.get(0);
nameList.remove(0);
queryWrapper.lambda().and(wq->{
wq.like(User::getName,firstName);
for(String name : nameList){
wq.or().like(User::getName,name);
}
});
条件用 Map 进行封装
“name” -> “张三”
“age” -> 20
public void testAllEq() {
QueryWrapper queryWrapper = new QueryWrapper<>();
// 封装条件
Map hashMap = new HashMap<>();
hashMap.put("name", "张三");
hashMap.put("age", 20);
queryWrapper.allEq(hashMap);
List userList = userMapper.selectList(queryWrapper);
userList.forEach(user -> {
System.out.println(user);
});
}
eq("列名", 值) -> 列名 = 值
public List listByDictCode(DictCode dictCode) {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.eq(Dict::getDictCode, dictCode.getCode())
.eq(Dict::getEnabled, DictEnableEnum.VALID.getType());
return this.baseMapper.selectList(wrapper);
}
ne("列名", 值) -> 列名 != 值
public List listByDictCode(DictCode dictCode) {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.ne(Dict::getDictCode, dictCode.getCode())
.ne(Dict::getEnabled, DictEnableEnum.VALID.getType());
return this.baseMapper.selectList(wrapper);
}
gt("age", 20) -> age > 20
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.gt(User::getAge, 20);
return this.baseMapper.selectList(wrapper);
}
ge("age", 20) -> age >= 20
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.ge(User::getAge, 20);
return this.baseMapper.selectList(wrapper);
}
lt("age", 20) -> age < 20
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.lt(User::getAge, 20);
return this.baseMapper.selectList(wrapper);
}
le("age", 21) -> age <= 21
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.le(User::getAge, 20);
return this.baseMapper.selectList(wrapper);
}
between("age", 18, 25) -> age BETWEEN 18 AND 25 ,年龄在18到25之间
notBetween就是不在18到25之间
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.between(User::getAge, 18,25);
return this.baseMapper.selectList(wrapper);
}
like 匹配值 -> "%值%" 模糊查询
notLike 模糊查询不匹配"%值%"
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.like(User::getName, "张");
return this.baseMapper.selectList(wrapper);
}
likeLeft 匹配值 -> "%值"
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.likeLeft(User::getName, "张");
return this.baseMapper.selectList(wrapper);
}
likeRight 匹配值 -> "值%"
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.likeRight(User::getName, "张");
return this.baseMapper.selectList(wrapper);
}
isNotNull 非空值查询
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.isNull(User::getName);
//wrapper.isNotNull(User::getName);
return this.baseMapper.selectList(wrapper);
}
in("name", "张三", "李四") -> name in ("张三", "李四") 姓名是张三或李四的用户
notIn
notIn("name", "张三", "李四") -> name not in ("张三", "李四") 姓名不是张三或李四的用户
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.in(User::getName, "张三","李四");
//wrapper.in(User::getName, "张三","李四");
return this.baseMapper.selectList(wrapper);
}
public List userList() {
LambdaQueryWrapper wrapper= new LambdaQueryWrapper<>();
// SELECT id,name,email,age FROM user WHERE (age IN (select age from user where id = 1))
wrapper.inSql(User::getAge, "select age from user where id = 1");
return this.baseMapper.selectList(wrapper);
}
public List userList() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.groupBy(User::getName);
return this.baseMapper.selectList(wrapper);
}
public List userList() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
// SELECT id,name,email,age FROM user ORDER BY name ASC,age DESC
wrapper.orderBy(true, true, User::getName).orderBy(true, false, User::getAge);
// SELECT id,name,email,age FROM user ORDER BY name ASC,age ASC
wrapper.orderByAsc(User::getName, User::getAge);
// SELECT id,name,email,age FROM user ORDER BY name DESC,age DESC
wrapper.orderByDesc(User::getName, User::getAge);
return this.baseMapper.selectList(wrapper);
}
public List userList() {
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
// SELECT id,name,email,age FROM user WHERE (name = ? AND id = ?)
wrapper.eq(User::getName, "张三").and().eq(User::getId,1);
// SELECT id,name,email,age FROM user WHERE (name = ? OR id = ?)
wrapper.eq(User::getName, "张三").or().eq(User::getId,1);
return this.baseMapper.selectList(wrapper);
}
这里说明一下or和and的问题
错误代码
public List userList() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getId,1);
wrapper.like(User::getName,"张")
.or()
.like(User::getEmail,"163")
.or()
.like(User::getAge,1);
}
根据上面的写法写出的sql语句如下:
WHERE id = '1'
AND name LIKE '%张%'
OR email LIKE '%163%'
OR age LIKE '%1%'
这样明显是不对的,根据mysql语句执行顺序or最后执行 ,这会导致一旦[name like '%张%']条件成立后面的or条件就会失效,所以第一个条件 并没有起到and的作用。
解决方法
public List userList() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getId,1);
wrapper.and(wrapper->wrapper.like(User::getName,"张")
.or()
.like(User::getEmail,"163")
.or()
.like(User::getAge,1)
);
}
这样得到的sql语句如下
WHERE id = '1'
AND (name LIKE '%张%'
OR email LIKE '%163%'
OR age LIKE '%1%')
这样就解决了,这个问题在我的公司中新人(包括我在内)貌似都遇到这个问题,在此说明一下
在末尾拼接sql语句
注:last()有sql注入的风险,请谨慎使用!
public List userList() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
// SELECT id,name,email,age FROM user WHERE (name = ? OR age = ?) limit 1
wrapper.eq(User::getName, "张三").or().eq(User::getAge, 20).last("limit 1");
return this.baseMapper.selectList(wrapper);
}
public List userList() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
// SELECT id,name,email,age FROM user WHERE (EXISTS (select name from user where age > ?))
wrapper.exists("select name from user where age > 21");
// SELECT id,name,email,age FROM user WHERE (NOT EXISTS (select name from user where age > ?))
wrapper.notExists("select name from user where age > 21");
return this.baseMapper.selectList(wrapper);
}