1、查看项目中是否导入mybatisPlus的jar包

2、servie 层和实现类要集成mybatisPlus

service 继承IService

实现类中要继承IService的实现类ServiceImpl

3、如果想要mapper中的一些方法,mapper 要继承BaseMapper

4、在实现类中实现方法,不需要在xml中写一个方法了,常用的方法

总结如下:

【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和IPage区别

使用方法:

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 {/** * 分页插件 */@Beanpublic PaginationInterceptor paginationInterceptor() {// 设置sql的limit为无限制,默认是500return new PaginationInterceptor().setLimit(-1);}}

(二)常用的lambda表达式的一些定义

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);}});

allEq

条件用 Map 进行封装

“name” -> “张三”“age” -> 20public 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

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

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

gt("age", 20) -> age > 20public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.gt(User::getAge, 20);return this.baseMapper.selectList(wrapper);}

ge

ge("age", 20) -> age >= 20public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.ge(User::getAge, 20);return this.baseMapper.selectList(wrapper);}

lt

​​lt("age", 20) -> age < 20public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.lt(User::getAge, 20);return this.baseMapper.selectList(wrapper);}

le

le("age", 21) -> age <= 21public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.le(User::getAge, 20);return this.baseMapper.selectList(wrapper);}

between,notBetween

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

like 匹配值 -> "%值%" 模糊查询notLike 模糊查询不匹配"%值%"public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.like(User::getName, "张"); return this.baseMapper.selectList(wrapper);}

likeLeft

likeLeft 匹配值 -> "%值"public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.likeLeft(User::getName, "张"); return this.baseMapper.selectList(wrapper);}

likeRight

likeRight 匹配值 -> "值%"public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.likeRight(User::getName, "张"); return this.baseMapper.selectList(wrapper);}

isNull 空值查询

isNotNull 非空值查询public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.isNull(User::getName);//wrapper.isNotNull(User::getName); return this.baseMapper.selectList(wrapper);}

in

in("name", "张三", "李四") -> name in ("张三", "李四") 姓名是张三或李四的用户notInnotIn("name", "张三", "李四") -> name not in ("张三", "李四") 姓名不是张三或李四的用户public List userList() {LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();wrapper.in(User::getName, "张三","李四");//wrapper.in(User::getName, "张三","李四"); return this.baseMapper.selectList(wrapper);}

inSql、notInSql

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);}

groupBy分组

public List userList() {LambdaQueryWrapper wrapper = new LambdaQueryWrapper();wrapper.groupBy(User::getName);return this.baseMapper.selectList(wrapper);}

orderBy、orderByAsc、orderByDesc

public List userList() {LambdaQueryWrapper wrapper = new LambdaQueryWrapper();// SELECT id,name,email,age FROM user ORDER BY name ASC,age DESCwrapper.orderBy(true, true, User::getName).orderBy(true, false, User::getAge); // SELECT id,name,email,age FROM user ORDER BY name ASC,age ASCwrapper.orderByAsc(User::getName, User::getAge);// SELECT id,name,email,age FROM user ORDER BY name DESC,age DESCwrapper.orderByDesc(User::getName, User::getAge);return this.baseMapper.selectList(wrapper);}

or、and

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%')

这样就解决了,这个问题在我的公司中新人(包括我在内)貌似都遇到这个问题,在此说明一下

last

在末尾拼接sql语句

注:last()有sql注入的风险,请谨慎使用!

public List userList() {LambdaQueryWrapper wrapper = new LambdaQueryWrapper();// SELECT id,name,email,age FROM user WHERE (name = ? OR age = ?) limit 1wrapper.eq(User::getName, "张三").or().eq(User::getAge, 20).last("limit 1");return this.baseMapper.selectList(wrapper);}

exists、notExists

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);}