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

简介

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

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

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

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 我们可以使用的条件构造主要有以下这些:

QueryMap 的使用

java 复制代码
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的使用

java 复制代码
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);
    }
}

参考:

相关推荐
parafeeee6 小时前
程序人生-Hello’s P2P
数据库·后端·asp.net
bug攻城狮7 小时前
Spring Boot应用内存占用分析与优化
java·jvm·spring boot·后端
今天你TLE了吗8 小时前
JVM学习笔记:第八章——执行引擎
java·jvm·笔记·后端·学习
XPoet8 小时前
AI 编程工程化:Rule——给你的 AI 员工立规矩
前端·后端·ai编程
韩立学长8 小时前
基于Springboot校园志愿者服务平台77pz7812(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
Java基基9 小时前
Spring让Java慢了30倍,JIT、AOT等让Java比Python快13倍,比C慢17%
java·开发语言·后端·spring
qq_12498707539 小时前
基于SpringBoot微信小程序的智能在线预约挂号系统(源码+论文+部署+安装)
spring boot·后端·微信小程序·毕业设计·计算机毕设·毕业设计源码
Victor3569 小时前
MongoDB(34)什么是聚合管道(Aggregation Pipeline)?
后端
Victor3569 小时前
MongoDB(35)聚合操作的常见阶段有哪些?
后端
追逐时光者10 小时前
2026年全面且实用的 Visual Studio 插件推荐,开发效率提升利器!
后端·visual studio