简介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 #设置默认的数据源或者数据源组,默认值即为masterstrict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源datasource:master:url: jdbc:mysql://127.0.0.1:3306/db01?characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=trueusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置slave_1:url: jdbc:mysql://127.0.0.1:3306/crm72?characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=trueusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Drivermybatis-plus:mapper-locations: mapper/*.xmltype-aliases-package: com.olive.entityconfiguration:map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImpljackson: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="https://www.huyubaike.com/biancheng/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 = "https://www.huyubaike.com/biancheng/id", type = IdType.AUTO) private Long id; @TableField("user_name") private String name; @TableField("sex") private int sex; @TableField("office") private String office;}
数据库持久类StudentMapper持久类
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> {}
TeacherMapper持久类
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> {}
推荐阅读
- 结合springboot实现,这里对接的是easy版本,工具用的是IDEA,WebStrom 支付宝沙箱服务
- 分布式存储系统之Ceph集群状态获取及ceph配置文件说明
- 原神3.2意识之舟所至之处任务怎么做
- vivox70参数配置_vivox70参数配置详情
- 原神3.2请饮下祝胜之酒任务怎么做
- 原神3.2如临神之畔任务怎么做
- 明日之后四周年庆是什么时候
- 明日之后四周年庆活动有哪些
- 原神正机之神七叶寂照秘密主怎么打
- 收纳梦之岛妆个熊熊怎么通关