EntityWrapper 简介
- 1、 MybatisPlus 通过 EntityWrapper(简称 EW,MybatisPlus 封装的一个查询条件构造器)或者 Condition(与 EW 类似) 来让用户自由的构建查询条件,简单便捷,没有额外的负担, 能够有效提高开发效率。
- 2、实体包装器,主要用于处理 sql 拼接,排序,实体参数查询等。
- 3、注意: 使用的是数据库字段,不是 Java 属性。
- 4、条件参数说明:
查询测试
@Test public void testEntityWrapperSelect() { 1. //我们需要分页查询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); 2. //我们需要分页查询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); 3. // 查询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); 4. //查询性别为女的, 根据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]]
修改测试
@Test public 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
删除测试
@Test public 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
文章转载至:https://blog.csdn.net/lizhiqiang1217/article/details/89738906