说明:
springboot2.3.x + mybatisplus + druid + sybase + mysql 多数据源整合
1、项目依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>dbs-yandype</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.21</version>
</dependency>
<!-- 数据库JDBC3.0连接驱动 -->
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 阿里 druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
2、配置文件 application.yml
spring:
datasource:
dynamic:
primary: mysql-db #设置默认的数据源或者数据源组
datasource:
sybase-db:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: net.sourceforge.jtds.jdbc.Driver
url: jdbc:jtds:sybase://127.0.0.1:6000/TEST;charset=cp936
username: sy
password:
mysql-db:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test_ac?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
druid:
# 连接池的配置信息
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
3、mybatisplus配置类
package com.yandype.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.yandype.mapper")
public class MybatisPlusConfig {
}
4、com.yandype.mapper 包下mapper接口
4.1、sybase 数据源 mapper 定义
package com.yandype.mapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@DS("sybase-db")
public interface SybaseTestUserMapper extends BaseMapper<SybaseTestUser> {
}
4.2、SybaseTestUser entity实体类
package com.yandype.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("TB_TEST_USER")
public class SybaseTestUser {
@TableId("ID")
private String id;
@TableField("USERNAME")
private String username;
@TableField("PASSWORD")
private String password;
@TableField("NAME")
private String name;
@TableField("BZ")
private String bz;
}
4.3、mysql 数据源 mapper 定义
package com.yandype.mapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
// @DS 切换数据源 @DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。
@DS("mysql-db")
public interface MysqlTestUserAcMapper extends BaseMapper<MysqlTestUser> {
}
4.4、MysqlTestUser entity实体类
package com.yandype.entity;
import lombok.Data;
@Data
public class MysqlTestUser {
private Long id;
private String userName;
private String userPhone;
}
5、单元测试
package com.yandype.mapper;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
public class TestUserAcMapperTest{
@Autowired
private SybaseTestUserMapper sybaseTestUserMapper;
@Autowired
private MysqlTestUserMapper mysqlTestUserMapper;
// sybase 数据源测试
@Test
public void sybaseSelectTest(){
List<SybaseTestUser> userList = sybaseTestUserMapper.selectList(null);
System.out.println(userList);
}
// mysql 数据源测试
@Test
public void mysqlSelectTest(){
List<MysqlTestUser> userList = mysqlTestUserMapper.selectList(null);
System.out.println(userList);
}
}
完整项目代码已提交到码云仓库:
https://gitee.com/yandype/springboot-mybats-plus-dbs
参考资料:
mybatis-plus官网文档多数据源配置:
https://baomidou.com/guide/dynamic-datasource.html