最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
MyBatis-Plus中使用EntityWrappe进行列表数据倒序设置方式代码示例
时间:2022-06-29 01:52:04 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下MyBatis-Plus中使用EntityWrappe进行列表数据倒序设置方式代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
使用EntityWrappe进行列表数据倒序设置
情景
项目中使用MyBatis-Plus的EntityWrapper,默认是升序,但实际项目中需要倒序显示,就查看了一下相关资料,在这里记录一下。
EntityWrapper是个很强大的玩意,支持多条件查询 EntityWrapper() wrapper= new EntityWrapper (); wrapper.between(column, val1, val2) wrapper.groupBy(columns) //对应sql中分组 wrapper.eq(column, params) //相当于where条件 wrapper.in(column, value) //sql中in wrapper.notIn(column, value) //sql中 not in wrapper.orderBy(columns, isAsc) //排序 wrapper.exists(value) //相对于sql中exists查询 wrapper.notExists(value) //相当于sql中not exists查询 wrapper.notBetween(column, val1, val2) //相当于sql中在某个范围内使用的between wrapper.ge(column, params) //大于等于 wrapper.le(column, params) //小于等于 wrapper.like(column, value) //模糊查询 wrapper.having(sqlHaving, params) //条件过滤
如下进行倒序案例
使用.orderBy("create_time",false)就可以实现倒序排列查询
@Override public PageUtils queryPage(Mapparams) { //EntityWrapper 可以封装bean参数 String name=(String)params.get("name"); Page page = this.selectPage( new Query (params).getPage(), new EntityWrapper () .like(StringUtils.isNotBlank(name),"name", name)//根据公司名进行查询 .orderBy("create_time",false)//按创建时间进行倒序查询 ); return new PageUtils(page); }
根据上面的可以进行很多条件查询,有兴趣的可以自己试一试哦
条件构造器EntityWrapper用法
EntityWrapper 简介
1、 MybatisPlus 通过 EntityWrapper(简称 EW,MybatisPlus 封装的一个查询条件构造器)或者 Condition(与 EW 类似) 来让用户自由的构建查询条件,简单便捷,没有额外的负担, 能够有效提高开发效率。
2、实体包装器,主要用于处理 sql 拼接,排序,实体参数查询等。
3、注意: 使用的是数据库字段,不是 Java 属性。
4、条件参数说明
测试查询:
@Test public void testEntityWrapperSelect() { //我们需要分页查询tbl_employee表中,年龄在18~50之间且性别为男且姓名为Tom的所有用户 Listemps =employeeMapper.selectPage(new Page (1, 2), new EntityWrapper () .between("age", 18, 50) .eq("gender", 1) .eq("last_name", "Tom") ); System.out.println(emps); //我们需要分页查询tbl_employee表中,年龄在18~50之间且性别为男且姓名为Tom的所有用户 List emps3 = employeeMapper.selectPage( new Page (1,2), Condition.create() .between("age", 18, 50) .eq("gender", "1") .eq("last_name", "Tom") ); System.out.println(emps3); // 查询tbl_employee表中, 性别为女并且名字中带有"老师" 或者 邮箱中带有"a" List emps1 = employeeMapper.selectList( new EntityWrapper () .eq("gender", 0) .like("last_name", "老师") //.or() // SQL: (gender = ? AND last_name LIKE ? OR email LIKE ?) .orNew() // SQL: (gender = ? AND last_name LIKE ?) OR (email LIKE ?) .like("email", "a") ); System.out.println(emps1); //查询性别为女的, 根据age进行排序(asc/desc), 简单分页 List emps2 = employeeMapper.selectList( new EntityWrapper () .eq("gender", 0) .orderBy("age") //默认是升序 //.orderDesc(Arrays.asList(new String [] {"age"})) .last("desc limit 1,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 protected], 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 protected], 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 protected], gender=1, age=22], Employee [id=2, lastName=Jerry, [email protected], gender=0, age=25], Employee [id=7, lastName=王五, [email protected], 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 protected], gender=1, age=22], Employee [id=2, lastName=Jerry, [email protected], gender=0, age=25], Employee [id=7, lastName=王五, [email protected], 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 age
Parameters: 0(Integer)
Total: 2
[Employee [id=7, lastName=王五, [email protected], gender=0, age=null], Employee [id=2, lastName=Jerry, [email protected], 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 DESC
Parameters: 0(Integer)
Total: 2
[Employee [id=2, lastName=Jerry, [email protected], gender=0, age=25], Employee [id=7, lastName=王五, [email protected], 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,3
Parameters: 0(Integer)
Total: 1
[Employee [id=7, lastName=王五, [email protected], gender=0, age=null]]
测试修改:
@Test public void testEntityWrapperUpdate() { Employee employee = new Employee(); employee.setLastName("王五"); employee.setEmail("[email protected]"); employee.setGender(0); employeeMapper.update(employee, new EntityWrapper() .eq("last_name", "Tom") .eq("age", 44) ); }
控制台输出:
Preparing: UPDATE tbl_employee SET last_name=?, email=?, gender=? WHERE (last_name = ? AND age = ?)
Parameters: 王五(String), [email protected](String), 0(Integer), Tom(String), 44(Integer)
Updates: 0
测试删除:
@Test public void testEntityWrapperDelete() { employeeMapper.delete( new EntityWrapper() .eq("last_name", "Tom") .eq("age", 22) ); }
控制台输出:
Preparing: DELETE FROM tbl_employee WHERE (last_name = ? AND age = ?)
Parameters: Tom(String), 22(Integer)
Updates: 1
相关文章
- 《无限暖暖》天星之羽获得位置介绍 12-20
- 《流放之路2》重铸台解锁方法介绍 12-20
- 《无限暖暖》瞄准那个亮亮的成就怎么做 12-20
- 《无限暖暖》魔气怪终结者完成方法 12-20
- 《无限暖暖》曙光毛团获得位置介绍 12-20
- 《无限暖暖》日光果获得位置介绍 12-20