码迷,mamicode.com
首页 > 移动开发 > 详细

浅谈MyBatis-Plus学习之条件构造器 EntityWrapper

时间:2020-01-10 01:12:22      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:pre   erb   vat   slist   color   mybatis   img   ica   eem   

一、EntityWrapper介绍

在实际的开发过程中更多的是带有复杂条件的SQL操作,而不是简单的增删改查。而在这方面MP也提供了条件构造器EntityWrapper(简称EW)来让开发者自由构建SQL操作条件。

注意:条件封装属性,使用的是数据库字段,而不是Java实体属性!

以下列出的是MybatisPlus提供的条件参数:

技术图片

二、以下是EntityWrapper的使用例子

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})
public class TestEntityWrapper {
    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Test
    public void testEntityWrapperSelectPage() {
        //需要分页查询 tbl_employee 表中,年龄在 18~50 之间性别为男且姓名为 xx 的所有用户
        List<Employee> emps = employeeMapper.selectPage(new Page<Employee>(1, 2), 
                                  new EntityWrapper<Employee>()
                                  .between("age", 18, 50)
                                  .eq("gender", 0)
                                  .eq("last_name", "jack"));
        System.out.println(emps);
        
    }
    
    @Test
    public void testEntityWrapperSelectList() {
        //需要查询 tbl_employee 表中, 性别为男且姓名中带有“J”的 或者 邮箱中带有“a”的所有用户
        List<Employee> emps = employeeMapper.selectList(new EntityWrapper<Employee>()
                                  .eq("gender", 1)
                                  .like("last_name", "j")
                                  //.or()  //WHERE (gender = ? AND last_name LIKE ? OR email LIKE ?)
                                  .orNew() //WHERE (gender = ? AND last_name LIKE ?) OR (email LIKE ?)
                                  .like("email", "a"));
        System.out.println(emps);
    }
    
    /**
     * 条件构造器  修改操作
     */
    @Test
    public void testEntityWrapperUpdate() {
        //修改tbl_employee 表中, 年龄为25且姓名为“jack”的用户
        //将修改的结果封装成对象
        Employee entity = new Employee();
        entity.setLastName("Lucy");
        entity.setEmail("lucy@121.com");
        Integer res = employeeMapper.update(entity,    //UPDATE tbl_employjee SET last_name=?, email=? WHERE (age = ? AND last_name = ?) 
                       new EntityWrapper<Employee>()
                              .eq("age", 25)
                              .eq("last_name", "jack")
                       );
        System.out.println("res: " + res);
    }
    
    /**
     * 条件构造器  删除操作
     */
    @Test
    public void testEntityWrapperDelete() {
        //删除名字有“o”且性别为女的用户
        employeeMapper.delete(new EntityWrapper<Employee>()  //WHERE (last_name LIKE ? AND gender = ?) 
                              .like("last_name", "o")
                              .eq("gender", 0)
                               );
    }
    
    /**
     * 条件构造器,排序操作
     */
    @Test
    public void testEntityWrapperSort() {
        //查询为女的,根据age进行排序(asc/desc),简单排序
        List<Employee> page = employeeMapper.selectPage(new Page<Employee>(1, 2), 
                                  new EntityWrapper<Employee>()  // WHERE (gender = ?) ORDER BY age ASC
                                  .eq("gender", 0)
                                  //.orderAsc(Arrays.asList(new String[] {"age"})));
                                  .orderBy("age")  // WHERE (gender = ?) ORDER BY age Desc
                                  .last("Desc") //手动拼接SQL,有注入危险  
                                  ); 
        System.out.println(page);
    }
    
    /**
     * Condiction的使用
     */
    @Test
    public void testConditionSelectPage() {
        //需要分页查询 tbl_employee 表中,年龄在 18~50 之间性别为男且姓名为 xx 的所有用户
        List<Employee> page = employeeMapper.selectPage(new Page<Employee>(1, 2), 
                                  Condition.create()
                                  .between("age", 18, 50)
                                  .eq("gender", 1)
                                  .eq("last_name", "jack")
                                  );
        System.out.println(page);
        
    }
}

浅谈MyBatis-Plus学习之条件构造器 EntityWrapper

标签:pre   erb   vat   slist   color   mybatis   img   ica   eem   

原文地址:https://www.cnblogs.com/jayhou/p/9824127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!