第一数据源
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(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"));
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")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 配置session工厂
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "secondarySqlSessionFactory")
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")
public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* 模板
* @param sqlSessionFactory
* @return
* @throws Exception
*/
@Bean(name = "secondarySqlSessionTemplate")
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
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
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
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j2
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);
}
}