Mybatis-Plus的条件构造器 QueryWrapper & UpdateWrapper

发布时间 2024-01-03 11:37:01作者: 进击的davis

简介

前面我们在学习 Java Spring Boot Mybatis-Plus 的简单使用的时候,是否发现我们在构造查询的时候,基本都是简单的 where 语句的查询,而且也不能去选择字段输出,没关系,Mybatis-Plus 为我们准备了应对方案,那就是 Wrapper 构造器。

总的来说,常用的条件构造器有两类,一类是用于查询的条件构造器-QueryWrapper,一类是用于更新的条件构造器-UpdateWrapper,二者与 Wrapper 的关系如下图:

1704181256(1).png

下面看看这几个类的各自作用。

Wrapper是MyBatis-Plus提供的一种查询条件封装类,用于构建查询条件。这是一个抽象类,主要有 QueryWrapper/UpdateWrapper/LambdaQueryWrapper/LambdaUpdateWrapper多个实现类,来完成查询或更新的条件构造器,由于本篇内容主要学习 QueryWrapper/UpdateWrapper,LambdaQueryWrapper/LambdaUpdateWrapper的内容请移步前往官网查阅。

AbstractWrapper,用于查询条件封装,生成 sql 的 where 条件,内部已经实现大量的条件构造语句,如 eq/lt/gt/like/orderby/groupby/in等条件构造。

QueryWrapper,Entity 对象封装操作类,用于查询。

UpdateWrapper,Update 条件封装操作类,用于更新。

通过 xxxWrapper 我们可以使用的条件构造主要有以下这些:

image.png

QueryMap 的使用

package com.example.springbootmybatisplusdemo.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootmybatisplusdemo.entity.User;
import com.example.springbootmybatisplusdemo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class QueryWrapperTest {
    @Autowired
    private UserMapper userMapper;

    // 查询指定列, select field1, field2, ... from table_name;
    @Test
    public void testSelect() {
        QueryWrapper qw = new QueryWrapper<>();
        qw.select("name", "age");
        List<User> userList = userMapper.selectList(qw);
        for (User user: userList) {
            System.out.println(String.format("name: %s, age=%d", user.getName(), user.getAge()));
        }
    }

    // 带 where条件的查询, select ... from table_name where some_condition;
    @Test
    public void testWhere() {
        QueryWrapper qw = new QueryWrapper<>();
        qw.lt("age", 20);
        List<User> userList = userMapper.selectList(qw);
        System.out.println("result: " + userList.size());
        for (User user: userList) {
            System.out.println(user.toString());
        }
    }

    // 带逻辑关系的 where条件查询,select ... from table_name where some_condition and|or ...;
    @Test
    public void testWhereLogic() {
        QueryWrapper qw = new QueryWrapper<>();
        qw.gt("age", 20);
        qw.lt("age", 40);
        qw.likeLeft("email", "@baomidou.com");
        qw.last("LIMIT 10");

        List<User> userList = userMapper.selectList(qw);
        System.out.println("result: " + userList.size());
        for (User user: userList) {
            System.out.println(user.toString());
        }
    }

    // where子句in查询, select ... from table_name where field1 between xxx and yyy and field2 in (select field2 from table_name2);
    @Test
    public void testWhereIn() {
        QueryWrapper qw = new QueryWrapper<>();
        qw.between("age", 10, 100);
        qw.inSql("age", "SELECT u.age from `user` u where age > 10 and age < 100");

        List<User> userList = userMapper.selectList(qw);
        System.out.println("result: " + userList.size());
        for (User user: userList) {
            System.out.println(user.toString());
        }
    }

    // where子句用SQL函数等
    // 通过map做参数
    @Test
    public void testMapParams() {
        Map<String, Object> params = new HashMap<>();
        params.put("age", 20);
        List<User> userList = userMapper.selectByMap(params);
        System.out.println("result: " + userList.size());
    }
    

    // 返回List,元素为Map
    @Test
    public void testReturnMaps() {
        QueryWrapper qw = new QueryWrapper<>();
        qw.between("age", 10, 100);

        List<Map<String, Object>> userList = userMapper.selectMaps(qw);
        System.out.println("result: " + userList.size());
        for (Map<String, Object> user: userList) {
            System.out.println(user.toString());
        }
    }

}

UpdateWrapper的使用

package com.example.springbootmybatisplusdemo.test;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.springbootmybatisplusdemo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class UpdateWrapperTest {
    @Autowired
    private UserMapper userMapper;

    // update wrapper 的使用,先构造更新条件,再通过 Mapper 更新
    @Test
    public void test() {
        UpdateWrapper uw = new UpdateWrapper<>();
        uw.eq("id", 1);
        uw.set("age", 81);

        int result = userMapper.update(uw);
        System.out.println("Update result: " + result);
    }
}

参考: