一、配置文件
jdk环境:1.8 配置了双数据源springboot+druid+pgsql,application.properties配置修改如下:
#当前入库主数据库
spring.primary.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.primary.datasource.driver-class-name=org.postgresql.Driver
spring.primary.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.primary.datasource.username=postgres
spring.primary.datasource.password=postgres
#
#从数据库
spring.secondary.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.secondary.datasource.driver-class-name=org.postgresql.Driver
spring.secondary.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.secondary.datasource.username=postgres
spring.secondary.datasource.password=postgresspring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
二、Java代码配置新增
主数据库注入
/*** @Author yang* @Date 2023/2/20 11:10* @Version 1.0*/
import com.alibaba.druid.pool.DruidDataSource;
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 = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {// 精确到master目录,以便跟其他数据源隔离static final String PACKAGE = "com.xx.dao.master";static final String MAPPER_LOCATION = "classpath*:com/xx/mapper/master/*.xml";@Value("${spring.primary.datasource.url}")private String url;@Value("${spring.primary.datasource.username}")private String user;@Value("${spring.primary.datasource.password}")private String password;@Value("${spring.primary.datasource.driver-class-name}")private String driverClass;private SqlSessionFactory mSessionFactory;@Bean(name = "masterDataSource")@Primarypublic DataSource masterDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driverClass);dataSource.setUrl(url);dataSource.setUsername(user);dataSource.setPassword(password);return dataSource;}@Bean(name = "masterTransactionManager")@Primarypublic DataSourceTransactionManager masterTransactionManager() {return new DataSourceTransactionManager(masterDataSource());}@Bean(name = "masterSqlSessionFactory")@Primarypublic SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(masterDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));//开启驼峰sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);this.mSessionFactory = sessionFactory.getObject();return sessionFactory.getObject();}/*** 获取主库 SessionFactory* @return*/public SqlSessionFactory getMSessionFactory(){return mSessionFactory;}}
从数据库Java代码:
/*** @Author yang* @Date 2023/2/20 11:52* @Version 1.0*/
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = SecondDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig {//精确到第二个数据库目录,以便跟其他数据源隔离static final String PACKAGE = "com.xx.dao.second";static final String MAPPER_LOCATION = "classpath*:com/xx/mapper/second/*.xml";@Value("${spring.secondary.datasource.url}")private String url;@Value("${spring.secondary.datasource.username}")private String user;@Value("${spring.secondary.datasource.password}")private String password;@Value("${spring.secondary.datasource.driver-class-name}")private String driverClass;@Bean(name = "secondDataSource")public DataSource clusterDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driverClass);dataSource.setUrl(url);dataSource.setUsername(user);dataSource.setPassword(password);return dataSource;}@Bean(name = "secondTransactionManager")public DataSourceTransactionManager clusterTransactionManager() {return new DataSourceTransactionManager(clusterDataSource());}@Bean(name = "secondSqlSessionFactory")public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("secondDataSource") DataSource clusterDataSource)throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(clusterDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(SecondDataSourceConfig.MAPPER_LOCATION));//开启驼峰sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);return sessionFactory.getObject();}
}
三、mapper接口、mybatis/xml文件配置
这里就就不一一贴代码了,主要是接口对应mybatis xml配置文件。项目文件接口如下:
创建成以上目录就可以了,分别是dao接口、Java数据源配置、mybatis映射文件。
四、怎么使用
通过以上配置文件和代码已经对两个数据源进行分割,直接正常使用访问代码即可,比如dao里面创建了testClass的操作
void insert(Test test); void delete();Controller层我们通过@Autowired注解 使用就可以了。
这里需要注意修改:
@MapperScan里面的basePackages
MAPPER_LOCATION变量的配置,这里主要是不同的数据源配置扫描不同的mybatis配置文件