当前位置 : 主页 > 网络编程 > 其它编程 >

MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置

来源:互联网 收集:自由互联 发布时间:2023-07-02
本篇内容主要讲解“MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强 本篇内容主要讲解“MyBatis-Plus中怎么使
本篇内容主要讲解“MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强

本篇内容主要讲解“MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让自由互联小编来带大家学习“MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置”吧!

使用EntityWrappe进行列表数据倒序设置

情景

项目中使用MyBatis-Plus的EntityWrapper,默认是升序,但实际项目中需要倒序显示,就查看了一下相关资料,在这里记录一下。

EntityWrapper是个很强大的玩意,支持多条件查询EntityWrapper()wrapper=newEntityWrapper();wrapper.between(column,val1,val2)wrapper.groupBy(columns)//对应sql中分组wrapper.eq(column,params)//相当于where条件wrapper.in(column,value)//sql中inwrapper.notIn(column,value)//sql中notinwrapper.orderBy(columns,isAsc)//排序wrapper.exists(value)//相对于sql中exists查询wrapper.notExists(value)//相当于sql中notexists查询wrapper.notBetween(column,val1,val2)//相当于sql中在某个范围内使用的betweenwrapper.ge(column,params)//大于等于wrapper.le(column,params)//小于等于wrapper.like(column,value)//模糊查询wrapper.having(sqlHaving,params)//条件过滤

如下进行倒序案例

使用.orderBy("create_time",false)就可以实现倒序排列查询

@OverridepublicPageUtilsqueryPage(Mapparams){//EntityWrapper可以封装bean参数Stringname=(String)params.get("name");Pagepage=this.selectPage(newQuery(params).getPage(),newEntityWrapper().like(StringUtils.isNotBlank(name),"name",name)//根据公司名进行查询.orderBy("create_time",false)//按创建时间进行倒序查询);returnnewPageUtils(page);}

根据上面的可以进行很多条件查询,有兴趣的可以自己试一试哦

条件构造器EntityWrapper用法

EntityWrapper 简介

1、 MybatisPlus 通过 EntityWrapper(简称 EW,MybatisPlus 封装的一个查询条件构造器)或者 Condition(与 EW 类似) 来让用户自由的构建查询条件,简单便捷,没有额外的负担, 能够有效提高开发效率。

2、实体包装器,主要用于处理 sql 拼接,排序,实体参数查询等。

3、注意: 使用的是数据库字段,不是 Java 属性。

4、条件参数说明

MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置

测试查询:

@TestpublicvoidtestEntityWrapperSelect(){//我们需要分页查询tbl_employee表中,年龄在18~50之间且性别为男且姓名为Tom的所有用户Listemps=employeeMapper.selectPage(newPage(1,2),newEntityWrapper().between("age",18,50).eq("gender",1).eq("last_name","Tom"));System.out.println(emps);//我们需要分页查询tbl_employee表中,年龄在18~50之间且性别为男且姓名为Tom的所有用户Listemps3=employeeMapper.selectPage(newPage(1,2),Condition.create().between("age",18,50).eq("gender","1").eq("last_name","Tom"));System.out.println(emps3);//查询tbl_employee表中,性别为女并且名字中带有"老师"或者邮箱中带有"a"Listemps1=employeeMapper.selectList(newEntityWrapper().eq("gender",0).like("last_name","老师")//.or()//SQL:(gender=?ANDlast_nameLIKE?ORemailLIKE?).orNew()//SQL:(gender=?ANDlast_nameLIKE?)OR(emailLIKE?).like("email","a"));System.out.println(emps1);//查询性别为女的,根据age进行排序(asc/desc),简单分页Listemps2=employeeMapper.selectList(newEntityWrapper().eq("gender",0).orderBy("age")//默认是升序//.orderDesc(Arrays.asList(newString[]{"age"})).last("desclimit1,3")//在sql最后拼接);System.out.println(emps2);}

控制台输出:

selectPage方法:Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (age BETWEEN ? AND ? AND gender = ? AND last_name = ?)Parameters: 18(Integer), 50(Integer), 1(Integer), Tom(String)Total: 1[Employee [id=1, lastName=Tom, email=tom@atguigu.com, gender=1, age=22]]selectPage使用Condition:Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (age BETWEEN ? AND ? AND gender = ? AND last_name = ?)Parameters: 18(Integer), 50(Integer), 1(String), Tom(String)Total: 1[Employee [id=1, lastName=Tom, email=tom@atguigu.com, gender=1, age=22]]selectList使用or:Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ? AND last_name LIKE ? OR email LIKE ?)Parameters: 0(Integer), %老师%(String), %a%(String)Total: 3[Employee [id=1, lastName=Tom, email=tom@atguigu.com, gender=1, age=22], Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25], Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]selectList使用orNew:Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ? AND last_name LIKE ?) OR (email LIKE ?)Parameters: 0(Integer), %老师%(String), %a%(String)Total: 3[Employee [id=1, lastName=Tom, email=tom@atguigu.com, gender=1, age=22], Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25], Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]selectList使用orderBy:Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ?) ORDER BY ageParameters: 0(Integer)Total: 2[Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null], Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25]]selectList使用orderDesc:Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ?) ORDER BY age DESCParameters: 0(Integer)Total: 2[Employee [id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25], Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]selectList使用last:Preparing: SELECT id AS id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (gender = ?) ORDER BY age desc limit 1,3Parameters: 0(Integer)Total: 1[Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]

测试修改:

@TestpublicvoidtestEntityWrapperUpdate(){Employeeemployee=newEmployee();employee.setLastName("王五");employee.setEmail("cls@sina.com");employee.setGender(0);employeeMapper.update(employee,newEntityWrapper().eq("last_name","Tom").eq("age",44));}

控制台输出:

Preparing: UPDATE tbl_employee SET last_name=?, email=?, gender=? WHERE (last_name = ? AND age = ?)Parameters: 王五(String), cls@sina.com(String), 0(Integer), Tom(String), 44(Integer) Updates: 0

测试删除:

@TestpublicvoidtestEntityWrapperDelete(){employeeMapper.delete(newEntityWrapper().eq("last_name","Tom").eq("age",22));}

控制台输出:

Preparing: DELETE FROM tbl_employee WHERE (last_name = ? AND age = ?) Parameters: Tom(String), 22(Integer)Updates: 1

到此,相信大家对“MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置”有了更深的了解,不妨来实际操作一番吧!这里是编程笔记网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

【文章转 东台网站制作 http://www.1234xp.com/dongtai.html 提供,感恩】
上一篇:Android无需root实现apk的静默安装
下一篇:没有了
网友评论