QueryWrapper是一个优雅的条件拼装器,作用是构建where之后的查询条件。
1、多条件匹配查询,并且多个条件之间是AND的关系
相当于sql中的 where 列1= xxx and 列2= xxx
方法:
default <V> Children allEq(Map<R, V> params) {
return allEq(params, true);
}
代码:
@Test
public void testAllEq() {
System.out.println(("----- allEq method test ------"));
QueryWrapper wrapper = new QueryWrapper();
Map<String,Object> map = new HashMap<>();
map.put("Name", "Jack");
map.put("Age", "20");
wrapper.allEq(map);
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
输出:
==> Preparing: SELECT id,name,age,email FROM user WHERE (Age = ? AND Name = ?)
==> Parameters: 20(String), Jack(String)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 2, Jack, 20, test2@baomidou.com
<== Total: 1
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
2、单个条件匹配查询,也就是指定一个字段和这个字段对应要匹配的值
相当于SQL的 where 列=xxx
方法:
default Children eq(R column, Object val) {
return eq(true, column, val);
}
代码:
@Test
public void testEq() {
System.out.println(("----- eq method test ------"));
QueryWrapper wrapper = new QueryWrapper();
wrapper.eq("Name","Jack");
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
输出:
==> Preparing: SELECT id,name,age,email FROM user WHERE (Name = ?)
==> Parameters: Jack(String)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 2, Jack, 20, test2@baomidou.com
<== Total: 1
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
3、不等于查询,也就是排除掉某写数据
相当于SQL中的 where 列 != xxx
也可以写成
where 列 <> xxx
方法:
default Children ne(R column, Object val) {
return ne(true, column, val);
}
代码:
@Test
public void testNe() {
System.out.println(("----- eq method test ------"));
QueryWrapper wrapper = new QueryWrapper();
wrapper.ne("Name","Jack");
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
输出:
==> Preparing: SELECT id,name,age,email FROM user WHERE (Name <> ?)
==> Parameters: Jack(String)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 1, Jone, 18, test1@baomidou.com
<== Row: 3, Tom, 28, test3@baomidou.com
<== Row: 4, Sandy, 21, test4@baomidou.com
<== Row: 5, Billie, 24, test5@baomidou.com
<== Total: 4
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
4、大于操作
相当于SQL的 where 列 > xxx
方法:
default Children gt(R column, Object val) {
return gt(true, column, val);
}
代码:查询年龄大于22岁的
@Test
public void testGt() {
System.out.println(("----- gt method test ------"));
QueryWrapper wrapper = new QueryWrapper();
wrapper.gt("Age","22");
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
输出:
==> Preparing: SELECT id,name,age,email FROM user WHERE (Age > ?)
==> Parameters: 22(String)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 3, Tom, 28, test3@baomidou.com
<== Row: 5, Billie, 24, test5@baomidou.com
<== Total: 2
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
mybatis-plus还有相似的方法如 ge是大于等于,lt是小于,le是小于等于
5、配置一个有起始和结束区间的查询条件
SQL中的 between xxx and xxx
方法:
default Children between(R column, Object val1, Object val2) {
return between(true, column, val1, val2);
}
代码:
@Test
public void testBetween() {
System.out.println(("----- between method test ------"));
QueryWrapper wrapper = new QueryWrapper();
wrapper.between("Age","21","25");
List<User> userList = userMapper.selectList(wrapper);
userList.forEach(System.out::println);
}
输出:
==> Preparing: SELECT id,name,age,email FROM user WHERE (Age BETWEEN ? AND ?)
==> Parameters: 21(String), 25(String)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 4, Sandy, 21, test4@baomidou.com
<== Row: 5, Billie, 24, test5@baomidou.com
<== Total: 2
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
还有与之相似的 notBetween方法。