依赖:
<!-- add by john com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.19</version> </dependency>
DruidConfig:
package com.icil.tracking.milestone.service.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.sql.SQLException; import java.util.Properties; @PropertySource(value = "classpath:application.properties",ignoreResourceNotFound = true) @Component public class DruidConfig { private Logger log = LoggerFactory.getLogger(DruidConfig.class); @Value("${spring.druid.initialSize}") private Integer initialSize; @Value("${spring.druid.minIdle}") private Integer minIdle; @Value("${spring.druid.maxActive}") private Integer maxActive; @Value("${spring.druid.maxPoolPreparedStatementPerConnectionSize}") private Integer maxPoolPreparedStatementPerConnectionSize; @Value("${spring.druid.maxWait}") private Long maxWait; @Value("${spring.druid.timeBetweenEvictionRunsMillis}") private Long timeBetweenEvictionRunsMillis; @Value("${spring.druid.minEvictableIdleTimeMillis}") private Long minEvictableIdleTimeMillis; @Value("${spring.druid.filters}") private String filters; @Value("${spring.druid.connectionProperties}") private Properties connectionProperties; @Value("${spring.druid.testWhileIdle}") private boolean testWhileIdle; @Value("${spring.druid.testOnBorrow}") private boolean testOnBorrow; @Value("${spring.druid.testOnReturn}") private boolean testOnReturn; @Value("${spring.druid.poolPreparedStatements}") private boolean poolPreparedStatements; public DruidDataSource druidDataSource(){ log.info("============= loading druid config ===================="); DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setInitialSize(initialSize); druidDataSource.setMinIdle(minIdle); druidDataSource.setMaxActive(maxActive); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); druidDataSource.setMaxWait(maxWait); druidDataSource.setTimeBetweenConnectErrorMillis(timeBetweenEvictionRunsMillis); druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); druidDataSource.setConnectProperties(connectionProperties); druidDataSource.setTestWhileIdle(testWhileIdle); druidDataSource.setTestOnBorrow(testOnBorrow); druidDataSource.setTestOnReturn(testOnReturn); druidDataSource.setPoolPreparedStatements(poolPreparedStatements); try { druidDataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } return druidDataSource; } @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); // registrationBean.addInitParameter("allow","127.0.0.1"); // 设置白名单 // registrationBean.addInitParameter("deny","127.0.0.1"); // 设置黑名单,优先级高于白名单 //设置控制台管理用户 registrationBean.addInitParameter("loginUsername","root"); registrationBean.addInitParameter("loginPassword","root"); //是否可以重置数据 registrationBean.addInitParameter("resetEnable","false"); return registrationBean; } @Bean public FilterRegistrationBean statFilter(){ //创建过滤器 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //设置过滤器过滤路径 filterRegistrationBean.addUrlPatterns("/*"); //忽略过滤的形式 filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
DBGlobalConfig (Global 表)
@Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "globalEntityManagerFactory", transactionManagerRef = "globalTransactionManager", basePackages = {"com.sea.milestone.service.repo.dao.global"}) public class DBGlobalConfig { @Bean("globalDataSourceProperties") @ConfigurationProperties("datasource.global") public DataSourceProperties dataSourceProperties() { return new DataSourceProperties(); } // @Bean("globalDataSource") // public DataSource dataSource(@Qualifier("globalDataSourceProperties") DataSourceProperties dataSourceProperties) { // return dataSourceProperties.initializeDataSourceBuilder().build(); // } @Autowired private DruidConfig druidConfig; @Bean("globalDataSource") @ConfigurationProperties("datasource.global") public DataSource dataSource(){ return druidConfig.druidDataSource(); } @Bean("globalEntityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier("globalDataSource") DataSource dataSource) { return builder .dataSource(dataSource) .packages("com.sea.milestone.service.repo.po.global") .persistenceUnit("global") .build(); } @Bean(name = "globalTransactionManager") public PlatformTransactionManager transactionManager( @Qualifier("globalEntityManagerFactory") EntityManagerFactory entityManagerFactory) { JpaTransactionManager manager = new JpaTransactionManager(entityManagerFactory); manager.setGlobalRollbackOnParticipationFailure(false); return manager; } }
application.properties
datasource.global.url = jdbc:mysql://192.168.18.129/Global?autoReconnect=true&useSSL=false datasource.global.username = root datasource.global.password = root datasource.global.driver-class-name = com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.generate_statistics = true
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql = true ##common druid spring.druid.initialSize = 5 spring.druid.minIdle = 5 spring.druid.maxActive = 20 spring.druid.maxWait = 60000 spring.druid.timeBetweenEvictionRunsMillis = 60000 spring.druid.minEvictableIdleTimeMillis = 300000 #spring.druid.validationQuery=SELECT 1 FROM DUAL #测试连接 spring.druid.testWhileIdle = true spring.druid.testOnBorrow = false spring.druid.testOnReturn = false spring.druid.poolPreparedStatements = false spring.druid.maxPoolPreparedStatementPerConnectionSize = 20 spring.druid.filters = stat,wall,log4j spring.druid.connectionProperties = 'druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000'