4.MP中Wrapper的使用

老马 老马 | 407 | 2022-07-29

Wrapper简介

image-20220729134743697

注意:

查询用QueryWrapper和LambdaQueryWrapper来封装

updateWrapper和LambdaUPdateWrapper不但能封装查询还能更改要更新的对象。

QueryWrapper的使用

QueryWrapper中的很多条件限定都是见名知其意的。下表列出来几个常用的:

image-20220729134854387

1.多条件进行查询

@Test
public void test01() {
    //查询用户,姓李的 & 年龄从20岁到30岁内 & 地址是北京朝阳的
    QueryWrapper<User> queryWrapper = new QueryWrapper();
    queryWrapper.likeRight("name", "李")
            .between("age", 20, 30)
            .eq("address", "北京朝阳");
    List<User> users = this.userMapper.selectList(queryWrapper);
    users.forEach(System.out::println);
}
//sql语句
==>  Preparing: SELECT id,name,age,email,address AS addr,
createtime,updatetime,is_deleted FROM lm_user 
WHERE is_deleted=0 AND (name LIKE ? AND age BETWEEN ? AND ? AND address = ?)
==> Parameters: 李%(String), 20(Integer), 30(Integer), 北京朝阳(String)

2.有排序查询

@Test
public void test02() {
    //按年龄降序查询用户,如果年龄相同则按id升序排列
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.orderByDesc("age")
            .orderByAsc("id");
    List<User> users = this.userMapper.selectList(queryWrapper);
    users.forEach(System.out::println);
}
//sql语句
SELECT id,name,age,email,address AS addr,createtime,updatetime,is_deleted 
FROM lm_user WHERE is_deleted=0 ORDER BY age DESC,id ASC

3.条件优先级查询

@Test
public void test03() {
    //(年龄大于20并且用户名中包含有张)或邮箱为null的用户信息
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.gt("age", 20)
            .like("name", "张")
            .or()
            .isNull("email");
    this.userMapper.selectList(queryWrapper);
    //用户名中包含有张并且(年龄大于20或邮箱为null)的用户信息
    QueryWrapper<User> query = new QueryWrapper<>();
    query.like("name", "张")
            .and(wrapper -> wrapper.gt("age", 20).or().isNull("email"));
    this.userMapper.selectList(query);
}
//sql语句
==>  Preparing: SELECT id,name,age,email,address AS addr,
createtime,updatetime,is_deleted FROM lm_user 
WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NULL)
==> Parameters: 20(Integer), %张%(String)
==>  Preparing: SELECT id,name,age,email,address AS addr,
createtime,updatetime,is_deleted FROM lm_user 
WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
==> Parameters: %张%(String), 20(Integer)

LambdaQueryWrapper的使用

用LamdaQueryWrapper的好处是数据库的字段名不用手写了,可以避免写错的风险。用法和QueryWrapper差不多。

@Test
public void test04() {
    //(年龄大于20并且用户名中包含有张)或邮箱为null的用户信息
    LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper();
    queryWrapper.gt(User::getAge, 20)
            .like(User::getName, "张")
            .or()
            .isNull(User::getEmail);
    List<User> userList = this.userMapper.selectList(queryWrapper);
    userList.forEach(System.out::println);

    //采用链式更方便。查询结果和上面一样的
    List<User> users = new LambdaQueryChainWrapper<>(this.userMapper)
            .gt(User::getAge, 20)
            .like(User::getName, "张")
            .or()
            .isNull(User::getEmail)
            .list();
    users.forEach(System.out::println);
}
//sql语句
==>  Preparing: SELECT id,name,age,email,address AS addr,
createtime,updatetime,is_deleted FROM lm_user 
WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NULL)
==> Parameters: 20(Integer), %张%(String)

UpdateWrapper的使用

@Test
public void test05() {
    //通过UpdateWrapper进行更新
    UpdateWrapper<User> updateWrapper = new UpdateWrapper();
    updateWrapper.gt("age", 20)
            .like("name", "张")
            .or()
            .isNull("email")
            .set("age", 23);
    this.userMapper.update(null, updateWrapper);

    //链式方式进行更新
    UpdateChainWrapper<User> updateChainWrapper = new UpdateChainWrapper<>(this.userMapper);
    updateChainWrapper.gt("age", 20)
            .like("name", "张")
            .or()
            .isNull("email")
            .set("age", 23).update();
}
//sql语句
==>  Preparing: UPDATE lm_user SET age=? WHERE is_deleted=0 
AND (age > ? AND name LIKE ? OR email IS NULL)
==> Parameters: 23(Integer), 20(Integer), %张%(String)

LambdaUpdateWrapper

@Test
public void test06() {
    boolean result = new LambdaUpdateChainWrapper<User>(this.userMapper)
            .set(User::getAge, 23)
            .set(User::getEmail, "laoma@163.com")
            .gt(User::getAge, 20)
            .like(User::getName, "张")
            .or()
            .isNull(User::getEmail)
            .update();
    System.out.println("更新结果:" + result);
}
//sql语句
==>  Preparing: UPDATE lm_user SET age=?,email=? WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NULL)
==> Parameters: 23(Integer), laoma@163.com(String), 20(Integer), %张%(String)

有条件时才加入条件

在实际开发过程中在拼接条件时,经常是判断这个值不为null,空字符串时才加入条件。这个在MP中再每个条件设置的第一个参数condition就是用来判断是否加入条件的条件。

@Test
public void test07() {
    User request = new User();
    request.setAge(null);
    request.setName("老马");
    request.setEmail("laoma@163.com");
    List<User> users = new LambdaQueryChainWrapper<>(this.userMapper)
            .select(User::getId, User::getName, User::getAge, User::getEmail)
            .gt(request.getAge()!=null, User::getAge, 20)
            .like(StringUtils.isNotBlank(request.getName()), User::getName, request.getName())
            .eq(StringUtils.isNotBlank(request.getEmail()), User::getEmail, request.getEmail())
            .list();
    users.forEach(System.out::println);
}
//sql语句
==>  Preparing: SELECT id,name,age,email FROM lm_user 
WHERE is_deleted=0 AND (name LIKE ? AND email = ?)
==> Parameters: %老马%(String), laoma@163.com(String)
文章标签: MybatisPlus
推荐指数:

真诚点赞 诚不我欺~

4.MP中Wrapper的使用

点赞 收藏 评论

关于作者

老马
老马

不要假装很努力,结果不会陪你演戏。

等级 LV2

粉丝 4

获赞 14

经验 204