使用mybatis-plus进行复杂查询时,往往需要给部分条件加上( ),提高优先级,防止查询出不满足期望的结果。
不加()
queryWrapper.eq(User::getRole, "user").like(User::getUsername,queryParam)
.or().like(User::getIdNumber,queryParam);
生成的sql
SELECT 字段列表 FROM sys_user where role = 'user' AND username LIKE ? OR id_number LIKE ? LIMIT ?
加()
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getRole, "user").and(wrapper -> wrapper.like(User::getUsername,queryParam)
.or().like(User::getIdNumber,queryParam));
生成的sql
SELECT 字段列表 FROM sys_user where role = 'user' AND (username LIKE ? OR id_number LIKE ?) LIMIT ?