Skip to content

Commit 5615863

Browse files
committed
Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置
1 parent 6d06a02 commit 5615863

File tree

11 files changed

+74
-82
lines changed

11 files changed

+74
-82
lines changed

2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/Chapter38Application.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,4 @@ public static void main(String[] args) {
1212
SpringApplication.run(Chapter38Application.class, args);
1313
}
1414

15-
@RestController
16-
static class TextController {
17-
18-
@GetMapping("/hello")
19-
public String hello() {
20-
return "hello world";
21-
}
22-
23-
}
24-
2515
}

2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/DataSourceConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.didispace.chapter37;
1+
package com.didispace.chapter38;
22

33
import org.springframework.beans.factory.annotation.Qualifier;
44
import org.springframework.boot.context.properties.ConfigurationProperties;

2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/PrimaryConfig.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
6+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
57
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
68
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
79
import org.springframework.context.annotation.Bean;
@@ -16,6 +18,7 @@
1618

1719
import javax.persistence.EntityManager;
1820
import javax.sql.DataSource;
21+
import java.util.Map;
1922

2023
@Configuration
2124
@EnableTransactionManagement
@@ -29,6 +32,15 @@ public class PrimaryConfig {
2932
@Qualifier("primaryDataSource")
3033
private DataSource primaryDataSource;
3134

35+
@Autowired
36+
private JpaProperties jpaProperties;
37+
@Autowired
38+
private HibernateProperties hibernateProperties;
39+
40+
private Map<String, Object> getVendorProperties() {
41+
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
42+
}
43+
3244
@Primary
3345
@Bean(name = "entityManagerPrimary")
3446
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
@@ -38,12 +50,13 @@ public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
3850
@Primary
3951
@Bean(name = "entityManagerFactoryPrimary")
4052
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
41-
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
42-
jpaVendorAdapter.setGenerateDdl(true);
53+
// HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
54+
// jpaVendorAdapter.setGenerateDdl(true);
4355
return builder
4456
.dataSource(primaryDataSource)
45-
.packages("com.didispace.domain.p") //设置实体类所在位置
57+
.packages("com.didispace.chapter38.p") //设置实体类所在位置
4658
.persistenceUnit("primaryPersistenceUnit")
59+
.properties(getVendorProperties())
4760
.build();
4861
}
4962

@@ -53,4 +66,5 @@ public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactory
5366
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
5467
}
5568

69+
5670
}

2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/SecondaryConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
6+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
57
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
68
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
79
import org.springframework.context.annotation.Bean;
@@ -28,6 +30,15 @@ public class SecondaryConfig {
2830
@Qualifier("secondaryDataSource")
2931
private DataSource secondaryDataSource;
3032

33+
@Autowired
34+
private JpaProperties jpaProperties;
35+
@Autowired
36+
private HibernateProperties hibernateProperties;
37+
38+
private Map<String, Object> getVendorProperties() {
39+
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
40+
}
41+
3142
@Bean(name = "entityManagerSecondary")
3243
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
3344
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
@@ -39,6 +50,7 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (Ent
3950
.dataSource(secondaryDataSource)
4051
.packages("com.didispace.chapter38.s") //设置实体类所在位置
4152
.persistenceUnit("secondaryPersistenceUnit")
53+
.properties(getVendorProperties())
4254
.build();
4355
}
4456

2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/p/User.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import javax.persistence.Entity;
77
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
89
import javax.persistence.Id;
910

1011
@Entity

2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/p/UserRepository.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,11 @@
55
import org.springframework.data.repository.query.Param;
66

77
/**
8-
* Created by 程序猿DD/翟永超 on 2020/2/15.
8+
* Created by 程序猿DD/翟永超 on 2020/6/22.
99
* <p>
1010
* Blog: http://blog.didispace.com/
1111
* Github: https://github.com/dyc87112/
1212
*/
1313
public interface UserRepository extends JpaRepository<User, Long> {
1414

15-
User findByName(String name);
16-
17-
User findByNameAndAge(String name, Integer age);
18-
19-
@Query("from User u where u.name=:name")
20-
User findUser(@Param("name") String name);
21-
2215
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
package com.didispace.chapter38.s;
22

3+
import lombok.AllArgsConstructor;
34
import lombok.Data;
45
import lombok.NoArgsConstructor;
56

67
import javax.persistence.Entity;
78
import javax.persistence.GeneratedValue;
9+
import javax.persistence.GenerationType;
810
import javax.persistence.Id;
911

1012
@Entity
1113
@Data
1214
@NoArgsConstructor
13-
public class User {
15+
public class Message {
1416

1517
@Id
1618
@GeneratedValue
1719
private Long id;
1820

19-
private String name;
20-
private Integer age;
21+
private String title;
22+
private String message;
2123

22-
public User(String name, Integer age) {
23-
this.name = name;
24-
this.age = age;
24+
public Message(String title, String message) {
25+
this.title = title;
26+
this.message = message;
2527
}
28+
2629
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.didispace.chapter38.s;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
/**
6+
* Created by 程序猿DD/翟永超 on 2020/6/22.
7+
* <p>
8+
* Blog: http://blog.didispace.com/
9+
* Github: https://github.com/dyc87112/
10+
*/
11+
public interface MessageRepository extends JpaRepository<Message, Long> {
12+
13+
14+
}

2.1.x/chapter3-8/src/main/java/com/didispace/chapter38/s/UserRepository.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# pring boot 1.x的配置:spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
22
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test1
33
spring.datasource.primary.username=root
4-
spring.datasource.primary.password=123456
4+
spring.datasource.primary.password=12345678
55
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
66

77
# spring boot 1.x的配置:spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
88
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2
99
spring.datasource.secondary.username=root
10-
spring.datasource.secondary.password=123456
10+
spring.datasource.secondary.password=12345678
1111
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
1212

13-
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
13+
# 日志打印执行的SQL
1414
spring.jpa.show-sql=true
15-
spring.jpa.hibernate.ddl-auto=create-drop
15+
# Hibernate的DDL策略
16+
spring.jpa.hibernate.ddl-auto=create-drop

0 commit comments

Comments
 (0)