1、条件构造器
MyBatisPlus支持各种复杂的where条件,可以满足日常开发的所有需求。
示例代码:
// 1.构建查询条件
QueryWrapper<User> wrapper = new QueryWrapper<User>()
.select("id", "username", "info", "balance")
.like("username", "o").ge("balance", 1000);
// 2. 查询
List<User> users = userMapper.selectList(wrapper);
users.forEach(System.out::println);
2、自定义SQL
我们可以利用MyBatisPlus的Wrapper来构建复杂的Where条件,然后自己定义SQL语句中剩下的部分。
① 基于Wrapper构建where条件
// 更新条件
List<Object> ids = new ArrayList<>();
ids.add(1L);
ids.add(2L);
ids.add(3L);
int amount = 200;
// 自定义条件
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
.in(User::getId, ids);
userMapper.updateBalanceById(queryWrapper, amount);
②在mapper方法参数中用@Param
注解声明wrapper变量名称,必须是ew
void updateBalanceById(@Param("ew") LambdaQueryWrapper<User> queryWrapper, @Param("amount") int amount);
③自定义SQL,并使用Wrapper条件
<update id="updateBalanceById">
update user
set balance = balance - #{amount} ${ew.customSqlSegment}
</update>
- THE END -
最后修改:2024年5月5日
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:https://www.forest88.top/2024/05/04/mybatis-plus-%e6%9d%a1%e4%bb%b6%e6%9e%84%e9%80%a0%e5%99%a8/
共有 1 条评论