欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于mybatis-plus QueryWrapper 排序的坑

程序员文章站 2022-03-06 08:08:20
目录querywrapper排序的坑条件构造器querywrapper案例一案例二案例三案例四querywrapper排序的坑大致可以总结成上面的两句话。条件构造器querywrapper我们再搞几个...

querywrapper排序的坑

基于mybatis-plus 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);
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。