欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

基于mybatis-plus QueryWrapper 排序的坑

时间:2022-12-22 09:54:46|栏目:JAVA代码|点击:

QueryWrapper排序的坑

大致可以总结成上面的两句话。

条件构造器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 '李%'
 */
@Test
public void selectByQueryWrapper3(){
  QueryWrapper<Employee> queryWrapper=new QueryWrapper();
  // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>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<Employee> 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 ='女')
 */
@Test
public void selectByQueryWrapper4(){
  QueryWrapper<Employee> queryWrapper=new QueryWrapper();
  // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
  queryWrapper.likeRight("name","李").or(wq->wq.isNotNull("email").eq("gender","女"));
  List<Employee> 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 ='女')
 */
@Test
public void selectByQueryWrapper5(){
  QueryWrapper<Employee> queryWrapper=new QueryWrapper();
  // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
  queryWrapper.likeRight("name","李").and(wq->wq.isNotNull("email").or().eq("gender","女"));
  List<Employee> 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
 */
@Test
public void selectByQueryWrapper6(){
  QueryWrapper<Employee> queryWrapper=new QueryWrapper();
  // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
  queryWrapper.likeRight("name","李").and(wq->wq.isNotNull("email").or().eq("gender","女"));
  List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
  System.out.println(employeeList);
}

上一篇:java 二分法详解几种实现方法

栏    目:JAVA代码

下一篇:MyBatis-Plus实现字段自动填充功能的示例

本文标题:基于mybatis-plus QueryWrapper 排序的坑

本文地址:http://www.codeinn.net/misctech/221922.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有