简介
本文介绍Mybatis-Plus中的QueryWrapper的用法。
全量查询
通过设置实体类的值来查询:若某一项设置为null,则不会拼接sql。
通过Map和allEq来查询:若某一项设置为null,则会拼接sql。(若想过滤null项,allEq的第二个参数设为false)
select示例:new QueryWrapper<>() .select(“name”, “age”)……
等值查询
查询名字叫“Tony”的,年龄等于20的。
- seletOne返回的是一条实体记录,当出现多条时会报错。
- 如果需要有多条记录时只取一条,可以使用IService.java的getOne(Wrapper<T>, Boolean)方法,指定第二个参数为false。
方案1:直接用eq
public void testSelectEqList() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", "张三"); queryWrapper.eq("age", 28); queryWrapper.eq("last_name", null); // 这样也可以 // queryWrapper.eq("name", "张三").eq("age", 28).eq("last_name", null); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
执行结果
==> Preparing: SELECT id,name,last_name,email,gender,age FROM tb_user WHERE (name = ? AND age = ? AND last_name = ?) ==> Parameters: 张三(String), 28(Integer), null <== Total: 0
方案2:传实体类
public void testSelectList() { User user = new User(); user.setName("张"); user.setAge(28); user.setLastName(null); QueryWrapper<User> queryWrapper = new QueryWrapper<>(user); //User user = userMapper.selectOne(queryWrapper); //查询一条记录 //System.out.println(user); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
执行结果
==> Preparing: SELECT id,name,last_name,email,gender,age FROM tb_user WHERE name=? AND age=? ==> Parameters: 张(String), 28(Integer) <== Total: 0
方案3:Map+allEq
public void selectWrapper13() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); Map<String, Object> map = new HashMap<>(); // 普通查询 map.put("name", "张三"); map.put("age",28); map.put("last_name",null); queryWrapper.allEq(map); // queryWrapper.allEq(map, false);// 传入false自动过滤为null的 // 自动过滤name参数 //queryWrapper.allEq((key, value) -> !key.equals("name"), map); //User user = userMapper.selectOne(queryWrapper); //查询一条记录 //System.out.println(user); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
执行结果
==> Preparing: SELECT id,name,last_name,email,gender,age FROM tb_user WHERE (name = ? AND last_name IS NULL AND age = ?) ==> Parameters: 张三(String), 28(Integer) <== Total: 0
传入false执行结果
==> Preparing: SELECT id,name,last_name,email,gender,age FROM tb_user WHERE (name = ? AND age = ?) ==> Parameters: 张三(String), 28(Integer) <== Total: 0
多条件查询
多条件查询
查询姓王的,年龄大于等于20,小于等于40.并且email不为空
public void selectWrapper01() { QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.likeRight("name", "王"); queryWrapper.between("age", 20, 40); queryWrapper.isNotNull("email"); // 这样写也可以 // queryWrapper.likeRight("name", "王") // .between("age", 20, 40) // .isNotNull("email"); List list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
多条件查询并排序
查询姓王的,或者年龄大于30。按照年龄降序,如果年龄相同按照id升序
public void selectWrapper02() { QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.likeRight("name", "王"); queryWrapper.or(); queryWrapper.ge("age", 30); queryWrapper.orderByDesc("age"); queryWrapper.orderByAsc("id"); // 这样写也可以 // queryWrapper.likeRight("name", "王") // .or() // .ge("age", 30) // .orderByDesc("age") // .orderByAsc("id"); List list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
返回部分字段
法1:返回需要的字段
返回多个列
public void selectWrapper09() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.select("name", "age", "phone").likeRight("name", "王").le("age", 30); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
注意,返回单个列时,不能直接赋值给List<String>,因为userMapper对应的泛型是User实体, 返回时的泛型也是List<User>。如果想返回List<String>,必须自定义SQL,将实体直接定为Object。
法2:返回需要的字段
public void selectWrapper12() { User user = new User(); user.setName("张"); user.setAge(28); QueryWrapper<User> queryWrapper = new QueryWrapper<>(user, "id", "last_name"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
执行结果
==> Preparing: SELECT id,last_name FROM tb_user WHERE name=? AND age=? ==> Parameters: 张(String), 28(Integer) <== Total: 0
法3:过滤不需要的字段
public void selectWrapper10() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.select(User.class, p -> !p.getColumn().equals("parent_id") && !p.getColumn().equals("create_time")).likeRight("name", "王"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
condition
简介
所有的执行条件前边都有一个boolean类型,表示是否加入sql语句。示例:
QueryWrapper<User> wrapper = new QueryWrapper<>(); //下边这两种是等效的 if(user.getName() != null){ wrapper.eq("name", user.getName()); } wrapper.eq(user.getName() != null, "name", user.getName()));
QueryWrapper<User> wrapper = new QueryWrapper<>(); //下边这两种是不等效的 if(ids != null){ wrapper.in("id", Arrays.asList(ids)); } wrapper.in(ids != null, "id", Arrays.asList(ids));
因为带boolean条件的方法,仍会将所有参数往下传。对于in或者notIn来说,会执行Arrays.asList(ids),如果ids为null,则会报异常:
java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:203) at java.util.Arrays$ArrayList.<init>(Arrays.java:3813) at java.util.Arrays.asList(Arrays.java:3800)
执行条件用来简化代码
private void condition(String name, String email) { QueryWrapper<User> wrapper = new QueryWrapper<>(); // 需要六行,太麻烦 // if(StringUtils.isNotBlank(name)){ // wrapper.like("name",name); // } // if(StringUtils.isNotBlank(name)){ // wrapper.like("name",name); // } // 用此法只需两行 wrapper.like(StringUtils.isNotBlank("name"), "name", name) .like(StringUtils.isNotBlank("email"), "email", email); List<User> users = userMapper.selectList(wrapper); for (User user : users) { System.out.println(user.toString()); } }
nested、and、in
多条件查询(nested)
(年龄小于40或邮箱不为空),并且名字为王姓
public void selectWrapper06() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .nested(qw -> qw.lt("age", 40).or().isNotNull("email")) .likeRight("name", "王"); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
多条件查询(and)
名字为王姓,并且年龄小于40或邮箱不为空
public void selectWrapper04() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.likeRight("name", "王"); queryWrapper.and(qw -> qw.lt("age", 40).or().isNotNull("email")); List list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
in
public void selectWrapper07() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.in("age", Arrays.asList(21, 26, 28)); List<User> users = userMapper.selectList(queryWrapper); users.forEach(System.out::println); }
其他操作
根据 Wrapper 条件,查询总记录数
public void selectCount() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", "lqf"); Integer count = userMapper.selectCount(queryWrapper); System.out.println(count); }
根据 wrapper 条件,查询全部记录
queryWrapper 实体对象封装操作类(可以为 null)为null查询全部
public void selectList() { List<User> list = userMapper.selectList(null); System.out.println(list); }
根据 Wrapper 条件,返回maps
queryWrapper 实体对象封装操作类(可以为 null)
public void testSelectNull() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(null); List<Map<String, Object>> mapList = userMapper.selectMaps(queryWrapper); mapList.forEach(System.out::println); }
打印结果
{gender=1, last_name=东方不败, id=321eece6b1620ab97123785edbdef490, email=dfbb@163.com, age=20} {gender=1, last_name=Tony, id=101eece6b1620ab97ffa8c8edbdef490, email=aaa@163.com, age=20} {gender=2, last_name=Pepper, id=9d532cbadd8ea0beb7ea5a7c867bc863, email=123@163.com, age=20}
根据 entity 条件,删除记录
QueryWrapper实体对象封装操作类(可以为 null),下方获取到queryWrapper后删除的查询条件为name字段为null的and年龄大于等于12的and email字段不为null的
public void delete() { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .isNull("name") .ge("age", 12) .isNotNull("email"); int delete = userMapper.delete(queryWrapper); System.out.println("delete return count = " + delete); }
根据 entity 条件,查询全部记录(并翻页)
page 分页查询条件(可以为 RowBounds.DEFAULT)
queryWrapper 实体对象封装操作类(可以为 null)
public void selectPage() { Page<User> page = new Page<>(1, 5); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); IPage<User> userIPage = userMapper.selectPage(page, queryWrapper); System.out.println(userIPage); } //需要在项目中加入分页插件 @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); }
打印结果
==> Preparing: SELECT COUNT(1) FROM user ==> Parameters: <== Columns: COUNT(1) <== Row: 100 ==> Preparing: SELECT id,name,age,email,status FROM user LIMIT 0,5 ==> Parameters: <== Columns: id, name, age, email, status <== Row: 1046282328366391319, lqf, 12, lqf@163.com, 0 <== Row: 1046282328366391320, lqf, 12, lqf@163.com, 0 <== Row: 1046282328366391321, lqf, 12, lqf@163.com, 0 <== Row: 1046282328366391322, lqf, 12, lqf@163.com, 0 <== Row: 1046282328366391323, lqf, 12, lqf@163.com, 0 <== Total: 5
根据 Wrapper 条件,查询全部记录(并翻页)
page 分页查询条件
queryWrapper 实体对象封装操作类
和上个分页同理只是返回类型不同
public void selectMapsPage() { Page<User> page = new Page<>(1, 5); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, queryWrapper); System.out.println(mapIPage); }
请先
!