经过搜索了解到,一般查询构造条件使用QueryWrapper,修改类型的使用UpdateWrapper;
以下是详细的内容:
QueryWrapper(LambdaQueryWrapper) 和UpdateWrapper(LambdaUpdateWrapper) 的父类是AbstractWrapper,用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件。
注意: entity 生成的 where 条件与 使用各个 api 生成的 where 条件没有任何关联行为。
QueryWrapper和UpdateWrapper区别:
1). QueryWrapper: Entity 对象封装操作类 。
2). UpdateWrapper : Update 条件封装,用于Entity对象更新操作。
QueryWrapper代码:
@Override
public List getUsersBySex(String sex) {
//条件构造器
QueryWrapper wrapper = new QueryWrapper<>();
//eq 代表“ = ”;例如 eq("sex", "男") ---> sex = '男';等同于拼接在sql语句后边的where条件。
wrapper.eq("sex",sex);
//将条件带入查询
List list = this.list(wrapper);
//返回数据
return list;
}
UpdateWrapper代码:
public boolean updateByUserId(String userId){
//条件构造器
UpdateWrapper wrapper = new UpdateWrapper<>();
//传入条件
wrapper.eq("id",userId);
//调用修改方法
return this.update(wrapper);
}
eq(R column, Object val)
eq(boolean condition, R column, Object val)
例: eq("name", "老王")
—>name = '老王'
ne(R column, Object val)
ne(boolean condition, R column, Object val)
例: ne("name", "老王")
—>name <> '老王'
gt(R column, Object val)
gt(boolean condition, R column, Object val)
例: gt("age", 18)
—>age > 18
ge(R column, Object val)
ge(boolean condition, R column, Object val)
例: ge("age", 18)
—>age >= 18
lt(R column, Object val)
lt(boolean condition, R column, Object val)
例: lt("age", 18)
—>age < 18
le(R column, Object val)
le(boolean condition, R column, Object val)
例: le("age", 18)
—>age <= 18
between(R column, Object val1, Object val2)
between(boolean condition, R column, Object val1, Object val2)
例: between("age", 18, 30)
—>age between 18 and 30
or()
or(boolean condition)
eq("id",1).or().eq("name","老王")
—>id = 1 or name = '老王'
or(Consumer<Param> consumer)
or(boolean condition, Consumer<Param> consumer)
or(i -> i.eq("name", "李白").ne("status", "活着"))
—>or (name = '李白' and status <> '活着')
and(Consumer<Param> consumer)
and(boolean condition, Consumer<Param> consumer)
例: and(i -> i.eq("name", "李白").ne("status", "活着"))
—>and (name = '李白' and status <> '活着')
like(R column, Object val)
like(boolean condition, R column, Object val)
LIKE ‘%值%’
例: like("name", "王")
—>name like '%王%'
notLike(R column, Object val)
notLike(boolean condition, R column, Object val)
NOT LIKE ‘%值%’
例: notLike("name", "王")
—>name not like '%王%'
groupBy(R... columns)
groupBy(boolean condition, R... columns)
例: groupBy("id", "name")
—>group by id,name
orderBy(boolean condition, boolean isAsc, R... columns)
例: orderBy(true, true, "id", "name")
—>order by id ASC,name ASC
orderByAsc(R... columns)
orderByAsc(boolean condition, R... columns)
例: orderByAsc("id", "name")
—>order by id ASC,name ASC
orderByDesc(R... columns)
orderByDesc(boolean condition, R... columns)
例: orderByDesc("id", "name")
—>order by id DESC,name DESC
having(String sqlHaving, Object... params)
having(boolean condition, String sqlHaving, Object... params)
例: having("sum(age) > 10")
—>having sum(age) > 10
例: having("sum(age) > {0}", 11)
—>having sum(age) > 11
获取 LambdaWrapper
在QueryWrapper中是获取LambdaQueryWrapper
在UpdateWrapper中是获取LambdaUpdateWrapper
链式写法:
public List getUsersByIdAndDate(Integer userId,Integer age){
//lambda条件构造器
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
//传入条件
queryWrapper.eq(UserEntity::getId,userId);
queryWrapper.eq(UserEntity::getAge,age);
//调用修改方法
return this.list(queryWrapper);
}