SpringBoot mybatisplus

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: SpringBoot mybatisplus

spring boot约定优于配置的思想让我们的开发更加专注,mybatis plus让orm开发更加有效率。MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

接下来我们开始整合spring boot使用。首先创建spring boot项目工程,基于2.1.4版本具体可以参考我的GitHub。mybatis-plus模块。地址:https://github.com/UniqueDong/springboot-study

在此之前我们通过maven来管理依赖

  • 拥有 Java 开发环境以及相应 IDE
  • 熟悉 Spring Boot
  • 熟悉 Maven

pom依赖如下所示:里面新加了dynamic-datasource-spring-boot-starter主要用来数据源动态切换,或者读写分离。

网上关于动态数据源的切换的文档有很多,核心只有两种。

  1. 构建多套环境,优势是方便控制也容易集成一些简单的分布式事务,缺点是非动态同时代码量较多,配置难度大。
  2. 基于spring提供原生的 AbstractRoutingDataSource ,参考一些文档自己实现切换。

如果你的数据源较少,场景不复杂,选择以上任意一种都可以。如果你需要更多特性,请尝试本动态数据源。

  1. 数据源分组,适用于多种场景 纯粹多库 读写分离 一主多从 混合模式。
  2. 简单集成Druid数据源监控多数据源,简单集成Mybatis-Plus简化单表,简单集成P6sy格式化sql,简单集成Jndi数据源。
  3. 简化Druid和HikariCp配置,提供全局参数配置。
  4. 提供自定义数据源来源(默认使用yml或properties配置)。
  5. 项目启动后能动态增减数据源。
  6. 使用spel动态参数解析数据源,如从session,header和参数中获取数据源。(多租户架构神器)
  7. 多层数据源嵌套切换。(一个业务ServiceA调用ServiceB,ServiceB调用ServiceC,每个Service都是不同的数据源)
  8. 使用正则匹配或spel表达式来切换数据源(实验性功能)。

#劣势

不能使用多数据源事务(同一个数据源下能使用事务),网上其他方案也都不能提供。

如果你需要使用到分布式事务,那么你的架构应该到了微服务化的时候了。

如果呼声强烈,项目达到800 star,作者考虑集成分布式事务。

PS: 如果您只是几个数据库但是有强烈的需求分布式事务,建议还是使用传统方式自己构建多套环境集成atomic这类,网上百度很多。

#约定

  1. 本框架只做 切换数据源 这件核心的事情,并不限制你的具体操作,切换了数据源可以做任何CRUD。
  2. 配置文件所有以下划线 _ 分割的数据源 首部 即为组的名称,相同组名称的数据源会放在一个组下。
  3. 切换数据源即可是组名,也可是具体数据源名称,切换时默认采用负载均衡机制切换。
  4. 默认的数据源名称为 master ,你可以通过spring.datasource.dynamic.primary修改。
  5. 方法上的注解优先于类上注解

同时我使用了maven的module概念。将myabtis plus模块放在一个父类pom管理的工程下

所以spring boot的依赖师门使用如下方式

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        <version>2.5.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

其对应的数据库 Schema 脚本如下:

``

DROP TABLE IF EXISTS vip_account;
CREATE TABLE vip_account
(
    id BIGINT(20) NOT NULL COMMENT '主键ID',
    username VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名'
    PRIMARY KEY (id)
);

我们使用yaml格式的配置文件,配置如下:

server:
  servlet:
    context-path: /mybatis
  port: 9002
spring:
  application:
    name: mybatis-plus
  datasource:
      # 读写分离
    dynamic:
      hikari:
        driver-class-name: com.mysql.jdbc.Driver
      datasource:
        master:
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/vip?useUnicode=true&characterEncoding=utf8&useSSL=false
        slave:
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/vip?useUnicode=true&characterEncoding=utf8&useSSL=false
# mybatis plus配置
mybatis-plus:
  global-config:
    db-config:
      id-type: auto
  # 定义我们的mapper.xml文件路径
  mapper-locations: classpath:/mapper/**/*Mapper.xml
  type-aliases-package: zero.springboot.study.mybatisplus.domain
  configuration:
    # 自动将数据库字段的下划线的下一个字母转驼峰,比如user_name 转为userName
    map-underscore-to-camel-case: true

这里mybatis plus利用springboot的starter自动装配特性就帮我们配置好了。有点类似于Java的SPI机制。

创建spring boot启动类

@SpringBootApplication
public class MybatisPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }
}
  • MybatisPlusConfig 配置告诉mybatis我们存放mapper接口所在的包
  • Mybatis plus ServiceImpl相当于BaseMapper的封装拓展
  • BaseMapper提供了单表的CRUD方便我们快速开发。
    编码时刻
    创建实体类 VipAccount(此处使用了 Lombok 简化代码)
    BaseMapper使用
    编写我们的Mapper接口类VipAccountMapper
    我们的Mapper接口只要继承mybatis plus的BaseMapper就有了基本的CRUD功能
    代码如下:
    @DS("master")标识使用的是那个数据源,该注解也可以使用在mapper的方法上标识同一个mapper不同方法使用不同的数据源,轻松实现读写分离
  1. @DS("master")
  2. publicinterfaceVipAccountMapperextendsBaseMapper{

  3. }
  4. @Data
  5. publicclassVipAccountimplementsSerializable{
  6. privateLong id;
  7. privateString username;
  8. }

开始使用

大功告成,最后就可以愉快的使用了。跟多的mybatis plus功能特性可以参考官方文档 Mybatis Plus,这里重点是合理的整合方式,通过yaml简单配置开箱即用。使用到的SQL表数据如下

单元测试走起

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import zero.springboot.study.mybatisplus.domain.VipAccount;
import zero.springboot.study.mybatisplus.mapper.master.VipAccountMapper;
import zero.springboot.study.mybatisplus.mapper.slave.VipAccountMapperSlave;
import zero.springboot.study.mybatisplus.service.vip.VipAccountServiceImpl;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisPlusApplication.class)
public class MybatisPlusApplicationTests {
    @Autowired
    private VipAccountServiceImpl vipAccountService;
    @Autowired
    private VipAccountMapper vipAccountMapper;
    @Autowired
    private VipAccountMapperSlave vipAccountMapperSlave;
    @Test
    public void testServiceCrud() {
        VipAccount vipAccount = new VipAccount();
        vipAccount.setUsername("青龙");
        vipAccountService.saveOrUpdate(vipAccount);
    }
    @Test
    public void testMapperCrud() {
        vipAccountMapper.deleteById(2);
        VipAccount vipAccount = new VipAccount();
        vipAccount.setUsername("青龙");
        vipAccountMapper.insert(vipAccount);
        VipAccount data = vipAccountMapperSlave.selectById(3);
        System.out.println(data.toString());
    }
}

您的点赞与关注、是我们的共同进步。



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
SQL Java 关系型数据库
SpringBoot整合MybatisPlus
SpringBoot整合MybatisPlus
29 0
|
1月前
|
druid Java 数据库连接
Spring Boot3整合MyBatis Plus
Spring Boot3整合MyBatis Plus
45 1
|
19天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
1月前
|
Java fastjson Apache
Spring Boot+Gradle+ MyBatisPlus3.x搭建企业级的后台分离框架
Spring Boot+Gradle+ MyBatisPlus3.x搭建企业级的后台分离框架
31 1
|
1月前
|
Java 关系型数据库 MySQL
springboot+mybatis-plus实例demo
springboot+mybatis-plus实例demo
29 0
|
1月前
|
SQL Java 关系型数据库
MyBatisPlus学习笔记(SpringBoot版)
MyBatisPlus学习笔记(SpringBoot版)
99 0
|
1月前
|
前端开发 JavaScript Java
springboot+mybatis plus+vue+elementui+axios 表格分页查询demo
springboot+mybatis plus+vue+elementui+axios 表格分页查询demo
33 0
|
1月前
|
前端开发 Java 数据安全/隐私保护
【五】springboot整合mybatis-plus
【五】springboot整合mybatis-plus
18 0
|
1月前
|
SQL Java 数据库连接
【四】springboot整合mybatis-plus
【四】springboot整合mybatis-plus
35 0
|
1月前
|
SQL 安全 Java
Springboot整合MybatisPlus
Springboot整合MybatisPlus
39 0