gt("age", 18)
→ age > 18
ge("age", 18)
→ age >= 18
lt("age", 18)
→ age < 18
le("age", 18)
→ age <= 18
isNull("name")
→ name is null
isNotNull("name")
→ name is not null
@Test
public void queryWrapperOne() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.
isNull("name")
.ge("age", 23)
.isNotNull("email");
// 逻辑删除
int result = userMapper.delete(queryWrapper);
System.out.println(result);
// 最终的语句为:UPDATE user SET deleted=1 WHERE deleted=0 AND name IS NULL AND age >= ? AND email IS NOT NULL
}
eq("name", "老王")
→ name = '老王'
ne("name", "老王")
→ name <> '老王'
@Test
public void queryWrapperTwo() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "BNTang");
// selectOne:返回的是一条记录,当出现多条时会报错
User user = userMapper.selectOne(queryWrapper);
System.out.println(user);
}
between("age", 18, 30)
→ age between 18 and 30
notBetween("age", 18, 30)
→ age not between 18 and 30
个别参数说明:
params
:key
为数据库字段名,value
为字段值如果想表示 null isNull,则在 value 取值的时候传入一个 null, 为
null
时调用 isNull 方法, 为false
时则忽略value
为null
的,也就是不拼接 value 为 null 的查询条件字段
allEq({id:1,name:"老王",age:null})
→id = 1 and name = '老王' and age is null
allEq({id:1,name:"老王",age:null}, false)
→id = 1 and name = '老王'
@Test
public void queryWrapperFour() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
Map<String, Object> queryParamsMap = new HashMap<>();
queryParamsMap.put("id", 1373266771933462530L);
queryParamsMap.put("name", "BNTang");
queryParamsMap.put("age", 23);
queryWrapper.allEq(queryParamsMap);
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
个别参数说明
filter
: 过滤函数, 是否允许字段传入比对条件中
params
与null
、IsNull
同上
allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null})
→name = '老王' and age is null
allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null}, false)
→name = '老王'
如上图可看到,只添加了一个 id 查询的条件因为我写的过滤条件为要包含 i 这个字符串,所以只有第一个 key, value 包含了所以就只添加了 id
like("name", "王")
→name like '%王%'
notLike("name", "王")
→name not like '%王%'
likeLeft("name", "王")
→name like '%王'
likeRight("name", "王")
→name like '王%'
@Test
public void queryWrapperFive() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.notLike("name", "BNTang")
.likeRight("email", "zq");
// 返回值是Map列表
List<Map<String, Object>> users = userMapper.selectMaps(queryWrapper);
users.forEach(System.out::println);
}
in("age",{1,2,3})
→age in (1,2,3)
notIn("age",{1,2,3})
→age not in (1,2,3)
inSql("age", "1,2,3,4,5,6")
→age in (1,2,3,4,5,6)
inSql("id", "select id from table where id < 3")
→id in (select id from table where id < 3)
notInSql("age", "1,2,3,4,5,6")
→age not in (1,2,3,4,5,6)
notInSql("id", "select id from table where id < 3")
→id not in (select id from table where id < 3)
exists("select id from table where age = 1")
→exists (select id from table where age = 1)
notExists("select id from table where age = 1")
→not exists (select id from table where age = 1)
@Test
public void queryWrapperSix() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", 1, 2, 3);
queryWrapper.inSql("id", "select id from user where id < 3");
// 返回值是Object列表
List<Object> users = userMapper.selectObjs(queryWrapper);
users.forEach(System.out::println);
}
注意事项:
主动调用
or
表示紧接着下一个方法不是用and
连接!(不调用or
则默认为使用and
连接)
eq("id",1).or().eq("name","老王")
→id = 1 or name = '老王'
or(i -> i.eq("name", "李白").ne("status", "活着"))
→or (name = '李白' and status <> '活着')
and(i -> i.eq("name", "李白").ne("status", "活着"))
→and (name = '李白' and status <> '活着')
@Test
public void queryWrapperSeven() {
// 修改值
User user = new User();
user.setAge(99);
user.setName("BNTang6666");
// 修改条件
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper
.like("name", "BNTang")
.or()
.between("age", 20, 30);
int result = userMapper.update(user, userUpdateWrapper);
System.out.println(result);
}
@Test
public void queryWrapperEight() {
// 修改值
User user = new User();
user.setAge(99);
user.setName("BNTang6666");
// 修改条件
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper
.like("name", "A")
.or(i -> i.eq("name", "BNTang6666").ne("age", 20));
int result = userMapper.update(user, userUpdateWrapper);
System.out.println(result);
}
orderBy(true, true, "id", "name")
→order by id ASC,name ASC
orderByDesc("id", "name")
→order by id DESC, name DESC
orderByAsc("id", "name")
→order by id ASC,name ASC
@Test
public void queryWrapperNine() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("id");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
注意事项:
只能调用一次, 多次调用以最后一次为准有 sql 注入的风险, 请谨慎使用
last("limit 1")
@Test
public void queryWrapperTen() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.last("limit 1");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
@Test
public void queryWrapperEleven() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id", "name", "age");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
set("name", "老李头")
set("name", "")
→数据库字段值变为空字符串set("name", null)
→据库字段值变为null
setSql("name = '老李头'")
@Test
public void queryWrapperTwelve() {
// 修改值
User user = new User();
user.setAge(10);
// 修改条件
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper
.like("name", "A")
// 除了可以查询还可以使用set设置修改的字段
.set("name", "BNTang")
// 可以有子查询
.setSql(" email = '[email protected]'");
int result = userMapper.update(user, userUpdateWrapper);
System.out.println(result);
}