Java之Mybatis-Plus

概述

MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发提高效率而生。MyBatis-Plus提供了通用的mapper和service,可以在不编写任何SQL语句的情况下,快速的实现对单表的CRUD、批量、逻辑州除、分页等操作。本文从MyBatis- Plus的特性及使用,到MyBatis-Pus所提供的优秀的插件,以及多数据源的配置都有详细的讲解。
本文主要以MysQL数据库为案例,使用ldea作为IDE,使用Maven作为构建工具,使用Spring Boot展示MyBatis-Plus的各个功能,所以需要有MyBatis和Spring Boot的基础

内容

image.png

官网

https://baomidou.com
image.png

特性

官网地址:https://baomidou.com/pages/24112f/#%E7%89%B9%E6%80%A7

支持数据库

任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库,具体支持情况如下,如果不在下列表查看分页部分教程 PR 您的支持。

  • MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb
  • 达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据库,瀚高数据库

框架结构

image.png
第一步:Scan Entity扫描实体,根据反射技术对实体类中的属性进行抽取;
第二步:分析表与实体类中的关系,以及表字段与实体属性之间的关系;
第三步:根据当前调用的方法来生成对应的SQL语句,再将生成的SQL语句注入到MyBatis的容器中

Quick Start

环境介绍

IDE:idea
JDK:JDK8
构建工具:Maven
数据库:MySQL:8.0.29
SpringBoot:2.7.7
MyBatis-Plus:3.5.1

数据库

-- 创建MyBatis-Plus测试数据库
CREATE DATABASE `mybatis_plus`;

USE `mybatis_plus`;

-- 创建测试user表
DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user`(
`id`	BIGINT(20) NOT NULL COMMENT '主键',
`name` VARCHAR(30) DEFAULT NULL COMMENT'姓名',
`age` INT(11) DEFAULT NULL COMMENT '年龄',
`email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY(`id`)
);

-- 添加测试数据
INSERT INTO `user` (id,name,age,email) VALUES
(1,'Jone',18,'test1@test.com'),
(2,'Jack',20,'test2@test.com'),
(3,'Tom',28,'test3@test.com'),
(4,'Sandy',21,'test4@test.com'),
(5,'Billie',24,'test5@test.com');

MyBatis-Plus在数据插入时,默认会使用雪花算法来生成id,所以采用bigint类型

创建工程

image.png

pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xiu</groupId>
    <artifactId>mybatis-plus-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis-plus-demo</name>
    <description>mybatis-plus-demo</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <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>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

MyBatis-Plus的依赖

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

image.png
** 注意:**
1、驱动类spring.datasource.driver-class-name:
SpringBoot2.0(内置jdbc5驱动),驱动类使用:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
SpringBoot2.1及以上(内置jdbc8驱动),驱动类使用:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
若配置与版本不对应,会有警告信息!!!
2、连接地址spring.datasource.url
MySQL5.7版本的url:
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=UTF-8&useSSL=false
MySQL8.0版本的url:
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimeZone=GMT%2B&characterEncoding=UTF-8&useSSL=false
否则运行时,会报错:java.sql.SQLException:The server time zone value…
当前MySQL驱动为8版本的且SpringBoot为2.7.7

application.properties 配置文件
#端口
server.port=8080

#数据源配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimeZone=GMT%2B&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=19960327xiu
实体类

MyBatis中有ORM的概念,对象关系映射,表中字段要与实体进行映射。

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author zhangzengxiu
 * @date 2022/12/24
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
   
    private static final long serialVersionUID = 4916508803893379409L;

    /**
     * 主键
     */
    private Long id;

    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 邮箱
     */
    private String email;

}
Mapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiu.mybatisplusdemo.pojo.User;

/**
 * @author zhangzengxiu
 * @date 2022/12/24
 */
public interface UserMapper extends BaseMapper<User> {
   
}

说明:
BaseMapper是由MyBatis-Plus提供的通用的Mapper,包含了对单表的各种CRUD操作

启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.xiu.mybatisplusdemo.mapper")
@SpringBootApplication
public class MybatisPlusDemoApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(MybatisPlusDemoApplication.class, args);
    }

}

说明:
@MapperScan注解是用来扫描,对应包下的所有Mapper接口, 所动态生成的代理类交给IOC容器进行管理
在这里插入图片描述

测试用例

image.png
报错原因:
@MapperScan注解是用来扫描,对应包下的所有Mapper接口, 所动态生成的代理类交给IOC容器进行管理。并不是将接口交给IOC容器进行管理,IOC容器中只能存在类所对应的Bean,而不能存在接口所对应的Bean,所以是将UserMapper动态生成的代理类放进IOC容器中,IDE在编译时, 认为UserMapper无法自动装配,但是,运行时不会有问题。
若强迫症,不希望看到红色波浪线,可以在UserMapper的接口上添加一个注解:@Repository,标识为持久层组件。
即可解决。

import com.xiu.mybatisplusdemo.mapper.UserMapper;
import com.xiu.mybatisplusdemo.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * @author zhangzengxiu
 * @date 2022/12/24
 */
@SpringBootTest
public class UserMapperTest {
   

    @Autowired
    private UserMapper userMapper;

    /**
     * 测试查询列表
     * 有条件:构造queryWrapper
     * 无条件传入:null
     */
    @Test
    public void testQueryList() {
   
        //null表示没有条件,若有条件,需要构造queryWrapper
        List<User> userList = userMapper.selectList(null);
        userList.forEach(System.out::println);
    }


}

之前我们需要在接口中写方法,并在对应的映射文件中编写SQL语句,但是现在我们仅需要使用MyBatis-plus为我们提供的方法即可。
测试结果:
image.png
测试成功!
当前我们只是根据extends BaseMapper设置了User的范型,即找到了对应的user表。
将user表中的字段赋值给了User类中对应的属性。
BaseMapper中存在各种各样的方法:
image.png

加入日志

当前我们只看到了MyBatis-Plus为我们提供的方法,即结果,如果要查看实际操作所生成的SQL语句,需要添加日志。
在application.properties中添加配置即可。

#日志配置
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

再次执行测试用例
image.png
然而,我们当前并没有指明我们要操作的表,以及表中的字段和实体类中属性的对应关系。
因为,当前我们的表名与类名刚好一致,表中的字段与属性刚好一一对应,完全匹配。
image.png
该图中显示:先去扫描实体,然后反射抽取实体类中属性,再分析表及表字段,再生成SQL注入容器。
所以,我们当前要操作的表及表字段由实体类及属性来决定

BaseMapper

源码:

/**
 * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
 * <p>这个 Mapper 支持 id 泛型</p>
 *
 * @author hubin
 * @since 2016-01-23
 */
public interface BaseMapper<T> extends Mapper<T> {
   

    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     */
    int insert(T entity);

    /**
     * 根据 ID 删除
     *
     * @param id 主键ID
     */
    int deleteById(Serializable id);

    /**
     * 根据实体(ID)删除
     *
     * @param entity 实体对象
     * @since 3.4.4
     */
    int deleteById(T entity);

    /**
     * 根据 columnMap 条件,删除记录
     *
     * @param columnMap 表字段 map 对象
     */
    int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * 根据 entity 条件,删除记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 删除(根据ID或实体 批量删除)
     *
     * @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<?> idList);

    /**
     * 根据 ID 修改
     *
     * @param entity 实体对象
     */
    int updateById(@Param(Constants.ENTITY) T entity);

    /**
     * 根据 whereEntity 条件,更新记录
     *
     * @param entity        实体对象 (set 条件值,可以为 null)
     * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    int update(@Param(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值