场景
dynamic-datasource-spring-boot-starter实现动态数据源Mysql和Sqlserver:
dynamic-datasource-spring-boot-starter实现动态数据源Mysql和Sqlserver_dynamic-datasource-spring-boot-starter mysql sqlse-CSDN博客
SpringBoot中整合MybatisPlus快速实现Mysql增删改查和条件构造器:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/135646024
在上面的基础上实现同时连接mysql和postgresql多数据源。
注:
博客:
霸道流氓气质-CSDN博客
实现
1、添加依赖
添加postgresql的依赖
<dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId></dependency>
添加mysql驱动
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>
添加多数据源dynamic-datasource的依赖
<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.2.1</version></dependency>
添加mybatisplus的依赖
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency>
完整pom依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--MySQL驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.2.1</version></dependency>
2、修改application.yml配置文件,添加多数据源的配置
spring:datasource:dynamic:primary: master #设置默认的数据源或者数据源组,默认值即为masterstrict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源datasource:master:url:jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driverdbcp2:min-idle: 5 # 数据库连接池的最小维持连接数initial-size: 5 # 初始化连接数max-total: 5 # 最大连接数max-wait-millis: 150 # 等待连接获取的最大超时时间pg:url: jdbc:postgresql://127.0.0.1:5432/testusername: postgrespassword: 123456driver-class-name: org.postgresql.Driverdbcp2:min-idle: 5 # 数据库连接池的最小维持连接数initial-size: 5 # 初始化连接数max-total: 5 # 最大连接数max-wait-millis: 150 # 等待连接获取的最大超时时间
3、postgresql中新建表t_student
并新建实体类
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "t_student")
public class Student implements Serializable {private static final long serialVersionUID = -5514139686858156155L;private Integer id;private String name;private Integer age;private String address;}
4、新建student的mapper
import com.badao.demo.config.DataSourceOfPG;
import com.badao.demo.entity.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;@Repository
@DataSourceOfPG
public interface StudentMapper extends BaseMapper<Student> {}
注意这里的@DataSourceOfPG是自定义注解,你也可以直接使用
@DS("pg")
这里的pg与上面yml中数据源名称对应,为防止pg修改导致多处修改,这里的dynamic datasource的 @DS注解使用
自定义注解实现。
import com.baomidou.dynamic.datasource.annotation.DS;
import java.lang.annotation.*;@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DS("pg")
public @interface DataSourceOfPG {}
关于自定义注解可以参考其他文章
Java中通过反射+自定义注解判断对象中部分属性是否为空,返回为空字段的名称或自定义含义:
Java中通过反射+自定义注解判断对象中部分属性是否为空,返回为空字段的名称或自定义含义_java判断空值注解-CSDN博客
SpringBoot中通过自定义Jackson注解实现接口返回数据脱敏:
SpringBoot中通过自定义Jackson注解实现接口返回数据脱敏_springboot 自定义 jacksonfilter-CSDN博客
user表参考上面博客
实体类
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "t_user")
public class User implements Serializable {private static final long serialVersionUID = -5514139686858156155L;private Integer id;private Integer userId;private String name;private Integer age;@TableField(exist = false)private String address;}
mapper为
@Repository
public interface UserMapper extends BaseMapper<User> {}
5、编写单元测试测试从两个数据源中获取数据
@SpringBootTest
class PGTest {@Autowiredprivate StudentMapper studentMapper;@Autowiredprivate UserMapper userMapper;@Testvoid test1() {List<Student> students = studentMapper.selectList(new LambdaQueryWrapper<>());System.out.println(students);List<User> users = userMapper.selectList(new LambdaQueryWrapper<>());System.out.println(users);}}
单元测试运行结果