Bootstrap

Mybatis-plus构造器使用

查询

 @Test
    public void test(){
        // 查询用户名包含a,年龄在20-30之间,邮箱信息不为null的用户信息
        //  SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
        //==> Parameters: %a%(String), 20(Integer), 30(Integer)
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name","a")
                .between("age",20,30)
                .isNotNull("email");
        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);
    }

排序

 @Test
    public  void test01(){
        // 查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序
        //SELECT id,name,age,email FROM user ORDER BY age DESC,id ASC
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        QueryWrapper<User> wrapper1 = wrapper.orderByDesc("age")
                .orderByAsc("id");
      List<User> list = userMapper.selectList(wrapper1);
            list.forEach(System.out::println);
    }

删除

  @Test
    public void test02(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.isNull("email");
        int list =  userMapper.delete(wrapper);
        System.out.println("result"+list);
    }

优先级

    @Test
    public void test03(){
        // (年龄大于20并且用户名包含a) 或者邮箱为null 的用户信息修改
        // UPDATE user SET name=?, age=? WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NULL)
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.gt("age",20)
                .like("name","a")
                .or()
                .isNull("email");
        User user = new User();
        user.setName("白毛");
        user.setAge(66);
        int update = userMapper.update(user, wrapper);
        System.out.println("result"+update);
    }
 @Test
    public void test04(){
        // 将用户名中包含有a并且(年龄大于20或者邮箱为null)的信息修改
        // lambda中条件优先执行
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name","a")
                .and(i->i.gt("age",20).or().isNull("email"));
        User user = new User();
        user.setName("小红果");
        int update = userMapper.update(user, wrapper);
        System.out.println(update);

    }

修改

    @Test
    public void test05(){
        // 将用户名中包含有a并且(年龄大于20或者邮箱为null)的信息修改
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.like("name","a")
                .and(i->i.gt("age",20).or().isNull("email"));
        wrapper.set("name","小黑").set("email","[email protected]");
        int update = userMapper.update(null, wrapper);
        System.out.println("result:"+update);
    }

查询

        @Test
    public void test06(){
        String name = "a";
        Integer ageBegin = null;
        Integer ageEnd = 30;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),"name",name)
                .ge(ageBegin != null,"age",ageBegin)
                .le(ageEnd!=null,"age",ageEnd);
            List<User> users = userMapper.selectList(wrapper);
            users.forEach(System.out::println);
        }
 @Test
        public void test07(){
            String name = "a";
            Integer ageBegin = null;
            Integer ageEnd = 30;
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(name),User::getName,name)
                    .ge(ageBegin !=null,User::getAge,ageBegin)
                    .le(ageEnd != null,User::getAge,ageEnd);
            List<User> list = userMapper.selectList(wrapper);
            list.forEach(System.out::println);
        }
  @Test
        public void test08(){
            // 将用户名中包含有a并且(年龄大于20或者邮箱为null)的信息修改
            LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
            wrapper.like(User::getName,"a")
                    .and(i->i.gt(User::getAge,20).or().isNull(User::getEmail));
            wrapper.set(User::getName,"小黑").set(User::getEmail,"[email protected]");
            int update = userMapper.update(null, wrapper);
            System.out.println("result:"+update);
        }

;