一、mysql的多数据源的配置
1. 项目结构
包名 | 说明 |
---|---|
com.kk.configuration | 数据库配置层 |
com.kk.testcss.controller | 控制层 |
com.kk.testcss.dao | 数据库操作层,又分了不同的两个包,分别操作数据源1 和 数据源2 |
com.kk.testcss.service | 业务逻辑层 |
com.kk.model | 实体类 |
com.kk.Application | 启动类 |
2.数据库配置
springboot 主配置文件 application.properties 文件添加数据库的基本信息
#数据源1
network.datasource.url=jdbc:mysql://127.0.0.1:3306/db2?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
network.datasource.username=root
network.datasource.password=root
network.datasource.driverClassName=com.mysql.jdbc.Driver
#数据源2
message.datasource.url=jdbc:mysql://127.0.0.1:3306/db2?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
message.datasource.username=root
message.datasource.password=root
message.datasource.driverClassName=com.mysql.jdbc.Driver
3. 需要导入的依赖
pom.xml 文件如下
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kk</groupId>
<artifactId>csstestdemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>csstestdemo1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- Druid 数据连接池依赖 不能支持sqlserver 2000-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<!--sql server 2000 依赖-->
<dependency>
<groupId>com.microsoft</groupId>
<artifactId>sql-server</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/mssqlserver2.jar</systemPath>
</dependency>
<!--freemarker前端页面模板依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4. 项目结构
5. 多数据源配置
- 编写数据源1的配置类
package com.konka.configuration;
import com.konka.testcss.utils.InitialDatabase;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = com.kk.configuration.NetworkConfig.PACKAGE, sqlSessionFactoryRef = "NetworkSqlSessionFactory")
public class NetworkConfig {
static final String PACKAGE = "com.kk.testcss.dao.network";
static final String MAPPER_LOCATION = "classpath:mapper/network/*.xml";
@Value("${network.datasource.driverClassName}")
private String driverClass;
@Value("${network.datasource.url}")
private String url;
@Value("${network.datasource.username}")
private String user;
@Value("${network.datasource.password}")
private String password;
@Bean(name = "NetworkDataSource")
@Primary
public DataSource NetworkDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "NetworkTransactionManager")
@Primary
public DataSourceTransactionManager NetworkTransactionManager() {
return new DataSourceTransactionManager(NetworkDataSource());
}
@Bean(name = "NetworkSqlSessionFactory")
@Primary
public SqlSessionFactory NetworkSqlSessionFactory(@Qualifier("NetworkDataSource") DataSource NetworkDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(NetworkDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(com.konka.configuration.NetworkConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
- 编写数据源2的配置类
package com.konka.configuration;
import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = com.konka.configuration.MessageConfig.PACKAGE, sqlSessionFactoryRef = "MessageSqlSessionFactory")
public class MessageConfig {
static final String PACKAGE = "com.konka.testcss.dao.message";
static final String MAPPER_LOCATION = "classpath:mapper/message/*.xml";
@Value("${message.datasource.driverClassName}")
private String driverClass;
@Value("${message.datasource.url}")
private String url;
@Value("${message.datasource.username}")
private String user;
@Value("${message.datasource.password}")
private String password;
@Bean(name = "MessageDataSource")
@Primary
public DataSource NetworkDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "MessageTransactionManager")
public DataSourceTransactionManager MessageTransactionManager() {
return new DataSourceTransactionManager(MessageDataSource());
}
@Bean(name = "MessageSqlSessionFactory")
public SqlSessionFactory MessageSqlSessionFactory(@Qualifier("MessageDataSource") DataSource MessageDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(MessageDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(com.konka.configuration.MessageConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
- @Primary 标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。「多数据源配置的时候注意,必须要有一个主数据源,用 @Primary 标志该 Bean」
- @MapperScan 扫描 Mapper 接口并容器管理
- @Value 获取全局配置文件 application.properties 的 kv 配置,并自动装配
sqlSessionFactoryRef 表示定义了 key ,表示一个唯一 SqlSessionFactory 实例
时间: 2024-11-09 14:31:09