lishinka

springboot使用druid整合mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
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 javax.sql.DataSource;
import java.sql.SQLException;

/**
* Spring Boot中默认支持的连接池有dbcp,dbcp2,tomcat,hikari三种连接池。
* 由于Druid暂时不在Spring Bootz中的直接支持,需要配置
*/
@Configuration
@Slf4j
public class DruidConfiguration {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
private String filters;
@Value("${spring.datasource.connectionProperties}")
private String connectionProperties;

@Bean
@Primary
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource(); //NOSONAR
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);

//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
log.error(e.getMessage(),e);
// 线程忽略异常,继续执行。
Thread.currentThread().interrupt();
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}

springboot 双数据源配置

1.application.configproperties 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
##数据源1-ldrcims
spring.datasource.ldrcims.driverClassName=com.mysql.jdbc.Driver
spring.datasource.ldrcims.url=jdbc:mysql://localhost:3306/ldrcims?ch1aracterEncoding=UTF-8&useSSL=false
spring.datasource.ldrcims.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.ldrcims.username=root
spring.datasource.ldrcims.password=123456

##数据源2-user
spring.datasource.user.driverClassName=com.mysql.jdbc.Driver
spring.datasource.user.url=jdbc:mysql://localhost:3306/rmis_new?ch1aracterEncoding=UTF-8&useSSL=false
spring.datasource.user.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.user.username=root
spring.datasource.user.password=123456

2.DataSourceConfigurer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Bean(name="userDataSource")
@Qualifier("userDataSource")
@ConfigurationProperties(prefix = "spring.datasource.user")
public DataSource userDataSource(){
return DataSourceBuilder.create().build();
}

@Bean(name="ldrcimsDataSource")
@Primary
@Qualifier("ldrcimsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.ldrcims")
public DataSource ldrcimsDataSource(){
return DataSourceBuilder.create().build();
}

3.LdrcimsConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "ldrcimsEntityManagerFactory",
transactionManagerRef = "ldrcimsTransactionManager",
basePackages = { "com.southgis.ldrcims.repository.ldrcims" }) // 设置Repository所在位置
public class LdrcimsConfig {

@Autowired
private JpaProperties jpaProperties;

@Autowired
@Qualifier("ldrcimsDataSource")
private DataSource ldrcimsDataSource;

@Bean(name = "ldrcimsEntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean ldrcimsEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(ldrcimsDataSource)
.properties(getVendorProperties(ldrcimsDataSource))
.packages("com.southgis.ldrcims.entity.ldrcims") //设置实体类所在位置
.persistenceUnit("ldrcimsPersistenceUnit")
.build();
//.getObject();//不要在这里直接获取EntityManagerFactory
}

private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}

@Bean(name = "ldrcimsEntityManagerFactory")
@Primary
public EntityManagerFactory ldrcimsEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return this.ldrcimsEntityManagerFactoryBean(builder).getObject();
}

@Bean(name = "ldrcimsTransactionManager")
@Primary
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(ldrcimsEntityManagerFactory(builder));
}
}

4. UserConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "userEntityManagerFactory",
transactionManagerRef = "userTransactionManager",
basePackages = { "com.southgis.ldrcims.repository.user" }) // 设置Repository所在位置
public class UserConfig {

@Autowired
private JpaProperties jpaProperties;

@Autowired
@Qualifier("userDataSource")
private DataSource userDataSource;

@Bean(name = "userEntityManagerFactoryBean")
public LocalContainerEntityManagerFactoryBean userEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(userDataSource)
.properties(getVendorProperties(userDataSource))
.packages("com.southgis.ldrcims.entity.user") //设置实体类所在位置
.persistenceUnit("userPersistenceUnit")
.build();
//.getObject();//不要在这里直接获取EntityManagerFactory
}

private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}

@Bean(name = "userEntityManagerFactory")
public EntityManagerFactory userEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return this.userEntityManagerFactoryBean(builder).getObject();
}
@Bean(name = "userTransactionManager")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(userEntityManagerFactory(builder));
}
}