本篇内容主要讲解“MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MyBatis-Plus中怎么使用EntityWrappe进行列表数据倒序设置”吧!
使用EntityWrappe进行列表数据倒序设置
情景
项目中使用MyBatis-Plus的EntityWrapper,默认是升序,但实际项目中需要倒序显示,就查看了一下相关资料,在这里记录一下。
EntityWrapper是个很强大的玩意,支持多条件查询 EntityWrapper<CompanyInfoEntity>() wrapper= new EntityWrapper<CompanyInfoEntity>(); 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(Map<String, Object> params) { //EntityWrapper 可以封装bean参数 String name=(String)params.get("name"); Page<CompanyInfoEntity> page = this.selectPage( new Query<CompanyInfoEntity>(params).getPage(), new EntityWrapper<CompanyInfoEntity>() .like(StringUtils.isNotBlank(name),"name", name)//根据公司名进行查询 .orderBy("create_time",false)//按创建时间进行倒序查询 ); return new PageUtils(page); }
根据上面的可以进行很多条件查询,有兴趣的可以自己试一试哦
条件构造器EntityWrapper用法
EntityWrapper 简介
MybatisPlus 通过 EntityWrapper(简称 EW,MybatisPlus 封装的一个查询条件构造器)或者 Condition(与 EW 类似) 来让用户自由的构建查询条件,简单便捷,没有额外的负担, 能够有效提高开发效率。
实体包装器,主要用于处理 sql 拼接,排序,实体参数查询等。
注意: 使用的是数据库字段,不是 Java 属性。
条件参数说明
测试查询:
@Testpublic void testEntityWrapperSelect() { //我们需要分页查询tbl_employee表中,年龄在18~50之间且性别为男且姓名为Tom的所有用户 List<Employee> emps =employeeMapper.selectPage(new Page<Employee>(1, 2), new EntityWrapper<Employee>() .between("age", 18, 50) .eq("gender", 1) .eq("last_name", "Tom") ); System.out.println(emps); //我们需要分页查询tbl_employee表中,年龄在18~50之间且性别为男且姓名为Tom的所有用户 List<Employee > emps3 = employeeMapper.selectPage( new Page<Employee>(1,2), Condition.create() .between("age", 18, 50) .eq("gender", "1") .eq("last_name", "Tom") ); System.out.println(emps3); // 查询tbl_employee表中, 性别为女并且名字中带有"老师" 或者 邮箱中带有"a" List<Employee> emps1 = employeeMapper.selectList( new EntityWrapper<Employee>() .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<Employee> emps2 = employeeMapper.selectList( new EntityWrapper<Employee>() .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=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 age
Parameters: 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 DESC
Parameters: 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,3
Parameters: 0(Integer)
Total: 1
[Employee [id=7, lastName=王五, email=xz@sina.com, gender=0, age=null]]
测试修改:
@Testpublic void testEntityWrapperUpdate() { Employee employee = new Employee(); employee.setLastName("王五"); employee.setEmail("cls@sina.com"); employee.setGender(0); employeeMapper.update(employee, new EntityWrapper<Employee>() .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
测试删除:
@Testpublic void testEntityWrapperDelete() { employeeMapper.delete( new EntityWrapper<Employee>() .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进行列表数据倒序设置”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!