首页 > 图灵资讯 > 技术篇>正文

Springboot 之 Mybatis-plus 多数据源

2023-05-04 10:17:51

简介

Mybatis-puls 官方提供的多数据源的使用dynamic-datasource-spring-boot-starter包的 @DS 注意,具体可参考官网:

https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter
pom.以下依赖于xml文件的引入

主要引入dynamic-datasource-spring-boot-starter

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.olive</groupId><artifactId>mybatis-plus-multip-datasource</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>mybatis-multip-datasource</name><url>http://maven.apache.org</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.14</version><relativePath /> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.2</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency></dependencies></project>
配置两个数据源

第一个主数据源分别是第一个(primary),第二数据源(slave_1)具体配置如下:

# server基本配置:  port: 8080# Spring数据库:  datasource:    dynamic:      primary: master #设置默认数据源或数据源组,默认值为master      strict: false #严格匹配数据源,默许false. 当true与指定数据源不匹配时,false使用默认数据源      datasource:        master:          url: jdbc:mysql://127.0.0.1:3306/db01?characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true          username: root          password: root          driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.开始支持SPI可以省略此配置        slave_1:          url: jdbc:mysql://127.0.0.1:3306/crm72?characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true          username: root          password: root          driver-class-name: com.mysql.cj.jdbc.Drivermybatis-plus:  mapper-locations: mapper/*.xml  type-aliases-package: com.olive.entity  configuration:    map-underscore-to-camel-case: true    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  jackson:    serialization:      indent-output: true
创建学生和教师的实体类别

学生实体类

package com.olive.entity;import java.io.Serializable;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;@TableName("t_student")@Datapublic class StudentDO implements Serializable{    @TableId(value="id", type = IdType.AUTO)    private Long id;    @TableField("user_name")    private String name;    @TableField("sex")    private int sex;    @TableField("grade")    private String grade;}

老师实体类

package com.olive.entity;import java.io.Serializable;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;@Data@TableName("t_teacher")public class TeacherDO implements Serializable {@TableId(value = "id", type = IdType.AUTO)private Long id;@TableField("user_name")private String name;@TableField("sex")private int sex;@TableField("office")private String office;}
数据库持久类

Studentmaper持久类

package com.olive.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.olive.entity.StudentDO;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface StudentMapper extends BaseMapper<StudentDO> {}

TeacherMaper持久类

package com.olive.mapper;import com.baomidou.dynamic.datasource.annotation.DS;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.olive.entity.TeacherDO;import org.apache.ibatis.annotations.Mapper;@DS("slave_1")@Mapperpublic interface TeacherMapper extends BaseMapper<TeacherDO> {}

以上两种持久类的实现都是继承BaseMapper该类提供了增删改查的基本方法;需要注意的是,TeacherMapper 类使用了@DS("slave_1")指定数据源;而且StudentMapper未指定,使用默认数据源。

实现服务

先定义操作学生和教师的两个界面

StudentService接口

package com.olive.service;import com.baomidou.mybatisplus.extension.service.IService;import com.olive.entity.StudentDO;public interface StudentService extends IService<StudentDO> {}

Teacherservice接口

package com.olive.service;import com.baomidou.mybatisplus.extension.service.IService;import com.olive.entity.TeacherDO;public interface TeacherService extends IService<TeacherDO> {}

重新定义接口的实现类

StudentServiceimplll类

package com.olive.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.olive.entity.StudentDO;import com.olive.mapper.StudentMapper;import com.olive.service.StudentService;import org.springframework.stereotype.Service;@Servicepublic class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentDO>        implements StudentService {}

ServiceImpl类型TeacherSer

package com.olive.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.olive.entity.TeacherDO;import com.olive.mapper.TeacherMapper;import com.olive.service.TeacherService;import org.springframework.stereotype.Service;@Servicepublic class TeacherServiceImpl extends ServiceImpl<TeacherMapper, TeacherDO>        implements TeacherService {}

常规非常标准地观察界面定义和界面实现。都是实现和继承。Mybatis-Plus提供的标准接口和基类

创建springboot引导类
package com.olive;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan("com.olive.mapper")@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class);    }}
测试
package com.olive;import com.olive.service.StudentService;import com.olive.service.TeacherService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import com.olive.entity.StudentDO;import com.olive.entity.TeacherDO;@SpringBootTestpublic class MybatisPlusTest {@AutowiredStudentService studentService;@AutowiredTeacherService teacherService;@Testpublic void userSave() {StudentDO studentDO = new StudentDO();studentDO.setName(“BUG弄潮儿”);studentDO.setSex(1);studentDO.setGrade(一年级);studentService.save(studentDO);TeacherDO teacherDO = new TeacherDO();teacherDO.setName(Java乐园);teacherDO.setSex(2);teacherDO.setOffice(语文);teacherService.save(teacherDO);}}

上一篇 常用Collection接口下集合,Map接口下集合
下一篇 Springboot 之 JPA 多数据源实现

文章素材均来源于网络,如有侵权,请联系管理员删除。