【java】mybatis-plus lambda函数表达式作用说明

文章目录

  • 前言
  • 一、 lambda表达式函数汇总
  • 总结


前言

本文主要记录了mybatis-plus lambda表达式中主要函数的作用。


一、 lambda表达式函数汇总

//查询学生
QueryWrapper<Student> queryWrapper = new QueryWrapper();
函数 说明 举例
eq equals等于(=) queryWrapper.lambda().eq(Student::getName,“老王”);
ne not equals不等于(<>) queryWrapper.lambda().ne(Student::getName,“老王”);
gt greater than大于(>) queryWrapper.lambda().gt(Student::getAge,26);
ge greater or equals大于等于(>=) queryWrapper.lambda().ge(Student::getAge,26);
lt less than小于(<) queryWrapper.lambda().lt(Student::getAge,26);
le less or equals小于等于(<=) queryWrapper.lambda().le(Student::getAge,26);
between between a and b queryWrapper.lambda().between(Student::getAge,26,30);
notBetween not between a and b queryWrapper.lambda().notBetween(Student::getAge,26,30);
like like ‘%值%’ queryWrapper.lambda().like(Student::getName,“老王”);
notLike not like ‘%值%’ queryWrapper.lambda().notLike(Student::getName,“老王”);
likeLeft like ‘%值’ queryWrapper.lambda().likeLeft(Student::getName,“老王”);
likeRight like ‘值%’ queryWrapper.lambda().likeRight(Student::getName,“老王”);
isNull 字段 is null queryWrapper.lambda().isNull(Student::getName);
isNotNull 字段 is not null queryWrapper.lambda().isNotNull(Student::getName);
in 字段 in (a,b,c) queryWrapper.lambda().in(Student::getId, idList);//注意判断idList不为空
notIn 字段 not in (a,b,c) queryWrapper.lambda().notIn(Student::getId, idList);//注意判断idList不为空
inSql 字段 in (sql语句) queryWrapper.lambda().inSql(Student::getId,“select id from student where is_deleted = 0”);//id in (select id from student where is_deleted = 0);
notInSql 字段 not in (sql语句) queryWrapper.lambda().notInSql(Student::getId,“select id from student where is_deleted = 0”);//id not in (select id from student where is_deleted = 0);
groupBy group by 字段a,字段b queryWrapper.lambda().groupBy(Student::getId,Student::getName);
orderBy order by 字段a,字段b queryWrapper.lambda().orderBy(Student::getId,Student::getName);
orderByAsc order by 字段a,字段b asc queryWrapper.lambda().orderByAsc(Student::getId,Student::getName);
orderByDesc order by 字段a,字段b desc queryWrapper.lambda().orderByDesc(Student::getId,Student::getName);
having having (sql语句) queryWrapper.lambda().having(“sum(age) > {0}”, 122);//having sum(age) > 22;
or or拼接 queryWrapper.lambda().eq(Student::getName,“老王”).or().eq(Student::getName,“老李”);// and name ='老王’ or name = ‘老李’;
and and嵌套 queryWrapper.lambda().and(obj ->obj.eq(TemplateEntity::getName, “老王”).or().like(TemplateEntity::getName, ‘王’));//and (name = ‘老王’ or name like ‘%王%’);
apply 拼接sql queryWrapper.lambda().apply(" and date_format(update_time, ‘%Y-%m-%d’) ={0}",“2021-10-16”);//and date_format(update_time, ‘%Y-%m-%d’) = ‘2021-10-16’; 直接传参会有sql注入风险
last 无视优化规则直接拼接到sql最后 queryWrapper.lambda().last(" limit 1,10");// limit 1,10; 只能调用一次,多次调用以最后一次为准,有sql注入的风险,请谨慎使用
exists 拼接exists语句 queryWrapper.lambda().exists(" select id from student where name = '老王‘’");// exists (select id from student where name = ‘老王’);
notExists 拼接not exists语句 queryWrapper.lambda().notExists(" select id from student where name = '老王‘’");// not exists (select id from student where name = ‘老王’);
nested 正常嵌套不带AND或者OR queryWrapper.lambda().nested(obj -> obj.eq(TemplateEntity::getName, “老王”).ne(TemplateEntity::getAge, 26));//and name = ‘老王’ and age != 26;

总结

本文主要是帮助自己记录常见的函数用法,如想看详细的文档说明,推荐阅读这篇博客。

你可能感兴趣的:(mybatis,java,数据库)