一、先修改配置文件
spring:
datasource:
name: druidDataSource
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
#数据库1
db1:
url: jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true
username: root
password: root
#数据源2
db2:
# 数据源2开关、默认关闭
enable: false
url: jdbc:mysql://localhost:3306/nacos_config?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true
username: root
password: root
二、添加配置类(根据生效位置配置)
1. DB1Config.java(数据源1)
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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;
/**
* @Author liull
* @DATE 2022/6/21 13:22
**/
@Slf4j
@Configuration
@MapperScan(value = {"xxx.xxx.xxx.db1.dao"}, sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class DB1Config {
@Value("${spring.datasource.druid.db1.url}")
private String url;
@Value("${spring.datasource.druid.db1.username}")
private String username;
@Value("${spring.datasource.druid.db1.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Bean("db1DataSource")
@Primary
public DruidDataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
log.info("db1 init");
return datasource;
}
@Bean("db1SqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
return sessionFactory.getObject();
}
@Bean("db1TransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("db1SqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
2. DB2Config.java(数据源2)
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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;
/**
* @Author liull
* @DATE 2022/6/21 13:32
**/
@Slf4j
@Configuration
// 修改包扫描路径
@MapperScan(value = "xxx.xxx.xxx.db2.dao", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DB2Config {
@Value("${spring.datasource.druid.db2.url}")
private String url;
@Value("${spring.datasource.druid.db2.username}")
private String username;
@Value("${spring.datasource.druid.db2.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Bean("db2DataSource")
@Qualifier("db2DataSource")
public DruidDataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
log.info("db2 init");
return datasource;
}
@Bean("db2SqlSessionFactory")
@Qualifier("db2DataSource")
public SqlSessionFactory sqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*Mapper.xml"));
return sessionFactory.getObject();
}
@Bean("db2TransactionManager")
@Qualifier("db2DataSource")
public DataSourceTransactionManager transactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("db2SqlSessionTemplate")
@Qualifier("db2DataSource")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
三、注意事项
1. 注意包扫描路径,建议两种数据源持久层分包写(dao.dao1,dao.dao2)
2. 注意两个配置文件:
Ⅰ)定义不同的sqlSessionTemplateRef
Ⅱ)注意默认的数据源@Primary
3. 注意静态文件路径,建议两种数据源分包写(mapper.db1,mapper.db2)
四、特别提示
-
多数据源配置几乎相同,只是换了不同的名字,由连接一个数据库变为连接两个数据库,在配置类中设置对应的包扫描路径和静态文件路径。
-
某些技术贴分享需要先修改启动类上面的注解,比如多数据源配置起数据库名(如下图),需要将@SpringBootApplication()修改为@SpringBootApplication(exclude={DataSourceAutoConfiguration.calss}),并且去掉@MapperScan(),为了让系统关闭自动扫描,这个视情节而定,不修改目前没有遇到问题。
-
如果大家遇到什么问题,欢迎评论讨论,如有错误之处,敬请留言。
相关文章
暂无评论...