目录
  1. 1. 项目搭建
  2. 2. 项目结构
  3. 3. 数据源配置
  4. 4. 项目地址
springboot JPA多数据源

项目搭建

  • SpringBoot 2.1.6.RELEASE
  • SpringBoot JPA 2.1.6.RELEASE
  • MySQL 5.5.62
  • SpringBoot Druid 1.1.18

项目结构

sj1.png

数据源配置

  • 配置文件
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
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8

jpa:
open-in-view: false
show-sql: true
generate-ddl: true
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect

datasource:
primary:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/primary?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
username: root
password: 123456
# 初始化连接大小
initial-size: 5
# 最小空闲连接数
min-idle: 5
max-active: 30
max-wait: 60000
# 可关闭的空闲连接间隔时间
time-between-eviction-runs-millis: 60000
# 配置连接在池中的最小生存时间
min-evictable-idle-time-millis: 300000
validation-query: select '1' from dual
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: false

# 配置监控统计拦截的filters
filters: stat,wall
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
use-global-data-source-stat: true
filter:
wall:
enabled: true
config:
strict-syntax-check: false

slave:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/slave?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
username: root
password: 123456
# 初始化连接大小
initial-size: 5
# 最小空闲连接数
min-idle: 5
max-active: 30
max-wait: 60000
# 可关闭的空闲连接间隔时间
time-between-eviction-runs-millis: 60000
# 配置连接在池中的最小生存时间
min-evictable-idle-time-millis: 300000
validation-query: select '1' from dual
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: false

# 配置监控统计拦截的filters
filters: stat,wall
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
use-global-data-source-stat: true
filter:
wall:
enabled: true
config:
strict-syntax-check: false
  • 主数据源配类
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
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.chensubao.repo.primary"},//这里要指向你的repository路径
entityManagerFactoryRef = "primaryEMF",
transactionManagerRef = "primaryTM")
public class PrimaryDataSourceConfig {

@Primary
@Bean("primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return new DruidDataSource();
}

@Primary
@Bean(name = "primaryEntityManager")
public EntityManager primaryEntityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}

@Primary
@Bean(name = "primaryEMF")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource())
//这个是指向你的domain, entity的路径
.packages("com.chensubao.entity.primary")
.persistenceUnit("primary")
.build();
}

@Primary
@Bean(name = "primaryTM")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
  • 从数据源配置类
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
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.chensubao.repo.slave"},//这里要指向你的repository路径
entityManagerFactoryRef = "slaveEMF",
transactionManagerRef = "slaveTM")
public class SlaveDataSourceConfig {

@Bean("slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return new DruidDataSource();
}

@Bean(name = "slaveEntityManager")
public EntityManager slaveEntityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}

@Bean(name = "slaveEMF")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(slaveDataSource())
//这个是指向你的domain, entity的路径
.packages("com.chensubao.entity.slave")
.persistenceUnit("slave")
.build();
}

@Bean(name = "slaveTM")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
  • 注入EntityManager
1
2
@PersistenceContext(unitName = "primary")
private EntityManager entityManager;
  • 使用事务注解
1
@Transactional(rollbackFor = Exception.class, transactionManager = "primaryTM")

项目地址

项目地址

文章作者: 今晚早睡
文章链接: https://www.chensubao.com/article/965dc7d3.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 洛 苏り

评论