MybatisPlus自定义SQL使用条件构造器和分页

1 前言

我们知道MybatisPlus对单表操作很方便,特别是其条件构造器和分页插件,避免我们书写简单重复的SQL,提高我们的效率。其实在自定义SQL中也可以使用MybatisPlus提供的条件构造器和分页插件,下面就来介绍如何自定义SQL中如何使用。

2 环境准备

  • 数据库新建student和teacher两张表

student表结构如下

id name teacher_id
1 小明 1
2 小红 1
3 小白 2

teacher表结构如下

id name
1 老师1
2 老师2
  • 项目的创建此处就省略了。导入如下依赖
xml 复制代码
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- 测试 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

<!-- MybatisPlus依赖 -->
<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.5.3.1</version>
</dependency>

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
</dependency>

<!-- MySQL驱动 -->
<dependency>
   <groupId>com.mysql</groupId>
   <artifactId>mysql-connector-j</artifactId>
   <scope>runtime</scope>
</dependency>
  • yml配置
yaml 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///test
    username: root
    password: root
mybatis-plus:
  # 开启MybatisPlus的日志打印
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # 如果mapper文件所在位置为resources/mapper下,此项可不配置
  mapper-locations: classpath:mapper/*.xml
  • 实体类

Student实体类

java 复制代码
package com.example.entity;

@Data
@TableName("student")
public class Student {
   //自动递增的主键
   @TableId(value = "id", type = IdType.AUTO)
   private Integer id;
   private String name;
   private Integer teacherId;
}

Teacher实体类

java 复制代码
package com.example.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("teacher")
public class Teacher {
   @TableId(value = "id", type = IdType.AUTO)
   private Integer id;
   private String name;
}
  • vo
java 复制代码
package com.example.vo;

/**
 * 用于接收查询出的数据
 */
@Data
public class StudentVo {
   private Integer id;
   private String name;
   private Teacher teacher;
}
  • 注意:在启动类上添加MapperScan注解扫描mapper接口所在包

3 实现

我们以查询出学生信息为例,如下图

3.1 使用条件构造器

  • mapper接口如下

注意:Wrapper形参的格式与下面要相同

java 复制代码
package com.example.mapper;

@Repository
public interface StudentMapper extends BaseMapper<Student> {
   StudentVo mySelectOne(@Param(Constants.WRAPPER) Wrapper<StudentVo> queryWrapper);
}
  • xml配置文件如下

${ew.customSqlSegment}:就是条件构造器中的条件了,不需要写WHERE

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.StudentMapper">
   <resultMap id="studentVoMap" type="com.example.vo.StudentVo">
      <result property="id" column="id" />
      <result property="name" column="name" />
      <association property="teacher" javaType="com.example.entity.Teacher">
         <result property="id" column="tid" />
         <result property="name" column="tname" />
      </association>
   </resultMap>

   <select id="mySelectOne" resultMap="studentVoMap">
      SELECT s.id, s.name, t.id tid, t.name tname
      FROM student s join teacher t on s.teacher_id = t.id
      ${ew.customSqlSegment}
   </select>
</mapper>
  • 测试
java 复制代码
@Test
void contextLoads() {
   QueryWrapper<StudentVo> wrapper = Wrappers.<StudentVo>query()
         .eq("s.id", 1);
   StudentVo studentVo = studentMapper.mySelectOne(wrapper);
   System.out.println(studentVo);
}

执行SQL如下

这样我们就能在自定义SQL中使用条件构造器了。

3.2 使用分页

MybatisPlus使用分页需要配置分页插件,配置如下

java 复制代码
package com.example.config;

@Configuration
public class MybatisPlusConfig {
   @Bean
   public MybatisPlusInterceptor paginationInterceptor() {
      MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
      interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
      return interceptor;
   }
}
  • mapper接口如下
less 复制代码
package com.example.mapper;

@Repository
public interface StudentMapper extends BaseMapper<Student> {
   Page<StudentVo> mySelectPage(Page<StudentVo> iPage, @Param(Constants.WRAPPER) Wrapper<StudentVo> queryWrapper);
}
  • xml文件如下
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.StudentMapper">
   <resultMap id="studentVoMap" type="com.example.vo.StudentVo">
      <result property="id" column="id" />
      <result property="name" column="name" />
      <association property="teacher" javaType="com.example.entity.Teacher">
         <result property="id" column="tid" />
         <result property="name" column="tname" />
      </association>
   </resultMap>

   <select id="mySelectPage" resultMap="studentVoMap">
      SELECT s.id, s.name, t.id tid, t.name tname
      FROM student s join teacher t on s.teacher_id = t.id
      ${ew.customSqlSegment}
   </select>
</mapper>
  • 测试
java 复制代码
@Test
void contextLoads() {
   QueryWrapper<StudentVo> wrapper = Wrappers.<StudentVo>query()
         .eq("s.id", 1);
   Page<StudentVo> page = studentMapper.mySelectPage(Page.of(1, 10), wrapper);
   System.out.println(page.getRecords());
}

执行的SQL如下

我们发现分页也起效果了。

4 总结

虽然我们能够在自定义SQL中使用MybatisPlus提供的条件构造器共和分页插件,但是还是有局限性的。

  1. 不能使用LambdaWrapper
  2. 自定义SQL使用条件构造器,只会将条件原封不动的加在SQL后。例子如下

我们要查询创建时间create_date等于2023年的

java 复制代码
// 我们只能书写为create_date,而不能书写为createDate
QueryWrapper<StudentVo> wrapper = Wrappers.<StudentVo>query()
                                          .eq("s.id", 1)
                                          .eq("create_date", 2023)

5 写在最后

上述观点只是小白在学习过程中对接触的新东西的总结和分享,若存在不足之处,还请大佬指出。

相关推荐
جيون داد ناالام ميづ4 小时前
三、MyBatis 与 Spring Boot 集成:极简配置 + 分页 / 乐观锁实战
spring boot·后端·mybatis
CoderYanger18 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
CoderYanger1 天前
Java EE 进阶:10. MyBatis-Plus使用(第一弹)
java·spring boot·后端·sql·spring·java-ee·mybatis
CoderYanger1 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第七弹):图书管理系统(强制登录)
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
乌暮4 天前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
CoderYanger4 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第四弹):图书管理系统(用户登录+添加图书)
java·数据库·spring boot·mysql·面试·java-ee·mybatis
牧瀬クリスだ4 天前
从零手写博客系统:分层架构与统一响应(一)
java·数据库·架构·mybatis·springboot·安全架构
CoderYanger5 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第二弹)
java·数据库·面试·职场和发展·java-ee·tomcat·mybatis
CoderYanger5 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第一弹)
java·数据库·spring·面试·java-ee·tomcat·mybatis
未秃头的程序猿5 天前
给新模块加了个 @MapperScan,启动崩了,最后发现跟 MapperScan 没关系
java·后端·mybatis