阅读背景:

springboot配置双数据源

来源:互联网 
第一数据源 package com.test.demo.config; 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.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; 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; /** * Created by kaixuan on 2018/5/4. */ @Configuration //MapperScan指定dao @MapperScan(basePackages = "com.test.demo.dao.primary", sqlSessionFactoryRef = "primarySqlSessionFactory") public class PrimaryDatasourceConfig { /** * 主数据库 连接mysql * @return */ @Bean(name = "primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.druid.primary") @Primary public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } /** * 配置session工厂 * @param dataSource * @return * @throws Exception */ @Bean(name = "primarySqlSessionFactory") @Primary public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/primary/**/*Mapper.xml"));//指定mapper return bean.getObject(); } /** * 配置事物管理器 * @param dataSource * @return */ @Bean(name = "primaryTransactionManager") @Primary public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * 模板 * @param sqlSessionFactory * @return * @throws Exception */ @Bean(name = "primarySqlSessionTemplate") @Primary public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } 第二数据源 package com.test.demo.config; 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.boot.autoconfigure.jdbc.DataSourceBuilder; 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 javax.sql.DataSource; /** * Created by kaixuan on 2018/5/4. */ @Configuration @MapperScan(basePackages = "com.test.demo.dao.secondary", sqlSessionFactoryRef = "secondarySqlSessionFactory") public class SecondaryDatasourceConfig { /** * 第二数据库源 用于连接sqlserver * @return */ @Bean(name = "secondaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.druid.secondary") // @Primary public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } /** * 配置session工厂 * @param dataSource * @return * @throws Exception */ @Bean(name = "secondarySqlSessionFactory") // @Primary public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/secondary/**/*Mapper.xml")); return bean.getObject(); } /** * 配置事物管理器 * @param dataSource * @return */ @Bean(name = "secondaryTransactionManager") // @Primary public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * 模板 * @param sqlSessionFactory * @return * @throws Exception */ @Bean(name = "secondarySqlSessionTemplate") // @Primary public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } 配置文件 #数据库,暂时没有集成连接池 spring: datasource: #整合连接池 type: com.alibaba.druid.pool.DruidDataSource druid: primary: url: jdbc:mysql://localhost:3306/mes?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF8&useSSL=false # url: jdbc:mysql:///mes?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF8&useSSL=false username: test password: test driver-class-name: com.mysql.jdbc.Driver minIdle: 1 maxIdle: 20 maxActive: 100 initialSize: 10 timeBetweenEvictionRunsMillis: 3000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 'ZTM' FROM DUAL validationQueryTimeout: 10000 testWhileIdle: true testOnBorrow: false testOnReturn: false maxWait: 60000 # 打开PSCache,并且指定每个连接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall,log4j2 secondary: url: jdbc:sqlserver://localhost:1433; DatabaseName=EDO username: test password: test driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver minIdle: 1 maxIdle: 20 maxActive: 100 initialSize: 10 timeBetweenEvictionRunsMillis: 3000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 'ZTM' FROM DUAL validationQueryTimeout: 10000 testWhileIdle: true testOnBorrow: false testOnReturn: false maxWait: 60000 # 打开PSCache,并且指定每个连接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall,log4j2 # redis: # database: 0 # password: # pool: # max-active: 200 # max-wait: -1 # max-idle: 20 # min-idle: 0 # timeout: 0 # activiti: # check-process-definitions: false #设置日志级别,打印sql #logging: # level: # com: # example: # demo: DEBUG # pattern: # console: '%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n' # file: '%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n' #更改端口 server: port: 8089 path: uploadPath: D:\excel\ downloadPath: D:\excel\ importloadPath: D:\excel\ exportloadPath: D:\excel\ 启动类 @SpringBootApplication @EnableCaching @EnableConfigurationProperties @ServletComponentScan @ComponentScan(basePackages = "com.test.demo") public class MesApplication { public static void main(String[] args) { SpringApplication.run(MesApplication.class, args); } } 第一数据源 package com.test.demo.config; import org.ap



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: