spring boot jpa 多数据源配置

本文详细介绍了如何在Spring Boot项目中配置和使用两个独立的数据源,包括配置文件设置、实体类创建、Repository定义及Java配置。同时,阐述了如何在事务中区分并调用不同数据源。

在实际项目中往往会使用2个数据源,这个时候就需要做额外的配置了。下面的配置在2.0.1.RELEASE 测试通过

1、配置文件

  配置两个数据源

spring.datasource.url=jdbc:mysql://localhost:13306/first?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.seconddatasource.url=jdbc:mysql://localhost:3306/second?useUnicode\=true&characterEncoding=utf-8&useSSL=false
spring.seconddatasource.driverClassName=com.mysql.jdbc.Driver spring.seconddatasource.username=root spring.seconddatasource.password=

  

2、entity

  创建两个简单的entity,它们分别对应不同的数据库。

  先看第一个entity,user

package com.example.first.entity;
 
@Entity
public class User {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;
    private int age;
}

  第二个entity, product

package com.example.second.entity;
 
@Entity
public class Product {
 
    @Id
    private Long id;
    private String name;
    private double price;
}

  注意:这两个enty位于不同的包里面。

 

3、Repository

  创建两个对应的Repository,注意他们也位于不同的包里

  

package com.example.first.dao;
 
public interface UserRepository
  extends JpaRepository<User, Long> { }

  

package com.example.second.dao;
 
public interface ProductRepository
  extends JpaRepository<Product, Long> { }

  

4、java 配置

  有两个数据源,需要两个配置文件,分别配置下面这几个Bean

  DataSource

  EntityManagerFactory 

  TransactionManager

 

  首先配置第一个数据源,我把它作为主数据源,注意,它们都使用了@Prime注解

@Configuration
@EnableJpaRepositories(basePackages = "com.example.first.dao",
        entityManagerFactoryRef = "firstEntityManager",
        transactionManagerRef = "firstTransactionManager")
public class FirstDataSourceConfig {

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean firstEntityManager(EntityManagerFactoryBuilder builder) {

        Map<String, String> properties = new HashMap<>();
        properties.put("hibernate.implicit_naming_strategy",
                "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy");
        properties.put("hibernate.physical_naming_strategy",
                "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
        return builder
                .dataSource(firstDataSource())
                .packages("com.example.user.entity")
                .persistenceUnit("first")
                .properties(properties)
                .build();
    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties firstDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSource firstDataSource() {
        return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }


    @Primary
    @Bean(name = "firstTransactionManager")
    public PlatformTransactionManager firstTransactionManager(@Qualifier("firstEntityManager")
                                                                      EntityManagerFactory firstEntityManagerFactory) {
        return new JpaTransactionManager(firstEntityManagerFactory);
    }
}

  

  接下来配置第二个数据源,和第一个完全一致,除了没有@Prime注解

@Configuration
@EnableJpaRepositories(
        basePackages = "com.example.second.dao",
        entityManagerFactoryRef = "secondEntityManager",
        transactionManagerRef = "secondTransactionManager"
)
public class SecondDataSourceConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean secondEntityManager(EntityManagerFactoryBuilder builder) {
        Map<String, String> properties = new HashMap<>();
        properties.put("hibernate.implicit_naming_strategy",
                "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy");
        properties.put("hibernate.physical_naming_strategy",
                "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
        return builder
                .dataSource(secondDataSource())
                .packages("com.example.second.entity")
                .persistenceUnit("second")
                .properties(properties)
                .build();
    }

    @Bean
    @ConfigurationProperties("spring.seconddatasource")
    public DataSourceProperties secondDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("spring.seconddatasource")
    public DataSource secondDataSource() {
        return secondDataSourceProperties().initializeDataSourceBuilder().build();
    }


    @Bean(name = "secondTransactionManager")
    public PlatformTransactionManager secondTransactionManager(@Qualifier("secondEntityManager")
                                                                      EntityManagerFactory secondEntityManagerFactory) {
        return new JpaTransactionManager(secondEntityManagerFactory);
    }
}

  

5、使用

  和单数据源的jpa使用方式基本一致。当使用事务注解是,第一个数据源的注解可以直接使用@Transactional,第二个为了区分必须使用@Transactional(value="secondTransactionManager")。可以在一个事务中调用另外一个数据源的事务。

 

2.0.1.RELEASE

转载于:https://www.cnblogs.com/lilinwei340/p/9657922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值