条件构造器
wapper介绍 :
Wrapper : 条件构造抽象类,最顶端父类,抽象类中提供4个方法
AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
AbstractLambdaWrapper : Lambda 语法使用 Wrapper统一处理解析 lambda 获取 column。
LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper : Lambda 更新封装Wrapper
QueryWrapper : Entity 对象封装操作类,不是用lambda语法
UpdateWrapper : Update 条件封装,用于Entity对象更新操作
mybatis-plus条件构造器简介
mybatis-plus提供了AbstractWrapper抽象类,提供了很多sql语法支持的方法,比如模糊查询,比较,区间,分组查询,排序,判断空等等,方便我们用面向对象的方式去实现sql语句。
AbstractWrapper
说明:
QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件
QueryWrapper
List<Company> company = companyService.list(new QueryWrapper<Company>().like("name",appoinment.getCompanyName()));
List<User> salesManager = userService.list(new QueryWrapper<User>().like("name",appoinment.getSalesManagerName()));
UpdateWrapper
contactsService.update(new UpdateWrapper<Contacts>().eq("company_id",company.getId()).set("create_user",(sales.getUserId())));
eq
eq(R column, Object val)
eq(boolean condition, R column, Object val)
等于 =
例: eq(“name”, “张三”)—>name = ‘张三’
between
between(R column, Object val1, Object val2)
between(boolean condition, R column, Object val1, Object val2)
BETWEEN 值1 AND 值2
例: between(“age”, 18, 30)—>age between 18 and 30
like
like(R column, Object val)
like(boolean condition, R column, Object val)
LIKE ‘%值%’
例: like(“name”, “李四”)—>name like ‘%李四%’
isNull
isNull(R column)
isNull(boolean condition, R column)
字段 IS NULL
例: isNull(“name”)—>name is null
isNotNull
isNotNull(R column)
isNotNull(boolean condition, R column)
字段 IS NOT NULL
例: isNotNull(“name”)—>name is not null
in
in(R column, Collection<?> value)
in(boolean condition, R column, Collection<?> value)
字段 IN (value.get(0), value.get(1), …)
例: in(“age”,{1,2,3})—>age in (1,2,3)
in(R column, Object... values)
in(boolean condition, R column, Object... values)
字段 IN (v0, v1, …)
例: in(“age”, 1, 2, 3)—>age in (1,2,3)
inSql
inSql(R column, String inValue)
inSql(boolean condition, R column, String inValue)
字段 IN ( sql语句 )
例: inSql(“age”, “1,2,3,4,5,6”)—>age in (1,2,3,4,5,6)
例: inSql(“id”, “select id from table where id < 3”)—>id in (select id from table where id < 3)
orderByDesc
orderByDesc(R... columns)
orderByDesc(boolean condition, R... columns)
排序:ORDER BY 字段, … DESC
例: orderByDesc(“id”, “name”)—>order by id DESC,name DESC
例子:根据创建时间进行排序(orderByDesc(“create_time”));
/**
*
*/
@GetMapping("/list")
@ApiOperationSupport(order = 2)
@ApiOperation(value = "分页", notes = "传入appoinment")
public R<IPage<AppoinmentVO>> list(AppoinmentVO appoinment, Query query) {
//模糊查询
List<Company> company = companyService.list(new QueryWrapper<Company>().like("name",appoinment.getCompanyName()));
List<User> salesManager = userService.list(new QueryWrapper<User>().like("name",appoinment.getSalesManagerName()));
IPage<Appoinment> pages = appoinmentService.page(Condition.getPage(query),
new QueryWrapper<Appoinment>().
in(null!=company&&company.size()>0,"company_id",company.stream().map(Company::getId).collect(Collectors.toList())).
in(null!=salesManager&&salesManager.size()>0,"sales_manager",salesManager.stream().map(User::getId).collect(Collectors.toList())).orderByDesc("create_time"));
if((null!=appoinment.getSalesManagerName()&&salesManager.size()==0)||null!=appoinment.getCompanyName()&&company.size()==0){
pages.setTotal(0);pages.setRecords(new ArrayList<Appoinment>());}
return R.data(AppoinmentWrapper.build().pageVO(pages));
}
func
func(Consumer<Children> consumer)
func(boolean condition, Consumer<Children> consumer)
func 方法(主要方便在出现if…else下调用不同方法能不断链)
例: func(i -> if(true) {i.eq(“id”, 1)} else {i.ne(“id”, 1)})
or
or()
or(boolean condition)
拼接 OR
注意事项: 主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)
例: eq(“id”,1).or().eq(“name”,“王五”)—>id = 1 or name = ‘王五’
OR 嵌套
or(Consumer<Param> consumer)
or(boolean condition, Consumer<Param> consumer)
例: or(i -> i.eq(“name”, “李四”).ne(“status”, “上班”))—>or (name = ‘李四’ and status <> ‘上班’)
分页插件
XML 自定义分页
UserMapper.java 方法内容
public interface UserMapper {
//可以继承或者不继承BaseMapper
/**
* <p>
* 查询 : 根据state状态查询用户列表,分页显示
* </p>
*
* @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位(你可以继承Page实现自己的分页对象)
* @param state 状态
* @return 分页对象
*/
IPage<User> selectPageVo(Page<?> page, Integer state);
}
UserMapper.xml 等同于编写一个普通 list 查询,mybatis-plus 自动替你分页
<select id="selectPageVo" resultType="com.baomidou.cloud.entity.UserVo">
SELECT id,name FROM user WHERE state=#{
state}
</select>
UserServiceImpl.java 调用分页方法
public IPage<User> selectUserPage(Page<User> page, Integer state) {
// 不进行 count sql 优化,解决 MP 无法自动优化 SQL 问题,这时候你需要自己查询 count 部分
// page.setOptimizeCountSql(false);
// 当 total 为小于 0 或者设置 setSearchCount(false) 分页插件不会进行 count 查询
// 要点!! 分页返回的对象与传入的对象是同一个
return userMapper.selectPageVo(page, state);
}
实例:
/**
* 分页
*/
@GetMapping("/list")
@ApiOperationSupport(order = 2)
@ApiOperation(value = "分页", notes = "传入contacts")
public R<IPage<ContactsVO>> list(Contacts contacts, Query query) {
IPage<Contacts> pages = contactsService.page(Condition.getPage(query), Condition.getQueryWrapper(contacts));
return R.data(ContactsWrapper.build().pageVO(pages));
}
来源:oschina
链接:https://my.oschina.net/u/4386695/blog/4873067