1.引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
我这里对于多数据源使用的是阿里巴巴的连接池。不必要,可自行选择。
2. application.yml文件中配置
spring:
datasource:
druid:
view:
username: root
password: password
url: jdbc:mysql://localhost:3306/view?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
driver-class-name: com.mysql.cj.jdbc.Driver
control:
username: root
password: password
url: jdbc:mysql://localhost:3306/control?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
driver-class-name: com.mysql.cj.jdbc.Driver
3.我这里使用的是tkMapper,其他的需要根据自己的框架修改。新建配置类DataSourceViewConfig 和 DataSourceControlConfig。代码如下
(注解在DataSourceControlConfig写的很清楚,DataSourceViewConfig就没写)
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.exc.mapper.view", sqlSessionFactoryRef = "sqlSessionFactoryView")
public class DataSourceViewConfig {
@Bean(name = "dataSourceView")
@ConfigurationProperties(prefix = "spring.datasource.druid.view")
public DataSource dataSourceView() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactoryView")
public SqlSessionFactory sqlSessionFactoryView(@Qualifier("dataSourceView") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/view/*.xml"));
sessionFactory.setTypeAliasesPackage("com.exc.entity.view");
sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sessionFactory.getObject();
}
@Bean(name = "sqlSessionTemplateView")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryView") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "transactionManagerView")
public PlatformTransactionManager transactionManagerView(@Qualifier("dataSourceView") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 org.springframework.transaction.PlatformTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
// 使用Spring的@Configuration注解声明这是一个配置类
// @MapperScan注解用于自动扫描并注册Mapper接口,tk.mybatis.spring.annotation.MapperScan是tk-mybatis提供的注解
// 注意:必须使用tk-mybatis的@MapperScan,而不是mybatis的基本包下的@MapperScan
// 至于原因,可以自己试试哦 哈哈
@MapperScan(basePackages = "com.exc.mapper.control", sqlSessionFactoryRef = "sqlSessionFactoryControl")
public class DataSourceControlConfig {
@Bean(name = "dataSourceControl")
// 定义一个名为"dataSourceControl"的Bean,负责创建数据源
// 使用@ConfigurationProperties注解绑定属性前缀,从application.properties或application.yml读取配置
@ConfigurationProperties(prefix = "spring.datasource.druid.control")
public DataSource dataSourceControl() {
// 使用DruidDataSourceBuilder创建并返回Druid数据源实例
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactoryControl")
// 定义一个名为"sqlSessionFactoryControl"的Bean,负责创建SqlSessionFactory
public SqlSessionFactory sqlSessionFactoryControl(@Qualifier("dataSourceControl") DataSource dataSource) throws Exception {
// 创建SqlSessionFactoryBean实例
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
// 设置数据源
sessionFactory.setDataSource(dataSource);
// 设置Mapper XML文件的位置,使用PathMatchingResourcePatternResolver解析资源模式
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/control/*.xml"));
// 设置实体类所在的包,MyBatis会从这个包中自动扫描实体类
sessionFactory.setTypeAliasesPackage("com.exc.entity.control");
// 获取SqlSessionFactory对象并设置配置,启用下划线到驼峰命名的自动转换
sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
// 返回创建好的SqlSessionFactory实例
return sessionFactory.getObject();
}
@Bean(name = "sqlSessionTemplateControl")
// 定义一个名为"sqlSessionTemplateControl"的Bean,负责创建SqlSessionTemplate
// SqlSessionTemplate提供了线程安全的执行SQL操作的方式
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryControl") SqlSessionFactory sqlSessionFactory) {
// 创建并返回SqlSessionTemplate实例
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "transactionManagerControl")
// 定义一个名为"transactionManagerControl"的Bean,负责创建PlatformTransactionManager
// PlatformTransactionManager用于管理事务
public PlatformTransactionManager transactionManagerView(@Qualifier("dataSourceControl") DataSource dataSource) {
// 创建并返回DataSourceTransactionManager实例,用于基于数据源的事务管理
return new DataSourceTransactionManager(dataSource);
}
}
使用的话,就直接使用mapper,在不同的包下的mapper会使用不同的数据源,比如在view包下的mapper,连接的就是view数据库。control同理