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);
    }
}

参考:

相关推荐
桑榆肖物35 分钟前
在 Centos7 上部署 ASP.NET 8.0 + YOLOv11 的踩坑实录
后端·yolo·asp.net
Vitalia2 小时前
从入门到精通Rust:资源库整理
开发语言·后端·rust
放羊郎2 小时前
CUDA兼容NVIDA版本关系
开发语言·后端·rust
天上掉下来个程小白6 小时前
登录-10.Filter-登录校验过滤器
spring boot·后端·spring·filter·登录校验
SomeB1oody7 小时前
【Rust中级教程】2.8. API设计原则之灵活性(flexible) Pt.4:显式析构函数的问题及3种解决方案
开发语言·后端·性能优化·rust
Asthenia04127 小时前
Mybatis-Interceptor参数_Invocation解析——公共字段填充设计思路&&阿里规约
后端
Asthenia04129 小时前
基于 MyBatis PageHelper 自定义 PageUtil 的分页实践指南
后端
Hamm10 小时前
封装一个优雅的自定义的字典验证器,让API字典参数验证更湿滑
java·spring boot·后端
刘立军10 小时前
本地大模型编程实战(22)用langchain实现基于SQL数据构建问答系统(1)
人工智能·后端·llm
刘立军10 小时前
本地大模型编程实战(21)支持多参数检索的RAG(Retrieval Augmented Generation,检索增强生成)(5)
人工智能·后端·llm