spring-data-jpa 多数据源
This commit is contained in:
@@ -22,12 +22,9 @@ import java.util.Map;
|
||||
@EnableJpaRepositories(
|
||||
entityManagerFactoryRef="entityManagerFactoryPrimary",
|
||||
transactionManagerRef="transactionManagerPrimary",
|
||||
basePackages= { "com.didispace.domain.one" })
|
||||
basePackages= { "com.didispace.domain.p" }) //设置Repository所在位置
|
||||
public class PrimaryConfig {
|
||||
|
||||
@Autowired
|
||||
private JpaProperties jpaProperties;
|
||||
|
||||
@Autowired @Qualifier("primaryDataSource")
|
||||
private DataSource primaryDataSource;
|
||||
|
||||
@@ -43,11 +40,14 @@ public class PrimaryConfig {
|
||||
return builder
|
||||
.dataSource(primaryDataSource)
|
||||
.properties(getVendorProperties(primaryDataSource))
|
||||
.packages("com.didispace.domain.one") //设置实体类所在位置
|
||||
.packages("com.didispace.domain.p") //设置实体类所在位置
|
||||
.persistenceUnit("primaryPersistenceUnit")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private JpaProperties jpaProperties;
|
||||
|
||||
private Map<String, String> getVendorProperties(DataSource dataSource) {
|
||||
return jpaProperties.getHibernateProperties(dataSource);
|
||||
}
|
||||
|
||||
@@ -21,12 +21,9 @@ import java.util.Map;
|
||||
@EnableJpaRepositories(
|
||||
entityManagerFactoryRef="entityManagerFactorySecondary",
|
||||
transactionManagerRef="transactionManagerSecondary",
|
||||
basePackages= { "com.didispace.domain.two" })
|
||||
basePackages= { "com.didispace.domain.s" }) //设置Repository所在位置
|
||||
public class SecondaryConfig {
|
||||
|
||||
@Autowired
|
||||
private JpaProperties jpaProperties;
|
||||
|
||||
@Autowired @Qualifier("secondaryDataSource")
|
||||
private DataSource secondaryDataSource;
|
||||
|
||||
@@ -40,11 +37,14 @@ public class SecondaryConfig {
|
||||
return builder
|
||||
.dataSource(secondaryDataSource)
|
||||
.properties(getVendorProperties(secondaryDataSource))
|
||||
.packages("com.didispace.domain.two")
|
||||
.packages("com.didispace.domain.s") //设置实体类所在位置
|
||||
.persistenceUnit("secondaryPersistenceUnit")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private JpaProperties jpaProperties;
|
||||
|
||||
private Map<String, String> getVendorProperties(DataSource dataSource) {
|
||||
return jpaProperties.getHibernateProperties(dataSource);
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.didispace.domain;
|
||||
package com.didispace.domain.p;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
+1
-8
@@ -1,4 +1,4 @@
|
||||
package com.didispace.domain;
|
||||
package com.didispace.domain.p;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@@ -13,12 +13,5 @@ import org.springframework.data.repository.query.Param;
|
||||
*/
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
User findByName(String name);
|
||||
|
||||
User findByNameAndAge(String name, Integer age);
|
||||
|
||||
@Query("from User u where u.name=:name")
|
||||
User findUser(@Param("name") String name);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.didispace.domain.s;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/3/21 下午3:35.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Entity
|
||||
public class Message {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String content;
|
||||
|
||||
public Message(){}
|
||||
|
||||
public Message(String name, String content) {
|
||||
this.name = name;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.didispace.domain.s;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/3/23 下午2:34.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
public interface MessageRepository extends JpaRepository<Message, Long> {
|
||||
|
||||
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
|
||||
spring.datasource.primary.username=root
|
||||
spring.datasource.primary.password=root
|
||||
spring.datasource.primary.password=52261340
|
||||
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
|
||||
|
||||
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
|
||||
spring.datasource.secondary.username=root
|
||||
spring.datasource.secondary.password=root
|
||||
spring.datasource.secondary.password=52261340
|
||||
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
|
||||
|
||||
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
|
||||
@@ -1,8 +1,14 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.domain.p.User;
|
||||
import com.didispace.domain.p.UserRepository;
|
||||
import com.didispace.domain.s.Message;
|
||||
import com.didispace.domain.s.MessageRepository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -11,6 +17,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@SpringApplicationConfiguration(Application.class)
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private MessageRepository messageRepository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -19,6 +29,19 @@ public class ApplicationTests {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
userRepository.save(new User("aaa", 10));
|
||||
userRepository.save(new User("bbb", 20));
|
||||
userRepository.save(new User("ccc", 30));
|
||||
userRepository.save(new User("ddd", 40));
|
||||
userRepository.save(new User("eee", 50));
|
||||
|
||||
Assert.assertEquals(5, userRepository.findAll().size());
|
||||
|
||||
messageRepository.save(new Message("o1", "aaaaaaaaaa"));
|
||||
messageRepository.save(new Message("o2", "bbbbbbbbbb"));
|
||||
messageRepository.save(new Message("o3", "cccccccccc"));
|
||||
|
||||
Assert.assertEquals(3, messageRepository.findAll().size());
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user