and 的使用
- 代码
public List<SysUser> testQueryWrapper() {
QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
//查询年龄为2的
queryWrapper1.lambda().eq(SysUser::getAge, 2);
System.out.println(this.list(queryWrapper1));
//查询年龄为2或者名字是老六的
QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().and(
wrapper1 -> wrapper1.eq(SysUser::getAge, 2)
.or().eq(SysUser::getName, "老六")));
List<SysUser> list = this.list(queryWrapper);
System.out.println(list);
return list;
}
- 输出sql
年龄为2的数据
==> Preparing: SELECT id,name,age,user_type,status,deleted,create_time,update_time FROM sys_user WHERE deleted=0 AND (age = ?)
==> Parameters: 2(Integer)
<== Columns: id, name, age, user_type, status, deleted, create_time, update_time
<== Row: 1, 张三, 2, null, null, 0, 2019-12-26 00:00:00.0, 2020-10-30 14:08:01.0
<== Row: 12, 王五, 2, 0, null, 0, 2021-01-07 00:00:00.0, null
<== Total: 2
年龄为2或者名字叫老六的数据
==> Preparing: SELECT id,name,age,user_type,status,deleted,create_time,update_time FROM sys_user WHERE deleted=0 AND ((age = ? OR (name = ?)))
==> Parameters: 2(Integer), 老六(String)