基于mybatis-plus|基于mybatis-plus QueryWrapper 排序的坑
目录
- QueryWrapper排序的坑
- 条件构造器QueryWrapper
- 案例一
- 案例二
- 案例三
- 案例四
QueryWrapper排序的坑
文章图片
大致可以总结成上面的两句话。
条件构造器QueryWrapper 【基于mybatis-plus|基于mybatis-plus QueryWrapper 排序的坑】我们再搞几个实例来深入理解下条件构造器QueryWrapper的用法:
案例一
查询姓李的,并且出生日期范围是1993-02-09到1994-04-09的员工
sql实现:
SELECT * FROM t_employee WHERE DATE_FORMAT(birthday,'%Y-%m-%d')>='1993-02-09' AND DATE_FORMAT(birthday,'%Y-%m-%d')<='1994-04-09' AND NAME LIKE '李%'
mp实现:
/** * 查询姓李的,并且出生日期范围是1993-02-09到1994-04-09的员工 * sql:SELECT * FROM t_employee WHERE DATE_FORMAT(birthday,'%Y-%m-%d')>='1993-02-09' AND DATE_FORMAT(birthday,'%Y-%m-%d')<='1994-04-09' AND NAME LIKE '李%' */@Testpublic void selectByQueryWrapper3(){QueryWrapperqueryWrapper=new QueryWrapper(); // QueryWrapper queryWrapper2=Wrappers. query(); queryWrapper.apply("DATE_FORMAT(birthday,'%Y-%m-%d')>={0} and DATE_FORMAT(birthday,'%Y-%m-%d')<={1}","1993-02-09","1994-04-09").likeRight("name","李"); List employeeList = employeeMapper.selectList(queryWrapper); System.out.println(employeeList); }
案例二
查询姓李的或者邮箱不为空并且是女性的员工
sql实现:
SELECT * FROM t_employee WHERE NAME LIKE '李%' OR (email IS NOT NULL AND gender ='女')
mp实现:
/** * 查询姓李的或者邮箱不为空并且是女性的员工 * sql:SELECT * FROM t_employee WHERE NAME LIKE '李%' OR (email IS NOT NULL AND gender ='女') */@Testpublic void selectByQueryWrapper4(){QueryWrapperqueryWrapper=new QueryWrapper(); // QueryWrapper queryWrapper2=Wrappers. query(); queryWrapper.likeRight("name","李").or(wq->wq.isNotNull("email").eq("gender","女")); List employeeList = employeeMapper.selectList(queryWrapper); System.out.println(employeeList); }
案例三
查询姓李的并且邮箱不为空或者是女性的员工
sql实现:
SELECT * FROM t_employee WHERE NAME LIKE '李%' AND (email IS NOT NULL OR gender ='女')
mp实现:
/** * 查询姓李的并且邮箱不为空或者是女性的员工 * sql:SELECT * FROM t_employee WHERE NAME LIKE '李%' AND (email IS NOT NULL OR gender ='女') */@Testpublic void selectByQueryWrapper5(){QueryWrapperqueryWrapper=new QueryWrapper(); // QueryWrapper queryWrapper2=Wrappers. query(); queryWrapper.likeRight("name","李").and(wq->wq.isNotNull("email").or().eq("gender","女")); List employeeList = employeeMapper.selectList(queryWrapper); System.out.println(employeeList); }
案例四
查询属于编号1,2,3部门的并且薪水小于等于3500的员工 根据年龄从大到小排序显示
sql实现:
SELECT * FROM t_employee WHERE salary<=3500 AND departmentId IN (1,2,3) ORDER BY birthday ASC
mp实现:
/** * 查询属于编号1,2,3部门的并且薪水小于等于3500的员工 根据年龄从大到小排序显示 * sql:SELECT * FROM t_employee WHERE salary<=3500 AND departmentId IN (1,2,3) ORDER BY birthday ASC */@Testpublic void selectByQueryWrapper6(){QueryWrapperqueryWrapper=new QueryWrapper(); // QueryWrapper queryWrapper2=Wrappers. query(); queryWrapper.likeRight("name","李").and(wq->wq.isNotNull("email").or().eq("gender","女")); List employeeList = employeeMapper.selectList(queryWrapper); System.out.println(employeeList); }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- mybatis-plus|mybatis-plus QueryWrapper 添加limit方式
- MyBatis-Plus|MyBatis-Plus 之selectMaps、selectObjs、selectCount、selectOne的使用
- 卷积|一文入门基于三维数据的深度学习
- 电赛|作品 | 基于STM32的智能天气预报系统(源码开源)
- diy|diy 单片机 自动浇花_基于单片机的自动浇花器的制作方法
- python聊天室详细教程_基于python实现聊天室程序
- 自动灌溉c语言,基于51单片机设计实现的自动浇花系统的电路方案设计(原理图+源代码)...
- c语言51单片机湿度浇花器,基于51单片机的室内自动浇花系统的制作方法
- SpringBoot基于redis自定义注解实现后端接口防重复提交校验
- Angular|Angular 基于自定义指令的内容投影 content projection 问题的单步调试