Springboot 多数据源 jpa mysql

1.pom

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <!--jpa-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.application.properties(我的版本springboot 2.0.3)

网上的其他blog这里不一样,我开始看着网上做,各种错。应该是版本的问题

#datasource1
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test1?useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=password
spring.datasource.primary.url=com.mysql.jdbc.Driver
#datasource2
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2?useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=password
spring.datasource.secondary.url=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto=create-drop

3.配置2个datasource

package com.example.mutidatasource.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class MutiDataSourceConfig {
//    datasource1
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource datasource1(){
        return DataSourceBuilder.create().build();
    }
//    datasource2
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource datasource2(){
        return DataSourceBuilder.create().build();
    }
}

4.two套JPA

第一套

package com.example.mutidatasource.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
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.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "localContainerEntityManagerFactoryBean1",
        basePackages = "com.example.mutidatasource.repository.first",//the location to scan repository
        transactionManagerRef = "platformTransactionManager1"
)
public class MutiEntityManager1 {
    @Resource
    private DataSource datasource1;
//    entity manager - factory 1
    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean1(EntityManagerFactoryBuilder builder){
        return builder.dataSource(datasource1)
                .properties(getVendorProperties())
                .packages("com.example.mutidatasource.entity.first")//the location to scan @entity
                .persistenceUnit("first").build();
    }
//    hibernate settings
    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }
//    Transaction manager
    @Bean
    @Primary
    public PlatformTransactionManager platformTransactionManager1(EntityManagerFactoryBuilder builder){
        return new JpaTransactionManager(localContainerEntityManagerFactoryBean1(builder).getObject());
    }
//    entity-manager
    @Bean
    @Primary
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return localContainerEntityManagerFactoryBean1(builder).getObject().createEntityManager();
    }
}

第二套差不多

package com.example.mutidatasource.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
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.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "localContainerEntityManagerFactoryBean2",
        basePackages = "com.example.mutidatasource.repository.second",//the location to scan repository
        transactionManagerRef = "platformTransactionManager2"
)
public class MutiEntityManager2 {
    @Resource
    private DataSource datasource2;
    //    entity manager - factory 1
    @Bean
    public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean2(EntityManagerFactoryBuilder builder){
        return builder.dataSource(datasource2)
                .properties(getVendorProperties())
                .packages("com.example.mutidatasource.entity.second")//the location to scan @entity
                .persistenceUnit("second").build();
    }
    //    hibernate settings
    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }
    //    Transaction manager
    @Bean
    public PlatformTransactionManager platformTransactionManager2(EntityManagerFactoryBuilder builder){
        return new JpaTransactionManager(localContainerEntityManagerFactoryBean2(builder).getObject());
    }
    //    entity-manager
    @Bean
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return localContainerEntityManagerFactoryBean2(builder).getObject().createEntityManager();
    }
}

5.two Entity

package com.example.mutidatasource.entity.first;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Entity1 {
    @Id
    private Integer id;
    @Column
    private String name = "datasource1_entity";
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Entity2 {
    @Id
    private Integer id;
    @Column
    private String name = "datasource2_entity";

6. two Repository

package com.example.mutidatasource.repository.first;

import com.example.mutidatasource.entity.first.Entity1;
import org.springframework.data.jpa.repository.JpaRepository;

public interface Irepository1 extends JpaRepository<Entity1,Long>{
}
package com.example.mutidatasource.repository.second;

import com.example.mutidatasource.entity.second.Entity2;
import org.springframework.data.jpa.repository.JpaRepository;

public interface Irepository2 extends JpaRepository<Entity2,Long>{
}

7. have 啊 try

package com.example.mutidatasource.controller;

import com.example.mutidatasource.entity.first.Entity1;
import com.example.mutidatasource.entity.second.Entity2;
import com.example.mutidatasource.repository.first.Irepository1;
import com.example.mutidatasource.repository.second.Irepository2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private Irepository1 irepository1;
    @Autowired
    private Irepository2 irepository2;
    @RequestMapping
    public String hello(){
        Entity1 entity1 = new Entity1();
        Entity2 entity2 = new Entity2();
        entity1.setId(5);
        entity2.setId(5);
        irepository1.save(entity1);
        irepository2.save(entity2);
        return "";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值