.
This commit is contained in:
@@ -33,8 +33,4 @@ public class DataSourceConfig {
|
|||||||
return DataSourceBuilder.create().build();
|
return DataSourceBuilder.create().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package com.didispace;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
||||||
|
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
@EnableJpaRepositories(
|
||||||
|
entityManagerFactoryRef="entityManagerFactoryPrimary",
|
||||||
|
transactionManagerRef="transactionManagerPrimary",
|
||||||
|
basePackages= { "com.didispace.domain.one" })
|
||||||
|
public class PrimaryConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JpaProperties jpaProperties;
|
||||||
|
|
||||||
|
@Autowired @Qualifier("primaryDataSource")
|
||||||
|
private DataSource primaryDataSource;
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean(name = "entityManagerPrimary")
|
||||||
|
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
|
||||||
|
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean(name = "entityManagerFactoryPrimary")
|
||||||
|
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
|
||||||
|
return builder
|
||||||
|
.dataSource(primaryDataSource)
|
||||||
|
.properties(getVendorProperties(primaryDataSource))
|
||||||
|
.packages("com.didispace.domain.one") //设置实体类所在位置
|
||||||
|
.persistenceUnit("primaryPersistenceUnit")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> getVendorProperties(DataSource dataSource) {
|
||||||
|
return jpaProperties.getHibernateProperties(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean(name = "transactionManagerPrimary")
|
||||||
|
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
|
||||||
|
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.didispace;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
|
||||||
|
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
@EnableJpaRepositories(
|
||||||
|
entityManagerFactoryRef="entityManagerFactorySecondary",
|
||||||
|
transactionManagerRef="transactionManagerSecondary",
|
||||||
|
basePackages= { "com.didispace.domain.two" })
|
||||||
|
public class SecondaryConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JpaProperties jpaProperties;
|
||||||
|
|
||||||
|
@Autowired @Qualifier("secondaryDataSource")
|
||||||
|
private DataSource secondaryDataSource;
|
||||||
|
|
||||||
|
@Bean(name = "entityManagerSecondary")
|
||||||
|
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
|
||||||
|
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "entityManagerFactorySecondary")
|
||||||
|
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
|
||||||
|
return builder
|
||||||
|
.dataSource(secondaryDataSource)
|
||||||
|
.properties(getVendorProperties(secondaryDataSource))
|
||||||
|
.packages("com.didispace.domain.two")
|
||||||
|
.persistenceUnit("secondaryPersistenceUnit")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> getVendorProperties(DataSource dataSource) {
|
||||||
|
return jpaProperties.getHibernateProperties(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "transactionManagerSecondary")
|
||||||
|
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
|
||||||
|
return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.didispace.domain;
|
||||||
|
|
||||||
|
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 User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private Integer age;
|
||||||
|
|
||||||
|
public User(){}
|
||||||
|
|
||||||
|
public User(String name, Integer age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Integer getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Integer age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.didispace.domain;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 程序猿DD
|
||||||
|
* @version 1.0.0
|
||||||
|
* @date 16/3/23 下午2:34.
|
||||||
|
* @blog http://blog.didispace.com
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,13 +1,9 @@
|
|||||||
package com.didispace;
|
package com.didispace;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
|
||||||
@@ -15,35 +11,14 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
@SpringApplicationConfiguration(Application.class)
|
@SpringApplicationConfiguration(Application.class)
|
||||||
public class ApplicationTests {
|
public class ApplicationTests {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
@Qualifier("primaryJdbcTemplate")
|
|
||||||
protected JdbcTemplate jdbcTemplate1;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
@Qualifier("secondaryJdbcTemplate")
|
|
||||||
protected JdbcTemplate jdbcTemplate2;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
jdbcTemplate1.update("DELETE FROM USER ");
|
|
||||||
jdbcTemplate2.update("DELETE FROM USER ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() throws Exception {
|
public void test() throws Exception {
|
||||||
|
|
||||||
// 往第一个数据源中插入两条数据
|
|
||||||
jdbcTemplate1.update("insert into user(id,name,age) values(?, ?, ?)", 1, "aaa", 20);
|
|
||||||
jdbcTemplate1.update("insert into user(id,name,age) values(?, ?, ?)", 2, "bbb", 30);
|
|
||||||
|
|
||||||
// 往第二个数据源中插入一条数据,若插入的是第一个数据源,则会主键冲突报错
|
|
||||||
jdbcTemplate2.update("insert into user(id,name,age) values(?, ?, ?)", 1, "aaa", 20);
|
|
||||||
|
|
||||||
// 查一下第一个数据源中是否有两条数据,验证插入是否成功
|
|
||||||
Assert.assertEquals("2", jdbcTemplate1.queryForObject("select count(1) from user", String.class));
|
|
||||||
|
|
||||||
// 查一下第一个数据源中是否有两条数据,验证插入是否成功
|
|
||||||
Assert.assertEquals("1", jdbcTemplate2.queryForObject("select count(1) from user", String.class));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user