The configuration of the data source can follow the example of the Spring boot Learning (vii) Web application using the JdbcTemplate Datasourceconfig implementation in a multi-data source configuration. Add a new JPA configuration to the first data source, noting two notes where you specify the entity entity and Repository definition location for the data source, and distinguish the primary data source with @Primary.
Package Com.xiaojingg;
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; /** * into GG */@Configuration @EnableTransactionManagement @EnableJpaRepositories (EntityManagerfactoryref= "Entitymanagerfactoryprimary", transactionmanagerref= "Transactionmanagerprimary", BasePa ckages= {"COM.XIAOJINGG.DOMAIN.P"})//Set repository location public class Primaryconfig {@Autowired @Qualifier ("Primaryd
Atasource ") Private DataSource Primarydatasource; @Primary @Bean (name = "Entitymanagerprimary") public Entitymanager Entitymanager (Entitymanagerfactorybuilder Build
ER) {return entitymanagerfactoryprimary (builder). GetObject (). Createentitymanager (); } @Primary @Bean (name = "Entitymanagerfactoryprimary") public Localcontainerentitymanagerfactorybean Entityma Nagerfactoryprimary (Entitymanagerfactorybuilder builder) {return builder. DataSource (primarydata Source). Properties (Getvendorproperties (Primarydatasource)). Packages ("Com.xiaojingg.domai
N.P ")//set where the entity class is located. Persistenceunit (" Primarypersistenceunit "). Build ();
} @Autowired private jpaproperties jpaproperties; Private map<string, string> getvendorproperties (DataSource DataSource) {return JPAPROPERTIES.GETHIBERNATEPR
Operties (DataSource); } @Primary @Bean (name = "Transactionmanagerprimary") public Platformtransactionmanager Transactionmanagerprim ary (Entitymanagerfactorybuilder builder) {return new Jpatransactionmanager (Entitymanagerfactoryprimary (builder). g
Etobject ()); }
}
A new JPA configuration for the second data source, similar to the first data source, is as follows:
Package Com.xiaojingg;
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; /** * into GG */@Configuration @EnableTransactionManagement @EnableJpaRepositories (entitymanagerfactoryref= "Entit
Ymanagerfactorysecondary ", Transactionmanagerref= "Transactionmanagersecondary", basepackages= {"COM.XIAOJINGG.DOMAIN.S"})//Set Reposito RY location public class Secondaryconfig {@Autowired @Qualifier ("Secondarydatasource") Private DataSource Secondaryda
Tasource;
@Bean (name = "Entitymanagersecondary") public entitymanager Entitymanager (Entitymanagerfactorybuilder builder) {
return Entitymanagerfactorysecondary (builder). GetObject (). Createentitymanager (); } @Bean (name = "Entitymanagerfactorysecondary") public Localcontainerentitymanagerfactorybean Entitymanagerfactor
Ysecondary (Entitymanagerfactorybuilder builder) {return builder. DataSource (Secondarydatasource) . Properties (Getvendorproperties (Secondarydatasource)). Packages ("COM.XIAOJINGG.DOMAIN.S")
Sets the location of the entity class. Persistenceunit ("Secondarypersistenceunit"). Build ();
} @Autowired private Jpaproperties jpaproperties; Private map<string, string> getvendorproperties (DataSource DataSource) {return Jpaproperties.gethibernat
Eproperties (DataSource); } @Bean (name = "Transactionmanagersecondary") Platformtransactionmanager transactionmanagersecondary (EntityManage
Rfactorybuilder builder) {return new Jpatransactionmanager (Entitymanagerfactorysecondary (builder). GetObject ()); }
}
The primary data source is configured with a configuration beginning with Spring.datasource.primary, and the second data source is configured with a configuration beginning with spring.datasource.secondary.
Package Com.xiaojingg;
Import Org.springframework.beans.factory.annotation.Qualifier;
Import Org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
Import org.springframework.boot.context.properties.ConfigurationProperties;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.Primary;
Import Javax.sql.DataSource; /** * Enter GG */@Configuration public class Datasourceconfig {@Bean (name = "Primarydatasource") @Qualifier ("Prim Arydatasource ") @ConfigurationProperties (prefix=" spring.datasource.primary ") public datasource Primarydatasource ()
{return datasourcebuilder.create (). build (); } @Bean (name = "Secondarydatasource") @Qualifier ("Secondarydatasource") @Primary @ConfigurationProperties (prefix= "Spring.datasource.secondary") public datasource Secondarydatasource () {return Datasourcebuilder.crea
Te (). build (); }
}
After the above configuration has been completed, the entity and data access objects for the primary data source are located at: COM.XIAOJINGG.DOMAIN.P, the entity and data access interfaces for the secondary data source are located at: com.xiaojingg.domain.s. Create the user entity and the corresponding repository interface under the respective entity and data provider master data sources under the two package respectively
Package COM.XIAOJINGG.DOMAIN.P;
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
/** * into GG * * @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;
}} package com.xiaojingg.domain.p;
Import Org.springframework.data.jpa.repository.JpaRepository; /** * Xiao Jin GG * * Public interface USerrepository extends Jparepository<user, long> {} from the data source, create the message entity and the corresponding Repository interface package
COM.XIAOJINGG.DOMAIN.S;
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
/** * into GG * * @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
}} package com.xiaojingg.domain.s;
Import Org.springframework.data.jpa.repository.JpaRepository; /** * Xiao Jin GG * * public interface Messagerepository extends Jparepository<message, long> {}
Finally, test cases are used to validate data operations using these two configurations for different data sources:
Package Com.xiaojingg;
Import Com.xiaojingg.domain.p.user;
Import Com.xiaojingg.domain.p.userrepository;
Import Com.xiaojingg.domain.s.message;
Import Com.xiaojingg.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.context.SpringBootTest;
Import Org.springframework.test.context.junit4.SpringRunner; /** * Jin GG */@RunWith (Springrunner.class) @SpringBootTest public class
springbootstudydemo7springdatajpamoreapplicationtests {@Autowired private userrepository userrepository;
@Autowired private Messagerepository messagerepository; @Before public void SetUp () {} @Test public void Test () throws Exception {Userrepository.save (new Us
ER ("AAA", 10));
Userrepository.save (The New User ("BBB", 20));
Userrepository.save (The New User ("CCC", 30)); Userrepository.save (NEW User ("ddd", 40));
Userrepository.save (The 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 ()); }
}
To run a screenshot: