GeezerVison is showing github.com — spring-data-jpa 多数据源 · feihugis/SpringBoot-Learning@ef18234
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Chapter3-2-4/src/main/java/com/didispace/PrimaryConfig.java +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | @EnableJpaRepositories( | 23 | 23 | entityManagerFactoryRef="entityManagerFactoryPrimary", | 24 | 24 | transactionManagerRef="transactionManagerPrimary", | 25 | | - basePackages= { "com.didispace.domain.one" }) | | 25 | + basePackages= { "com.didispace.domain.p" }) //设置Repository所在位置 | 26 | 26 | public class PrimaryConfig { | 27 | 27 |
| 28 | | - @Autowired | 29 | | - private JpaProperties jpaProperties; | 30 | | - | 31 | 28 | @Autowired @Qualifier("primaryDataSource") | 32 | 29 | private DataSource primaryDataSource; | 33 | 30 |
| @@ -43,11 +40,14 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (Entit | 43 | 40 | return builder | 44 | 41 | .dataSource(primaryDataSource) | 45 | 42 | .properties(getVendorProperties(primaryDataSource)) | 46 | | - .packages("com.didispace.domain.one") //设置实体类所在位置 | | 43 | + .packages("com.didispace.domain.p") //设置实体类所在位置 | 47 | 44 | .persistenceUnit("primaryPersistenceUnit") | 48 | 45 | .build(); | 49 | 46 | } | 50 | 47 |
| | 48 | + @Autowired | | 49 | + private JpaProperties jpaProperties; | | 50 | + | 51 | 51 | private Map<String, String> getVendorProperties(DataSource dataSource) { | 52 | 52 | return jpaProperties.getHibernateProperties(dataSource); | 53 | 53 | } | |
Collapse file Copy file name to clipboardExpand all lines: Chapter3-2-4/src/main/java/com/didispace/SecondaryConfig.java +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | @EnableJpaRepositories( | 22 | 22 | entityManagerFactoryRef="entityManagerFactorySecondary", | 23 | 23 | transactionManagerRef="transactionManagerSecondary", | 24 | | - basePackages= { "com.didispace.domain.two" }) | | 24 | + basePackages= { "com.didispace.domain.s" }) //设置Repository所在位置 | 25 | 25 | public class SecondaryConfig { | 26 | 26 |
| 27 | | - @Autowired | 28 | | - private JpaProperties jpaProperties; | 29 | | - | 30 | 27 | @Autowired @Qualifier("secondaryDataSource") | 31 | 28 | private DataSource secondaryDataSource; | 32 | 29 |
| @@ -40,11 +37,14 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (Ent | 40 | 37 | return builder | 41 | 38 | .dataSource(secondaryDataSource) | 42 | 39 | .properties(getVendorProperties(secondaryDataSource)) | 43 | | - .packages("com.didispace.domain.two") | | 40 | + .packages("com.didispace.domain.s") //设置实体类所在位置 | 44 | 41 | .persistenceUnit("secondaryPersistenceUnit") | 45 | 42 | .build(); | 46 | 43 | } | 47 | 44 |
| | 45 | + @Autowired | | 46 | + private JpaProperties jpaProperties; | | 47 | + | 48 | 48 | private Map<String, String> getVendorProperties(DataSource dataSource) { | 49 | 49 | return jpaProperties.getHibernateProperties(dataSource); | 50 | 50 | } | |
Collapse file Copy file name to clipboardExpand all lines: Chapter3-2-4/src/main/java/com/didispace/domain/p/User.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -package com.didispace.domain; | | 1 | +package com.didispace.domain.p; | 2 | 2 |
| 3 | 3 | import javax.persistence.Column; | 4 | 4 | import javax.persistence.Entity; | |
Collapse file Copy file name to clipboard +1-8Lines changed: 1 addition & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -package com.didispace.domain; | | 1 | +package com.didispace.domain.p; | 2 | 2 |
| 3 | 3 | import org.springframework.data.jpa.repository.JpaRepository; | 4 | 4 | import org.springframework.data.jpa.repository.Query; | | 13 | 13 | */ | 14 | 14 | public interface UserRepository extends JpaRepository<User, Long> { | 15 | 15 |
| 16 | | - User findByName(String name); | 17 | | - | 18 | | - User findByNameAndAge(String name, Integer age); | 19 | | - | 20 | | - @Query("from User u where u.name=:name") | 21 | | - User findUser(@Param("name") String name); | 22 | | - | 23 | 16 |
| 24 | 17 | } |
Collapse file Copy file name to clipboard +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.didispace.domain.s; | | 2 | + | | 3 | +import javax.persistence.Column; | | 4 | +import javax.persistence.Entity; | | 5 | +import javax.persistence.GeneratedValue; | | 6 | +import javax.persistence.Id; | | 7 | + | | 8 | +/** | | 9 | + * @author 程序猿DD | | 10 | + * @version 1.0.0 | | 11 | + * @date 16/3/21 下午3:35. | | 12 | + * @blog http://blog.didispace.com | | 13 | + */ | | 14 | +@Entity | | 15 | +public class Message { | | 16 | + | | 17 | + @Id | | 18 | + @GeneratedValue | | 19 | + private Long id; | | 20 | + | | 21 | + @Column(nullable = false) | | 22 | + private String name; | | 23 | + | | 24 | + @Column(nullable = false) | | 25 | + private String content; | | 26 | + | | 27 | + public Message(){} | | 28 | + | | 29 | + public Message(String name, String content) { | | 30 | + this.name = name; | | 31 | + this.content = content; | | 32 | + } | | 33 | + | | 34 | + public Long getId() { | | 35 | + return id; | | 36 | + } | | 37 | + | | 38 | + public void setId(Long id) { | | 39 | + this.id = id; | | 40 | + } | | 41 | + | | 42 | + public String getName() { | | 43 | + return name; | | 44 | + } | | 45 | + | | 46 | + public void setName(String name) { | | 47 | + this.name = name; | | 48 | + } | | 49 | + | | 50 | + public String getContent() { | | 51 | + return content; | | 52 | + } | | 53 | + | | 54 | + public void setContent(String content) { | | 55 | + this.content = content; | | 56 | + } | | 57 | + | | 58 | +} |
Collapse file Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.didispace.domain.s; | | 2 | + | | 3 | +import org.springframework.data.jpa.repository.JpaRepository; | | 4 | + | | 5 | + | | 6 | +/** | | 7 | + * @author 程序猿DD | | 8 | + * @version 1.0.0 | | 9 | + * @date 16/3/23 下午2:34. | | 10 | + * @blog http://blog.didispace.com | | 11 | + */ | | 12 | +public interface MessageRepository extends JpaRepository<Message, Long> { | | 13 | + | | 14 | + | | 15 | +} |
Collapse file Copy file name to clipboard +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1 | 2 | 2 | spring.datasource.primary.username=root | 3 | | -spring.datasource.primary.password=root | | 3 | +spring.datasource.primary.password=52261340 | 4 | 4 | spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver | 5 | 5 |
| 6 | 6 | spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2 | 7 | 7 | spring.datasource.secondary.username=root | 8 | | -spring.datasource.secondary.password=root | | 8 | +spring.datasource.secondary.password=52261340 | 9 | 9 | spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver | | 10 | + | | 11 | +spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop |
Collapse file Copy file name to clipboardExpand all lines: Chapter3-2-4/src/test/java/com/didispace/ApplicationTests.java +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package com.didispace; | 2 | 2 |
| | 3 | +import com.didispace.domain.p.User; | | 4 | +import com.didispace.domain.p.UserRepository; | | 5 | +import com.didispace.domain.s.Message; | | 6 | +import com.didispace.domain.s.MessageRepository; | | 7 | +import org.junit.Assert; | 3 | 8 | import org.junit.Before; | 4 | 9 | import org.junit.Test; | 5 | 10 | import org.junit.runner.RunWith; | | 11 | +import org.springframework.beans.factory.annotation.Autowired; | 6 | 12 | import org.springframework.boot.test.SpringApplicationConfiguration; | 7 | 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | 8 | 14 |
| | 11 | 17 | @SpringApplicationConfiguration(Application.class) | 12 | 18 | public class ApplicationTests { | 13 | 19 |
| | 20 | + @Autowired | | 21 | + private UserRepository userRepository; | | 22 | + @Autowired | | 23 | + private MessageRepository messageRepository; | 14 | 24 |
| 15 | 25 | @Before | 16 | 26 | public void setUp() { | @@ -19,6 +29,19 @@ public void setUp() { | 19 | 29 | @Test | 20 | 30 | public void test() throws Exception { | 21 | 31 |
| | 32 | + userRepository.save(new User("aaa", 10)); | | 33 | + userRepository.save(new User("bbb", 20)); | | 34 | + userRepository.save(new User("ccc", 30)); | | 35 | + userRepository.save(new User("ddd", 40)); | | 36 | + userRepository.save(new User("eee", 50)); | | 37 | + | | 38 | + Assert.assertEquals(5, userRepository.findAll().size()); | | 39 | + | | 40 | + messageRepository.save(new Message("o1", "aaaaaaaaaa")); | | 41 | + messageRepository.save(new Message("o2", "bbbbbbbbbb")); | | 42 | + messageRepository.save(new Message("o3", "cccccccccc")); | | 43 | + | | 44 | + Assert.assertEquals(3, messageRepository.findAll().size()); | 22 | 45 |
| 23 | 46 | } | 24 | 47 |
| |
|