Spring Boot Learning Advanced Notes (iv)-Multiple data source configuration (JdbcTemplate, SPRING-DATA-JPA)

Source: Internet
Author: User

In the actual development process, often we need to link multiple databases to operate, so the configuration of multiple data sources is unavoidable.

First, JdbcTemplate support:

Spring Boot configuration Multi-data source is relatively simple

1) Modify the configuration file "Application.properties"

spring.datasource.primary.url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding= Utf-8&usessl=false

Spring.datasource.primary.username=Root

spring.datasource.primary.password=123456

Spring.datasource.primary.driver-class-name=Com.mysql.jdbc.Driver

spring.datasource.secondary.url=jdbc:mysql://192.168.2.111:3306/test?useunicode=true&characterencoding= Utf-8&usessl=false

Spring.datasource.secondary.username=Root

spring.datasource.secondary.password=123456

Spring.datasource.secondary.driver-class-name=Com.mysql.jdbc.Driver

2) Add data Class "Datasourceconfig.java"

Import Javax.sql.DataSource;

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 Org.springframework.jdbc.core.JdbcTemplate;

@Configuration

Public class Datasourceconfig {

@Bean(name = "Primarydatasource")

@Qualifier("Primarydatasource")

@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.create (). build ();

}

@Bean(name = "Primaryjdbctemplate")

Public JdbcTemplate Primaryjdbctemplate (

@Qualifier ("Primarydatasource") DataSource DataSource) {

return new JdbcTemplate (dataSource);

}

@Bean(name = "Secondaryjdbctemplate")

Public JdbcTemplate Secondaryjdbctemplate (

@Qualifier ("Secondarydatasource") DataSource DataSource) {

return new JdbcTemplate (dataSource);

}

}

3) Add different jdbctemplate according to the annotations when the service is called

@Autowired

@Qualifier("Primaryjdbctemplate")

protected JdbcTemplate jdbctemplate;

@Autowired

@Qualifier("Secondaryjdbctemplate")

protected JdbcTemplate jdbcTemplate2;

@Override

Public Integer Getusercount () {

return jdbctemplate.queryforobject ("SELECT COUNT (1) from USER", Integer. ) Class);

}

@Override

Public Integer GetUserCount2 () {

return jdbctemplate2.queryforobject ("SELECT COUNT (1) from USER", Integer. ) Class);

}

Second, SPRING-DATA-JPA Support:

1) Create the data source configuration class "Primaryconfig.java" and "Secondconfig.java", with "" as an example


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.ZH.SPRINGBOOTDEMO.DOMAIN.P "})//Set repository location
public class Primaryconfig {

@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.ZH.SPRINGBOOTDEMO.DOMAIN.P ")//Set the entity class where
. Persistenceunit ("Primarypersistenceunit"). Build ();
}

@Autowired
Private Jpaproperties jpaproperties;

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 ());
}

}

2) Create the entity class domain and the corresponding respository

Import Org.springframework.data.jpa.repository.JpaRepository;
Import Org.springframework.data.jpa.repository.Query;
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);


}

3) The corresponding respository can be called according to the annotations.

Spring Boot Learning Advanced Notes (iv)-Multiple data source configuration (JdbcTemplate, SPRING-DATA-JPA)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.