国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot項(xiàng)目中的多數(shù)據(jù)源支持的方法

SpringBoot項(xiàng)目中的多數(shù)據(jù)源支持的方法

2021-01-23 12:48QiHaiYan Java教程

本篇文章主要介紹了SpringBoot項(xiàng)目中的多數(shù)據(jù)源支持的方法,主要介紹在SpringBoot項(xiàng)目中利用SpringDataJpa技術(shù)如何支持多個(gè)數(shù)據(jù)庫的數(shù)據(jù)源,有興趣的可以了解一下

1.概述

項(xiàng)目中經(jīng)常會(huì)遇到一個(gè)應(yīng)用需要訪問多個(gè)數(shù)據(jù)源的情況,本文介紹在SpringBoot項(xiàng)目中利用SpringDataJpa技術(shù)如何支持多個(gè)數(shù)據(jù)庫的數(shù)據(jù)源。

具體的代碼參照該 示例項(xiàng)目

2.建立實(shí)體類(Entity)

首先,我們創(chuàng)建兩個(gè)簡單的實(shí)體類,分別屬于兩個(gè)不同的數(shù)據(jù)源,用于演示多數(shù)據(jù)源數(shù)據(jù)的保存和查詢。

Test實(shí)體類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.example.demo.test.data;
 
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name = "test")
public class Test {
 
  @Id
  private Integer id;
 
  public Test(){
 
  }
 
  public Integer getId() {
    return this.id;
  }
 
  public void setId(Integer id){
    this.id = id;
  }
}

Other實(shí)體類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.demo.other.data;
 
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name = "other")
public class Other {
 
  @Id
  private Integer id;
 
  public Integer getId() {
    return this.id;
  }
 
  public void setId(Integer id){
    this.id = id;
  }
}

需要注意的是,這兩個(gè)實(shí)體類分屬于不同的package,這一點(diǎn)極為重要,spring會(huì)根據(jù)實(shí)體類所屬的package來決定用那一個(gè)數(shù)據(jù)源進(jìn)行操作。

3.建立Repository

分別建立兩個(gè)實(shí)體類對應(yīng)的Repository,用于進(jìn)行數(shù)據(jù)操作。

TestRepository:

?
1
2
3
4
5
6
package com.example.demo.test.data;
 
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface TestRepository extends JpaRepository<Test, Integer> {
}

OtherRepository:

?
1
2
3
4
5
6
package com.example.demo.other.data;
 
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface OtherRepository extends JpaRepository<Other, Integer> {
}

得益于spring-data-jpa優(yōu)秀的封裝,我們只需創(chuàng)建一個(gè)接口,就擁有了對實(shí)體類的操作能力。

3.對多數(shù)據(jù)源進(jìn)行配置

分別對Test和Other兩個(gè)實(shí)體類配置對應(yīng)的數(shù)據(jù)源。配置的內(nèi)容主要包含三個(gè)要素:

  1. dataSource,數(shù)據(jù)源的連接信息
  2. entityManagerFactory,數(shù)據(jù)處理
  3. transactionManager,事務(wù)管理

Test實(shí)體類的數(shù)據(jù)源配置 TestDataConfig:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.example.demo.config;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 = "entityManagerFactory",
    basePackages = {"com.example.demo.test.data"}
)
public class TestDataConfig {
 
  @Autowired
  private JpaProperties jpaProperties;
 
  @Primary
  @Bean(name = "dataSource")
  @ConfigurationProperties(prefix = "spring.datasource")
  public DataSource dataSource() {
    return DataSourceBuilder.create().build();
  }
 
  @Primary
  @Bean(name = "entityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(
      EntityManagerFactoryBuilder builder,
      @Qualifier("dataSource") DataSource dataSource) {
    return builder
        .dataSource(dataSource)
        .packages("com.example.demo.test.data")
        .properties(jpaProperties.getHibernateProperties(dataSource))
        .persistenceUnit("test")
        .build();
  }
 
  @Primary
  @Bean(name = "transactionManager")
  public PlatformTransactionManager transactionManager(
      @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
  }
 
}

代碼中的Primary注解表示這是默認(rèn)數(shù)據(jù)源。

Other實(shí)體類的數(shù)據(jù)源配置 OtherDataConfig:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.example.demo.config;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 = "otherEntityManagerFactory",
    transactionManagerRef = "otherTransactionManager",
    basePackages = {"com.example.demo.other.data"}
)
public class OtherDataConfig {
 
  @Autowired
  private JpaProperties jpaProperties;
 
  @Bean(name = "otherDataSource")
  @ConfigurationProperties(prefix = "other.datasource")
  public DataSource otherDataSource() {
    return DataSourceBuilder.create().build();
  }
 
  @Bean(name = "otherEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean otherEntityManagerFactory(
      EntityManagerFactoryBuilder builder,
      @Qualifier("otherDataSource") DataSource otherDataSource) {
    return builder
        .dataSource(otherDataSource)
        .packages("com.example.demo.other.data")
        .properties(jpaProperties.getHibernateProperties(otherDataSource))
        .persistenceUnit("other")
        .build();
  }
 
  @Bean(name = "otherTransactionManager")
  public PlatformTransactionManager otherTransactionManager(
      @Qualifier("otherEntityManagerFactory") EntityManagerFactory otherEntityManagerFactory) {
    return new JpaTransactionManager(otherEntityManagerFactory);
  }
 
}

3.數(shù)據(jù)操作

我們創(chuàng)建一個(gè)Service類TestService來分別對兩個(gè)數(shù)據(jù)源進(jìn)行數(shù)據(jù)的操作。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example.demo.service;
 
import com.example.demo.other.data.Other;
import com.example.demo.other.data.OtherRepository;
import com.example.demo.test.data.Test;
import com.example.demo.test.data.TestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class TestService {
 
  @Autowired
  private TestRepository testRepository;
 
  @Autowired
  private OtherRepository otherRepository;
 
  @Value("${name:World}")
  private String name;
 
  public String getHelloMessage() {
    Test test = new Test();
    test.setId(1);
    test = testRepository.save(test);
 
    Other other = new Other();
    other.setId(2);
    other = otherRepository.save(other);
 
    return "Hello " + this.name + " : test's value = " + test.getId() + " , other's value = " + other.getId();
 
  }
 
}

對Test和Other分別進(jìn)行數(shù)據(jù)插入和讀取操作,程序運(yùn)行后會(huì)打印出兩個(gè)數(shù)據(jù)源各自的數(shù)據(jù)。 數(shù)據(jù)庫采用的mysql,連接信息在application.yml進(jìn)行配置。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
spring:
 datasource:
  url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false
  testWhileIdle: true
  validationQuery: SELECT 1 from dual
  username: test
  password: 11111111
  driverClassName: com.mysql.jdbc.Driver
 jpa:
  database: MYSQL
  show-sql: true
  hibernate:
   show-sql: true
   ddl-auto: create
   naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
  properties:
   hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
other:
 datasource:
  url: jdbc:mysql://localhost:3306/other?characterEncoding=utf-8&useSSL=false
  testWhileIdle: true
  validationQuery: SELECT 1
  username: other
  password: 11111111
  driverClassName: com.mysql.jdbc.Driver
 jpa:
  database: MYSQL
  show-sql: true
  hibernate:
   show-sql: true
   ddl-auto: create
   naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
  properties:
   hibernate.dialect: org.hibernate.dialect.MySQL5Dialect

Test實(shí)體對應(yīng)的是主數(shù)據(jù)源,采用了spring-boot的默認(rèn)數(shù)據(jù)源配置項(xiàng),Other實(shí)體單獨(dú)配置數(shù)據(jù)源連接。具體應(yīng)該讀取哪一段配置內(nèi)容,是在配置類OtherDataConfig中這行代碼指定的。

?
1
@ConfigurationProperties(prefix = "other.datasource")

本示例需要建立的數(shù)據(jù)庫用戶和庫可以通過以下命令處理:

?
1
2
3
4
5
6
CREATE USER 'test'@'localhost' IDENTIFIED BY '11111111';
GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost';
CREATE USER 'other'@'localhost' IDENTIFIED BY '11111111';
GRANT ALL PRIVILEGES ON *.* TO 'other'@'localhost';
create database test;
create database other;

4.總結(jié)

spring-data-jpa極大的簡化了數(shù)據(jù)庫操作,對于多數(shù)據(jù)源的支持,也只是需要增加一下配置文件和配置類而已。其中的關(guān)鍵內(nèi)容有3點(diǎn):

  1. 配置文件中數(shù)據(jù)源的配置
  2. 配置類的編寫
  3. 實(shí)體類所在的package必須與配置類中指定的package一致,如OtherDataConfig中指定的basePackages = {"com.example.demo.other.data"}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://my.oschina.net/hiease/blog/1553763

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品一二三 | 特级淫片日本高清视频免费 | 欧美精品一二三区 | 久久久精品综合 | 亚洲欧美视频在线观看 | 精品久久久久久久人人人人传媒 | 国产综合欧美 | 91久久综合亚洲鲁鲁五月天 | 一区二区三区四区精品 | 最近中文字幕免费 | 天堂资源| 日本伊人久久 | 成人免费视频观看视频 | 成人免毛片 | 国产在线观看一区 | www.日韩| 中文字幕在线观看日本 | av在线免费网址 | 欧美视频日韩视频 | 国产在线精品一区 | 黄色一级毛片在线观看 | 97国产精品 | 美女黄18 | 91久久精品国产91久久 | 成人欧美一区二区三区在线观看 | 无码一区二区三区视频 | 精品国产91亚洲一区二区三区www | 欧美综合久久 | 亚洲精品一级 | 国产精品99久久久久久动医院 | 狠狠色噜噜狠狠狠8888米奇 | 在线视频a | 中文字幕一区二区三区日韩精品 | 视频一区在线播放 | 亚洲精品成人天堂一二三 | 成人av播放 | 日本在线不卡视频 | 亚洲 成人 一区 | 久色视频在线观看 | 91精品国产91久久久久久 | 一区二区精品视频 |