前言

Wrapper 使用 lambda 形式可以通过方法引用的方式来使用实体字段名,避免直接写数据库表字段名时的错写名字。

一、new QueryWrapper()

一般的 QueryWrapper 写法:

List<User> userList = baseMapper.selectList(new QueryWrapper<User>().eq("user_id", userId));

需要手写数据库字段 user_id,容易出错,并且与数据库字段产生依赖。

lambda 的 QueryWrapper 写法:

List<User> userList = baseMapper.selectList(new QueryWrapper<User>().lambda().eq(User::getId, userId)));

直接调用类的 get 方法进行关联,有一定代码解耦效果。

二、Wrappers

List<User> userList = baseMapper.selectList(Wrappers.lambdaQuery(User.class).eq(User::getId, userId));

三、lambdaQueryWrapper

查一条数据:

User user = new LambdaQueryChainWrapper<>(baseMapper).eq(User::getId, userId).one();

查对象列表:

List<User> userList = new LambdaQueryChainWrapper<>(baseMapper).eq(User::getId, userId).list();

链式查询得到 list 对象列表,可读性没有 lambdaQuery 好。

四、Wrapper 拼接 sql 的方法

函数说明例子生成的sql语句
eq等于 =eq(“name”, “老王”)name = ‘老王’
ne不等于 <>ne(“name”, ‘老王’name <> ‘老王’
gt大于 >gt(“age”, 18)age > 18
ge大于等于 >=ge(“age”, 18)age >= 18
lt小于 <lt(“age”, 18)age < 18
le小于等于 <=le(“age”, 18)age <= 18
betweenBETWEEN v1 AND v2between(“age”, 18, 24)age between 18 and 24
notBetweenNOT BETWEEN v1 AND v2notBetween(“age”,18,24)age not between 18 and 24
likeLIKE ‘%值%’like(“name”,“王”)name like '%王%’
notLikeNOT LIKE ‘%值%’notLike(“name”,“王”)name not like '%王%’
likeLeftLIKE ‘%值’likeLeft(“name”,“王”)name like '%王
likeRightLIKE ‘值%’likeRight (“name”,“王”)name like '王%’
isNull字段 IS NULLisNull (“name”)name is null
isNotNull字段 IS NOT NULLisNotNull (“name”)name is not null
in字段 IN (v1, v2, v3)in(“age”,{1,2,3} )age in (1,2,3)
notIn字段 NOT IN (v1, v2, v3)notIn(“age”, 1, 2, 3)age not in (1, 2, 3)
inSql字段 IN ( sql语句 )inSql(“id”,“select id from table where id < 3”)id in (select id from table where id < 3)
notInSql字段 NOT IN ( sql语句 )notInSql(“id”, “select id from table where id < 3”)age not in (select id from table where id < 3)
groupBy分组:GROUP BY 字段1, …groupBy(“id”, “name”)group by id, name
orderByAsc升序排序:ORDER BY 字段1, … ASCorderByAsc(“id”, “name”)order by id ASC, name ASC
orderByDesc降序排序:ORDER BY 字段1, … DESCorderByDesc(“id”, “name”)order by id DESC, name DESc
orderBy排序:ORDER BY 字段1, …orderBy(true, true, “id”,“name”)orderBy(true,,true,“id”,“name”)
havingHAVING ( sql 语句 )having(“sum(age) > {0}”,11)having sum(age) > 11
or拼接 OR主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)
and拼接 ADN默认连接符
apply拼接 sql例: apply(“date_format(dateColumn, ‘%Y-%m-%d’) = {0}”,“2008-08-08”)—>date_format(dateColumn,‘%Y-%m-%d’) = ’2008-08-08’")
该方法可用于数据库函数动态入参的params对应前面sqlHaving内部的{index}部分.这样是不会有sql注入风险的,反之会有!
last无视优化规则直接拼接到 sql 的最后,只能调用一次,多次调用以最后一次为准,有sql注入的风险,请谨慎使用。例如 last(“limit 1”)(limit 1)
exists拼接 EXISTSexists(“select id from table where age = 18”)exists(select id from table where age = 18)
notExists拼接 NOT EXISTSnotExists(“select id from table where age = 18”)not exists(“select id from table where age = 18”)
nested正常嵌套不带 AND 或者 ORnested(i -> i.eq(“name”, “李白”).ne(“status”, “活着”))(name = ‘李白’ and status <> ‘活着’)

Wrapper 附带 if 判断条件进行查询

List<SysDept> deptAllList = deptMapper.selectList(
				Wrappers.<SysDept>lambdaQuery().like(StrUtil.isNotBlank(deptName), SysDept::getName, deptName));
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐