Spring Boot整合JPA使用多个数据源的方法步骤

 更新时间:2019年08月09日 09:39:35   作者:Charles Zhang  
这篇文章主要给大家介绍了关于Spring Boot整合JPA使用多个数据源的方法步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

介绍

JPA(Java Persistence API)Java 持久化 API,是 Java 持久化的标准规范,Hibernate 是持久化规范的技术实现,而 Spring Data JPA 是在 Hibernate 基础上封装的一款框架。

第一次使用 Spring JPA 的时候,感觉这东西简直就是神器,几乎不需要写什么关于数据库访问的代码一个基本的 CURD 的功能就出来了。在这篇文章中,我们将介绍 Spring Boot 整合 JPA 使用多个数据源的方法。

开发环境:

  • Spring Boot 2.0.5
  • Spring Data JPA 2.0.5
  • MySQL 5.6
  • JDK 8
  • IDEA 2018.3
  • Windows 10

引入依赖

首先我们要 Spring Boot 引入 spring-boot-starter-data-jpa 依赖。

Maven 配置:

  <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>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
  </dependency>

Gradle 配置:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.5.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.5.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.0.5.RELEASE'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'

配置数据源

Spring Boot 提供了使用 application.properties 或 application.yml 文件配置项目属性的方法。我比较习惯使用 application.yml 文件,所以这里我只列出 application.yml 文件的写法。

spring:
 datasource:
  product:
   driver-class-name: com.mysql.jdbc.Driver
   jdbc-url: jdbc:mysql://127.0.0.1:3306/product?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
   username: root
   password: test123$
  customer:
   driver-class-name: com.mysql.jdbc.Driver
   jdbc-url: jdbc:mysql://127.0.0.1:3306/customer?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
   username: root
   password: test123$
 jpa:
  generate-ddl: true

配置好 application.yml 文件后分别在数据库创建 customer 和 product 数据库。

添加实体(Entity)类

客户实体:

package com.springboot.jpa.customer.models;

import javax.persistence.*;

@Entity
public class Customer {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;
  @Column(unique = true, nullable = false)
  private String email;
  private String firstName;
  private String lastName;

  protected Customer() {
  }

  public Customer(String email, String firstName, String lastName) {
    this.email = email;
    this.firstName = firstName;
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return String.format("Customer[id=%d, firstName='%s', lastName='%s',email='%s']", id, firstName, lastName, email);
  }

  public Integer getId() {
    return id;
  }

  public String getEmail() {
    return email;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }
}

产品实体:

package com.springboot.jpa.product.models;

import javax.persistence.*;

@Entity
public class Product {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int id;

  @Column(nullable = false)
  private String code;
  private String name;
  private double price;


  protected Product() {
  }

  public Product(String code, String name, double price) {
    this.code = code;
    this.name = name;
    this.price = price;
  }

  @Override
  public String toString() {
    return String.format("Product[id=%d, code='%s', name='%s', price='%s']", id, code, name, price);
  }

  public int getId() {
    return id;
  }

  public String getCode() {
    return code;
  }

  public String getName() {
    return name;
  }

  public double getPrice() {
    return price;
  }
}

添加数据仓库(Repository)类

客户 Repository:

package com.springboot.jpa.customer.repository;

import com.springboot.jpa.customer.models.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
}

产品 Repository:

package com.springboot.jpa.product.repository;

import com.springboot.jpa.product.models.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
}

添加配置(Config)类

客户配置:

package com.springboot.jpa.customer.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "customerEntityManagerFactory", transactionManagerRef = "customerTransactionManager", basePackages = {"com.springboot.jpa.customer.repository"})
public class CustomerConfig {

  @Primary
  @Bean(name = "customerDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.customer")
  public DataSource customerDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Primary
  @Bean(name = "customerEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("customerDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource).packages("com.springboot.jpa.customer.models").persistenceUnit("customer").build();
  }

  @Primary
  @Bean(name = "customerTransactionManager")
  public PlatformTransactionManager customerTransactionManager(@Qualifier("customerEntityManagerFactory") EntityManagerFactory customerEntityManagerFactory) {
    return new JpaTransactionManager(customerEntityManagerFactory);
  }
}

产品配置:

package com.springboot.jpa.product.config;


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "productEntityManagerFactory", transactionManagerRef = "productTransactionManager", basePackages = {"com.springboot.jpa.product.repository"}
)
public class ProductConfig {

  @Bean(name = "productDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.product")
  public DataSource dataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "productEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("productDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource).packages("com.springboot.jpa.product.models").persistenceUnit("product").build();
  }

  @Bean(name = "productTransactionManager")
  public PlatformTransactionManager productTransactionManager(@Qualifier("productEntityManagerFactory") EntityManagerFactory productEntityManagerFactory) {
    return new JpaTransactionManager(productEntityManagerFactory);
  }
}

项目结构:

src/main/java
- com.springboot.jpa
      - product
        - config
        - models
        - repository
      - customer
        - config
        - models
        - repository

添加测试类

客户测试类 CustomerDataSourcesTests:

package com.springboot.jpa;

import com.springboot.jpa.customer.repository.CustomerRepository;
import com.springboot.jpa.customer.models.Customer;
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;
import org.springframework.transaction.annotation.Transactional;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerDataSourcesTests {

  @Autowired
  private CustomerRepository customerRepository;

  @Test
  @Transactional("customerTransactionManager")
  public void createCustomer() {

    Customer customer = new Customer("master@weilog.net", "Charles", "Zhang");
    customer = customerRepository.save(customer);
    assertNotNull(customerRepository.findById(customer.getId()));
    assertEquals(customerRepository.findById(customer.getId()).get().getEmail(), "master@weilog.net");
  }
}

产品测试类 ProductDataSourcesTests:

package com.springboot.jpa;

import com.springboot.jpa.product.models.Product;
import com.springboot.jpa.product.repository.ProductRepository;
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;
import org.springframework.transaction.annotation.Transactional;

import org.junit.Test;

import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductDataSourcesTests {

  @Autowired
  private ProductRepository productRepository;

  @Test
  @Transactional("productTransactionManager")
  public void createProduct() {
    Product product = new Product("10000", "Book", 80.0);
    product = productRepository.save(product);

    assertNotNull(productRepository.findById(product.getId()));
  }

}

测试

分别运行两个测试类通过后,查询数据库。

客户表:

mysql> SELECT * FROM customer;
+----+-------------------+-----------+----------+
| id | email       | firstName | lastName |
+----+-------------------+-----------+----------+
| 1 | master@weilog.net | Charles  | Zhang  |
+----+-------------------+-----------+----------+
1 row in set

产品表:

mysql> SELECT * FROM product;
+----+-------+------+-------+
| id | code | name | price |
+----+-------+------+-------+
| 1 | 10000 | Book |  80 |
+----+-------+------+-------+
1 row in set

本文地址:Spring Boot 整合 JPA 使用多个数据源

项目地址:spring-boot-jpa

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

相关文章

  • Spring中基于xml的AOP实现详解

    Spring中基于xml的AOP实现详解

    这篇文章主要介绍了Spring中基于xml的AOP实现详解,基于xml与基于注解的AOP本质上是非常相似的,都是需要封装横切关注点,封装到切面中,然后把横切关注点封装为一个方法,再把该方法设置为当前的一个通知,再通过切入点表达式定位到横切点就可以了,需要的朋友可以参考下
    2023-09-09
  • 详解Java如何实现图像灰度化

    详解Java如何实现图像灰度化

    这篇文章主要介绍了灰度化的几种方法,以及如何使用Java实现灰度化。同时分析了网上一种常见却并不妥当的Java灰度化实现,以及证明了opencv的灰度化是使用“加权灰度化”法,下面一起来看看。
    2016-08-08
  • Spring Boot 发送邮件功能案例分析

    Spring Boot 发送邮件功能案例分析

    这篇文章主要介绍了 Spring Boot 发送邮件功能,本文通过代码结合案例分析给大家介绍的非常详细,需要的朋友可以参考下
    2017-11-11
  • SpringBootTest--踩坑错误的解决

    SpringBootTest--踩坑错误的解决

    这篇文章主要介绍了SpringBootTest--踩坑错误的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture

    Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture

    这篇文章主要介绍了Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture,本文直接给出实现代码,代码中包含详细注释,需要的朋友可以参考下
    2015-06-06
  • MyBatis核心源码深度剖析SQL语句执行过程

    MyBatis核心源码深度剖析SQL语句执行过程

    这篇文章主要介绍了MyBatis核心源码深度剖析SQL执行过程,mybatis执行SQL的流程都是根据statement字符串从configuration中获取对应的mappedStatement,本文通过实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2022-05-05
  • java中对象为null时的打印输出方式

    java中对象为null时的打印输出方式

    这篇文章主要介绍了java中对象为null时的打印输出方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Java多线程的其他知识_动力节点Java学院整理

    Java多线程的其他知识_动力节点Java学院整理

    这篇文章主要介绍了Java多线程的其他知识,需要的朋友可以参考下
    2017-05-05
  • SpringSecurity OAuth2单点登录和登出的实现

    SpringSecurity OAuth2单点登录和登出的实现

    本文主要介绍了SpringSecurity OAuth2单点登录和登出的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • tcc分布式事务框架体系解析

    tcc分布式事务框架体系解析

    这篇文章主要为大家介绍了tcc分布式事务框架体系结构的解析说明,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03

最新评论