SpringBoot implements multiple data source configuration

Spring boot for multi data source configuration

springBoot implements the configuration of multiple data sources application.properties Configure two data sources in


Two datasources are required to read application.properties Different configurations of.

    public class DataSourceConfig{
        @Bean(name = "primaryDataSource")
        public DataSource primaryDataSource(){
            return DataSourceBuilder.create().build();

        @Bean(name = "secondaryDataSource")
        public DataSource secondaryDataSource(){
            return DataSourceBuilder.create().build();

JdbcTemplate support

In springBoot, the support for JdbcTemplate is relatively simple, and you only need to inject the corresponding datasource

    @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);

In this way, different data sources will be configured into different jdbctemplates. When using it, you can use the @ Autowired annotation with @ Qualifier

    public class TemplateService{
        private JdbcTemplate primaryJdbaTemplate

JPA support

JPA supports the configuration of multiple data sources. The configuration of DataSource is the same as shown in the JdbcTemplate above, and each JPA configuration must be matched with a configuration class. In the class, it is necessary to write the Entity entity class and Repository corresponding to the correct data source

 * @Author:High key city
 * @time: 
 * @Discription: Jpa Allocation of resources
        entityManagerFactoryRef = "entityManagerFactoryPrimary",
        transactionManagerRef = "transactionManagerPrimary",
        basePackages = {"com.gaojiancheng.demo.repository"}     //Set the location of the repository
public class PrimaryConfig {
    //Inject the previously configured PrimaryDataSource here
    private DataSource primaryDataSource;

    private JpaProperties jpaProperties;

//Entity class manager, as a Bean, named entityManagerPrimary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder){
        return entityManagerFactoryBean(builder).getObject().createEntityManager();

    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder){
        return builder.dataSource(primaryDataSource)
                .packages("com.gaojiancheng.demo.model")            //Set the location of entity class

    private Map<String , String> getVendorProperties(DataSource dataSource){
        return jpaProperties.getHibernateProperties(dataSource);

    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder){
        return new JpaTransactionManager(entityManagerFactoryBean(builder).getObject());

It should be noted that the correct location of the entity class and repository should be written, and the entity class manager and other contents should be written according to the above contents.

When configuring the second data source, the difference is that the name of the DataSource injection is changed from PrimaryDataSource to SecondaryDataSource

Next, write the information for the entity class

*Entity class User
public class User {
    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;
    // Omit getter and setter

Write the corresponding Repository

public interface UserRepository extends JpaRepository<User, Long> {

Finally, we test it with Junit

public class ApplicationTests {
    private UserRepository userRepository;
    private MessageRepository messageRepository;
    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());

Tags: Spring JDBC MySQL SpringBoot

Posted on Mon, 20 Jul 2020 12:04:53 -0400 by Julian Pedley