条件构造器
1.1 EntityWrapper
- Mybatis-Plus 通过 EntityWrapper(简称 EW,MP 封装的一个查询条件构造器)或者 Condition(与 EW 类似) 来构建查询条件,简单便捷!
- 实体包装器,主要用于处理 sql 拼接,排序,实体参数查询等!
- 实体包装器 EntityWrapper 继承 Wrapper
注意: 使用的是数据库字段,不是 Java 属性!
2.1 EW
2.1.1 查询
public class TestMPEW01 {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private EmployeeMapper employeeMapper = ioc.getBean("employeeMapper", EmployeeMapper.class);
/**
* 条件构造器 查询操作
*/
@Test
public 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表中, 性别为女并且名字中带有"老师" 或者 邮箱中带有"a"
List<Employee> emps3 = 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(emps3);
// 查询性别为女的, 根据age进行排序(asc/desc), 简单分页
List<Employee> emps4 = employeeMapper.selectList(new EntityWrapper<Employee>()
.eq("gender", 0)
.orderBy("age")
//.orderDesc(Arrays.asList(new String [] {"age"}))
.last("desc limit 1,3")
);
System.out.println(emps4);
}
}
2.1.2 更改
@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)
);
}
2.1.3 删除
@Test
public void testEntityWrapperDelete() {
employeeMapper.delete(new EntityWrapper<Employee>()
.eq("last_name", "Tom")
.eq("age", 45)
);
}
2.1.4 Condition
List<Employee> emps2 = employeeMapper.selectPage(new Page<Employee>(1, 2),
Condition.create()
.between("age", 18, 50)
.eq("gender", "1")
.eq("last_name", "Tom")
);
System.out.println(emps2);
3.1 条件参数说明
查询方式 | 说明
---|---
setSqlSelect | 设置 SELECT 查询字段
where | WHERE 语句,拼接 + WHERE 条件
and | AND 语句,拼接 + AND 字段=值
andNew | AND 语句,拼接 + AND (字段=值)
or | OR 语句,拼接 + OR 字段=值
orNew | OR 语句,拼接 + OR (字段=值)
eq | 等于=
allEq | 基于 map 内容等于=
ne | 不等于<>
gt | 大于>
ge | 大于等于>=
lt | 小于<
le | 小于等于<=
like | 模糊查询 LIKE
notLike | 模糊查询 NOT LIKE
in | IN 查询
notIn | NOT IN 查询
isNull | NULL 值查询
isNotNull | IS NOT NULL
groupBy | 分组 GROUP BY
having | HAVING 关键词
orderBy | 排序 ORDER BY
orderAsc | ASC 排序 ORDER BY
orderDesc | DESC 排序 ORDER BY
exists | EXISTS 条件语句
notExists | NOT EXISTS 条件语句
between | BETWEEN 条件语句
notBetween | NOT BETWEEN 条件语句
addFilter | 自由拼接 SQL
last | 拼接在最后,例如:last("LIMIT 1")
4.1 小结
Integer selectCount(@Param("ew") Wrapper<T> var1);
List<T> selectList(@Param("ew") Wrapper<T> var1);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> var1);
List<Object> selectObjs(@Param("ew") Wrapper<T> var1);
List<T> selectPage(RowBounds var1, @Param("ew") Wrapper<T> var2);
List<Map<String, Object>> selectMapsPage(RowBounds var1, @Param("ew") Wrapper<T> var2);
Integer update(@Param("et") T var1, @Param("ew") Wrapper<T> var2);
Integer updateForSet(@Param("setStr") String var1, @Param("ew") Wrapper<T> var2);
Integer delete(@Param("ew") Wrapper<T> var1);