欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot整合JPA使用多個數(shù)據(jù)源的方法步驟

 更新時間:2019年08月09日 09:39:35   作者:Charles Zhang  
這篇文章主要給大家介紹了關(guān)于Spring Boot整合JPA使用多個數(shù)據(jù)源的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

介紹

JPA(Java Persistence API)Java 持久化 API,是 Java 持久化的標(biāo)準(zhǔn)規(guī)范,Hibernate 是持久化規(guī)范的技術(shù)實現(xiàn),而 Spring Data JPA 是在 Hibernate 基礎(chǔ)上封裝的一款框架。

第一次使用 Spring JPA 的時候,感覺這東西簡直就是神器,幾乎不需要寫什么關(guān)于數(shù)據(jù)庫訪問的代碼一個基本的 CURD 的功能就出來了。在這篇文章中,我們將介紹 Spring Boot 整合 JPA 使用多個數(shù)據(jù)源的方法。

開發(fā)環(huán)境:

  • 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'

配置數(shù)據(jù)源

Spring Boot 提供了使用 application.properties 或 application.yml 文件配置項目屬性的方法。我比較習(xí)慣使用 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 文件后分別在數(shù)據(jù)庫創(chuàng)建 customer 和 product 數(shù)據(jù)庫。

添加實體(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;
  }
}

產(chǎn)品實體:

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

添加數(shù)據(jù)倉庫(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> {
}

產(chǎn)品 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);
  }
}

產(chǎn)品配置:

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

項目結(jié)構(gòu):

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

產(chǎn)品測試類 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()));
  }

}

測試

分別運行兩個測試類通過后,查詢數(shù)據(jù)庫。

客戶表:

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

產(chǎn)品表:

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

本文地址:Spring Boot 整合 JPA 使用多個數(shù)據(jù)源

項目地址:spring-boot-jpa

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • SpringBoot實現(xiàn)WebSocket即時通訊的示例代碼

    SpringBoot實現(xiàn)WebSocket即時通訊的示例代碼

    本文主要介紹了SpringBoot實現(xiàn)WebSocket即時通訊的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java實現(xiàn)拖拽文件上傳dropzone.js的簡單使用示例代碼

    Java實現(xiàn)拖拽文件上傳dropzone.js的簡單使用示例代碼

    本篇文章主要介紹了Java實現(xiàn)拖拽文件上傳dropzone.js的簡單使用示例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-07-07
  • java使用BeanUtils.copyProperties踩坑經(jīng)歷

    java使用BeanUtils.copyProperties踩坑經(jīng)歷

    最近在做個項目,踩了個坑特此記錄一下,本文主要介紹了使用BeanUtils.copyProperties踩坑經(jīng)歷,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Java經(jīng)典面試題匯總--多線程

    Java經(jīng)典面試題匯總--多線程

    本篇總結(jié)的是Java多線程相關(guān)的面試題,后續(xù)會持續(xù)更新,希望我的分享可以幫助到正在備戰(zhàn)面試的實習(xí)生或者已經(jīng)工作的同行,如果發(fā)現(xiàn)錯誤還望大家多多包涵,不吝賜教,謝謝
    2021-06-06
  • Java弱鍵集合WeakHashMap及ConcurrentCache原理詳解

    Java弱鍵集合WeakHashMap及ConcurrentCache原理詳解

    這篇文章主要介紹了Java弱鍵集合WeakHashMap及ConcurrentCache原理詳解,基于哈希表的Map接口實現(xiàn),支持null鍵和值,但是WeakHashMap具有弱鍵,可用來實現(xiàn)緩存存儲,在進(jìn)行GC的時候會自動回收鍵值對,需要的朋友可以參考下
    2023-09-09
  • SpringBoot中隨機(jī)鹽值+雙重SHA256加密實戰(zhàn)

    SpringBoot中隨機(jī)鹽值+雙重SHA256加密實戰(zhàn)

    本文主要介紹了SpringBoot中隨機(jī)鹽值+雙重SHA256加密實戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • Java 中的類和對象詳情

    Java 中的類和對象詳情

    這篇文章主要介紹了Java 中的類和對象,類可以看成是創(chuàng)建Java對象的模板,下面文章圍繞著Java 類與對象詳細(xì)內(nèi)容展開,需要的朋友可以參考一下
    2021-11-11
  • shiro攔截認(rèn)證的全過程記錄

    shiro攔截認(rèn)證的全過程記錄

    Apache?Shiro是一個強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗證、授權(quán)、密碼和會話管理,下面這篇文章主要給大家介紹了關(guān)于shiro攔截認(rèn)證的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Springboot實現(xiàn)XSS漏洞過濾的示例代碼

    Springboot實現(xiàn)XSS漏洞過濾的示例代碼

    這篇文章主要介紹了Springboot實現(xiàn)XSS漏洞過濾的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • java課程設(shè)計之坦克大戰(zhàn)

    java課程設(shè)計之坦克大戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了java課程設(shè)計之坦克大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12

最新評論