一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - SpringBoot集成Spring Data JPA及讀寫分離

SpringBoot集成Spring Data JPA及讀寫分離

2020-09-14 13:46Java教程網(wǎng) JAVA教程

這篇文章主要介紹了SpringBoot集成Spring Data JPA及讀寫分離的相關(guān)知識(shí),需要的朋友可以參考下

相關(guān)代碼: github OSCchina

JPA是什么

JPA(Java Persistence API)是Sun官方提出的Java持久化規(guī)范,它為Java開發(fā)人員提供了一種對(duì)象/關(guān)聯(lián)映射工具 來管理Java應(yīng)用中的關(guān)系數(shù)據(jù).它包括以下幾方面的內(nèi)容:

1.ORM映射 支持xml和注解方式建立實(shí)體與表之間的映射.

2.Java持久化API 定義了一些常用的CRUD接口,我們只需直接調(diào)用,而不需要考慮底層JDBC和SQL的細(xì)節(jié).

3.JPQL查詢語言 這是持久化操作中很重要的一個(gè)方面,通過面向?qū)ο蠖敲嫦驍?shù)據(jù)庫(kù)的查詢語言查詢數(shù)據(jù),避免程序的SQL語句緊密耦合.

在工作中,我們都會(huì)用到ORM技術(shù),比如Hibernate,JOOQ等,根據(jù)需求的不同,我們會(huì)采用不同的ORM框架,當(dāng)我們需要 更換ORM框架來滿足我們的需求時(shí),由于不同ORM框架的實(shí)現(xiàn),使用方式的區(qū)別以及各自為營(yíng),我們往往需要對(duì)代碼進(jìn)行重構(gòu).JPA的 出現(xiàn)就是為了解決這個(gè)問題,JPA充分吸收了現(xiàn)有一些ORM框架的優(yōu)點(diǎn),具有易于使用,伸縮性強(qiáng)等優(yōu)點(diǎn),為ORM技術(shù)提供了一套標(biāo)準(zhǔn)的 接口用來整合不同的ORM框架.

Hibernate對(duì)JPA的實(shí)現(xiàn)

JPA本身并不做具體的實(shí)現(xiàn),而只是定義了一些接口規(guī)范,讓其它ORM來具體的實(shí)現(xiàn)這些接口,就目前來說,對(duì)JPA規(guī)范實(shí)現(xiàn)最好的就是 Hibernate了.這里提一下Mybatis,Mybatis并沒有實(shí)現(xiàn)JPA規(guī)范,它本身也不能算做一個(gè)真正的ORM框架.

Spring Data JPA是什么

Spring Data JPA只是Spring Data框架的一個(gè)模塊,可以極大的簡(jiǎn)化JPA的使用,Spring Data JPA強(qiáng)大的地方還在于能夠簡(jiǎn)化我們 對(duì)持久層業(yè)務(wù)邏輯的開發(fā),通過規(guī)范持久層方法的名稱,通過名稱來判斷需要實(shí)現(xiàn)什么業(yè)務(wù)邏輯,我們機(jī)會(huì)可以在不寫一句sql,不做任何dao層 邏輯的情況下完成我們絕大部分的開發(fā),當(dāng)然,對(duì)于一些復(fù)雜的,性能要求高的查詢,Spring Data JPA一樣支持我們使用原生的sql.

在這里我們不過多的去介紹JPA以及Spring Data JPA,主要還是與SpringBoot集成的一些細(xì)節(jié)以及示例.

引入依賴

?
1
2
3
4
5
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

我們引入這個(gè)依賴后,發(fā)現(xiàn)也引入了Hibernate的包,這是現(xiàn)在一種默認(rèn)的做法,Hibernate已經(jīng)被作為JPA規(guī)范的最好實(shí)現(xiàn)了,這里就不介紹Druid數(shù)據(jù)源的 配置了,大家可以看另外一篇XXXX.

配置我們的數(shù)據(jù)源以及JPA(Hibernate)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#配置模板
#https://docs.spring.io/spring-boot/docs/1.4.0.RELEASE/reference/html/common-application-properties.html
#數(shù)據(jù)源
spring.datasource.druid.write.url=jdbc:mysql://localhost:3306/jpa
spring.datasource.druid.write.username=root
spring.datasource.druid.write.password=1
spring.datasource.druid.write.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.read.url=jdbc:mysql://localhost:3306/jpa
spring.datasource.druid.read.username=root
spring.datasource.druid.read.password=1
spring.datasource.druid.read.driver-class-name=com.mysql.jdbc.Driver
#JPA (JpaBaseConfiguration, HibernateJpaAutoConfiguration)
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.database=mysql
spring.jpa.generate-ddl=true
#就是hibernate.hbm2ddl.auto,具體說明可以看README
spring.jpa.hibernate.ddl-auto=update
#通過方法名解析sql的策略,具體說明可以看README,這里就不配置了
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultComponentSafeNamingStrategy
spring.jpa.show-sql=true
#spring.jpa.properties.*
#spring.jpa.properties.hibernate.hbm2ddl.auto=update
#spring.jpa.properties.hibernate.show_sql=true
#spring.jpa.properties.hibernate.use-new-id-generator-mappings=true

druid數(shù)據(jù)源注入

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Configuration
public class DruidDataSourceConfig {
  /**
   * DataSource 配置
   * @return
   */
  @ConfigurationProperties(prefix = "spring.datasource.druid.read")
  @Bean(name = "readDruidDataSource")
  public DataSource readDruidDataSource() {
    return new DruidDataSource();
  }
  /**
   * DataSource 配置
   * @return
   */
  @ConfigurationProperties(prefix = "spring.datasource.druid.write")
  @Bean(name = "writeDruidDataSource")
  @Primary
  public DataSource writeDruidDataSource() {
    return new DruidDataSource();
  }
}

EntityManagerFactory實(shí)例注入

EntityManagerFactory類似于Hibernate的SessionFactory,mybatis的SqlSessionFactory 總之,在執(zhí)行操作之前,我們總要獲取一個(gè)EntityManager,這就類似于Hibernate的Session, mybatis的sqlSession. 注入EntityManagerFactory有兩種方式,一種是直接注入EntityManagerFactory,另一種是通過 LocalContainerEntityManagerFactoryBean來間接注入.雖說這兩種方法都是基于 LocalContainerEntityManagerFactoryBean的,但是在配置上還是有一些區(qū)別.

1.直接注入EntityManagerFactory

配置:通過spring.jpa.properties.*來配置Hibernate的屬性

?
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
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use-new-id-generator-mappings=true
@Configuration
@EnableJpaRepositories(value = "com.lc.springBoot.jpa.repository",
            entityManagerFactoryRef = "writeEntityManagerFactory",
            transactionManagerRef="writeTransactionManager")
public class WriteDataSourceConfig {
  @Autowired
  JpaProperties jpaProperties;
  @Autowired
  @Qualifier("writeDruidDataSource")
  private DataSource writeDruidDataSource;
  /**
   * EntityManagerFactory類似于Hibernate的SessionFactory,mybatis的SqlSessionFactory
   * 總之,在執(zhí)行操作之前,我們總要獲取一個(gè)EntityManager,這就類似于Hibernate的Session,
   * mybatis的sqlSession.
   * @return
   */
  @Bean(name = "writeEntityManagerFactory")
  @Primary
  public EntityManagerFactory writeEntityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("com.lc.springBoot.jpa.entity");
    factory.setDataSource(writeDruidDataSource);//數(shù)據(jù)源
    factory.setJpaPropertyMap(jpaProperties.getProperties());
    factory.afterPropertiesSet();//在完成了其它所有相關(guān)的配置加載以及屬性設(shè)置后,才初始化
    return factory.getObject();
  }
  /**
   * 配置事物管理器
   * @return
   */
  @Bean(name = "writeTransactionManager")
  @Primary
  public PlatformTransactionManager writeTransactionManager() {
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    jpaTransactionManager.setEntityManagerFactory(this.writeEntityManagerFactory());
    return jpaTransactionManager;
  }
}

2.先注入LocalContainerEntityManagerFactoryBean,再獲取EntityManagerFactory

配置:

?
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
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.database=mysql
spring.jpa.generate-ddl=true
#就是hibernate.hbm2ddl.auto,具體說明可以看README
spring.jpa.hibernate.ddl-auto=update
#通過方法名解析sql的策略,具體說明可以看README,這里就不配置了
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultComponentSafeNamingStrategy
spring.jpa.show-sql=true
@Configuration
@EnableJpaRepositories(value = "com.lc.springBoot.jpa.repository",
    entityManagerFactoryRef = "writeEntityManagerFactory",
    transactionManagerRef = "writeTransactionManager")
public class WriteDataSourceConfig1 {
  @Autowired
  JpaProperties jpaProperties;
  @Autowired
  @Qualifier("writeDruidDataSource")
  private DataSource writeDruidDataSource;
  /**
   * 我們通過LocalContainerEntityManagerFactoryBean來獲取EntityManagerFactory實(shí)例
   * @return
   */
  @Bean(name = "writeEntityManagerFactoryBean")
  @Primary
  public LocalContainerEntityManagerFactoryBean writeEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
    return builder
        .dataSource(writeDruidDataSource)
        .properties(jpaProperties.getProperties())
        .packages("com.lc.springBoot.jpa.entity") //設(shè)置實(shí)體類所在位置
        .persistenceUnit("writePersistenceUnit")
        .build();
    //.getObject();//不要在這里直接獲取EntityManagerFactory
  }
  /**
   * EntityManagerFactory類似于Hibernate的SessionFactory,mybatis的SqlSessionFactory
   * 總之,在執(zhí)行操作之前,我們總要獲取一個(gè)EntityManager,這就類似于Hibernate的Session,
   * mybatis的sqlSession.
   * @param builder
   * @return
   */
  @Bean(name = "writeEntityManagerFactory")
  @Primary
  public EntityManagerFactory writeEntityManagerFactory(EntityManagerFactoryBuilder builder) {
    return this.writeEntityManagerFactoryBean(builder).getObject();
  }
  /**
   * 配置事物管理器
   * @return
   */
  @Bean(name = "writeTransactionManager")
  @Primary
  public PlatformTransactionManager writeTransactionManager(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(writeEntityManagerFactory(builder));
  }
}

對(duì)于這個(gè)配置

?
1
2
3
4
5
6
7
8
9
10
11
@Bean(name = "writeEntityManagerFactoryBean")
  @Primary
  public LocalContainerEntityManagerFactoryBean writeEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
    return builder
        .dataSource(writeDruidDataSource)
        .properties(jpaProperties.getProperties())
        .packages("com.lc.springBoot.jpa.entity") //設(shè)置實(shí)體類所在位置
        .persistenceUnit("writePersistenceUnit")
        .build();
    //.getObject();//不要在這里直接獲取EntityManagerFactory
  }

getObject()方法可以獲取到EntityManagerFactory的實(shí)例,看似跟第一種沒有什么區(qū)別,但是我們不能直接用 getObject(),不然會(huì)獲取不到,報(bào)空指針異常.

讀寫分離配置

自定義注入AbstractRoutingDataSource

?
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
@Configuration
public class DataSourceConfig {
  private final static String WRITE_DATASOURCE_KEY = "writeDruidDataSource";
  private final static String READ_DATASOURCE_KEY = "readDruidDataSource";
  /**
   * 注入AbstractRoutingDataSource
   * @param readDruidDataSource
   * @param writeDruidDataSource
   * @return
   * @throws Exception
   */
  @Bean
  public AbstractRoutingDataSource routingDataSource(
      @Qualifier(READ_DATASOURCE_KEY) DataSource readDruidDataSource,
      @Qualifier(WRITE_DATASOURCE_KEY) DataSource writeDruidDataSource
  ) throws Exception {
    DynamicDataSource dataSource = new DynamicDataSource();
    Map<Object, Object> targetDataSources = new HashMap();
    targetDataSources.put(WRITE_DATASOURCE_KEY, writeDruidDataSource);
    targetDataSources.put(READ_DATASOURCE_KEY, readDruidDataSource);
    dataSource.setTargetDataSources(targetDataSources);
    dataSource.setDefaultTargetDataSource(writeDruidDataSource);
    return dataSource;
  }
}

自定義注解

?
1
2
3
4
5
6
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TargetDataSource {
  String dataSource() default "";//數(shù)據(jù)源
}

使用ThreadLocal使數(shù)據(jù)源與線程綁定

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class DynamicDataSourceHolder {
   //使用ThreadLocal把數(shù)據(jù)源與當(dāng)前線程綁定
   private static final ThreadLocal<String> dataSources = new ThreadLocal<String>();
   public static void setDataSource(String dataSourceName) {
     dataSources.set(dataSourceName);
   }
   public static String getDataSource() {
     return (String) dataSources.get();
   }
   public static void clearDataSource() {
     dataSources.remove();
   }
 }
 public class DynamicDataSource extends AbstractRoutingDataSource {
   @Override
   protected Object determineCurrentLookupKey() {
     //可以做一個(gè)簡(jiǎn)單的負(fù)載均衡策略
     String lookupKey = DynamicDataSourceHolder.getDataSource();
     System.out.println("------------lookupKey---------"+lookupKey);
     return lookupKey;
   }
 }

定義切面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Aspect
  @Component
  public class DynamicDataSourceAspect {
    @Around("execution(public * com.lc.springBoot.jpa.service..*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
      MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
      Method targetMethod = methodSignature.getMethod();
      if (targetMethod.isAnnotationPresent(TargetDataSource.class)) {
        String targetDataSource = targetMethod.getAnnotation(TargetDataSource.class).dataSource();
        System.out.println("----------數(shù)據(jù)源是:" + targetDataSource + "------");
        DynamicDataSourceHolder.setDataSource(targetDataSource);
      }
      Object result = pjp.proceed();//執(zhí)行方法
      DynamicDataSourceHolder.clearDataSource();
      return result;
    }
  }

以上所述是小編給大家介紹的SpringBoot集成Spring Data JPA及讀寫分離,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品美女福利视频免费专区 | 91九色视频无限观看免费 | 狠狠草视频 | 美女逼逼喷水 | 亚洲无线一二三四区 | 国产网站视频 | 特级av毛片免费观看 | 日韩理论片 | 欧美日本道免费一区二区三区 | haodiaocao几万部精彩视频 | 啊用力好大粗黑人小说 | 99久精品 | 96免费精品视频在线 | 青青青国产在线观看 | 日本一在线中文字幕天堂 | 男人猛进女人屁股免费 | 国产尤物视频 | 国内视频一区二区三区 | 2018成年动漫在线观看 | 赤坂丽女医bd无删减在线观看 | 91人成尤物在线 | 午夜影院免费体验 | 欧美ggg666| 成人小视频在线观看免费 | 国产精品人人视频 | 免费看伦理片 | 欧美图片另类小说综合 | 九九热免费在线观看 | 国产精品久久久久久久久免费观看 | 亚洲国产天堂综合一区 | 精品国产原创在线观看视频 | 60老妇性xxxxhd | 亚洲国产欧美另类 | 亚洲一区二区三区在线播放 | 午夜影视在线观看 | 好男人社区www影院在线观看 | 奇米久草 | 亚洲国产精品日韩高清秒播 | 精品日韩二区三区精品视频 | a韩剧 | 免费观看欧美成人禁片 |