点击(此处)折叠或打开
-
-
1. 配置 pom
-
-
<!-- druid 数据库连接池 -->
-
<dependency>
-
<groupId>com.alibaba</groupId>
-
<artifactId>druid</artifactId>
-
<version>1.1.12</version>
-
<exclusions>
-
<exclusion>
-
<groupId>com.alibaba</groupId>
-
<artifactId>jconsole</artifactId>
-
</exclusion>
-
<exclusion>
-
<groupId>com.alibaba</groupId>
-
<artifactId>tools</artifactId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
-
2. 配置 application.properties
-
-
#druid连接池
-
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
-
#最大活跃数
-
spring.datasource.maxActive: 20
-
#初始化数量
-
spring.datasource.initialSize: 1
-
#最大连接等待超时时间
-
spring.datasource.maxWait: 60000
-
#打开PSCache,并且指定每个连接PSCache的大小
-
spring.datasource.poolPreparedStatements: true
-
spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
-
#通过connectionProperties属性来打开mergeSql功能;慢SQL记录
-
#connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
-
spring.datasource.minIdle: 1
-
spring.datasource.timeBetweenEvictionRunsMillis: 60000
-
spring.datasource.minEvictableIdleTimeMillis: 300000
-
spring.datasource.validationQuery: select 1 from dual
-
spring.datasource.testWhileIdle: true
-
spring.datasource.testOnBorrow: false
-
spring.datasource.testOnReturn: false
-
#配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
-
filters: stat, wall, log4j
-
-
3.Config 配置类
-
-
-
-
/**
-
* 说明:第一数据源配置
-
* 作者:FH
-
* from:fhadmin.cn
-
*/
-
@Configuration
-
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") //扫描 Mapper 接口并容器管理
-
public class MasterDataSourceConfig {
-
-
static final String PACKAGE = "org.fh.mapper.dsno1"; //master 目录
-
static final String MAPPER_LOCATION = "classpath:mybatis/dsno1/*/*.xml"; //扫描的 xml 目录
-
static final String CONFIG_LOCATION = "classpath:mybatis/dsno1/mybatis-config.xml"; //自定义的mybatis config 文件位置
-
static final String TYPE_ALIASES_PACKAGE = "org.fh.entity"; //扫描的 实体类 目录
-
-
@Value("${datasource.no1.url}")
-
private String url;
-
-
@Value("${datasource.no1.username}")
-
private String user;
-
-
@Value("${datasource.no1.password}")
-
private String password;
-
-
@Value("${datasource.no1.driver-class-name}")
-
private String driverClass;
-
-
@Bean(name = "masterDataSource")
-
@Primary
-
public DataSource masterDataSource() {
-
DruidDataSource dataSource = new DruidDataSource();
-
dataSource.setDriverClassName(driverClass);
-
dataSource.setUrl(url);
-
dataSource.setUsername(user);
-
dataSource.setPassword(password);
-
return dataSource;
-
}
-
-
@Bean(name = "masterTransactionManager")
-
@Primary
-
public DataSourceTransactionManager masterTransactionManager() {
-
return new DataSourceTransactionManager(masterDataSource());
-
}
-
-
@Bean(name = "masterSqlSessionFactory")
-
@Primary
-
public 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.setConfigLocation(new DefaultResourceLoader().getResource(MasterDataSourceConfig.CONFIG_LOCATION));
-
sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE);
-
return sessionFactory.getObject();
-
}
-
}
-
-