日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

只读副本和Spring Data第2部分:配置基础项目

發(fā)布時(shí)間:2023/12/3 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 只读副本和Spring Data第2部分:配置基础项目 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在上一篇文章中,我們使用相同的數(shù)據(jù)設(shè)置了多個(gè)PostgreSQL實(shí)例。
我們的下一步將是使用這兩個(gè)服務(wù)器來配置我們的spring項(xiàng)目。

如前所述,由于我們使用完全相同的數(shù)據(jù)庫,因此我們將使用Spring Boot JPA帖子中的一些代碼。

這將是我們的gradle構(gòu)建文件

plugins { id 'org.springframework.boot' version '2.1.9.RELEASE' id 'io.spring.dependency-management' version '1.0.8.RELEASE' id 'java' } group = 'com.gkatzioura' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation "org.postgresql:postgresql:42.2.8" testImplementation 'org.springframework.boot:spring-boot-starter-test' }

現(xiàn)在,讓我們基于上一個(gè)博客上創(chuàng)建的表來創(chuàng)建模型。

package com.gkatzioura.springdatareadreplica.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table (name = "employee" , catalog= "spring_data_jpa_example" ) public class Employee { @Id @Column (name = "id" ) @GeneratedValue (strategy = GenerationType.IDENTITY) private Long id; @Column (name = "firstname" ) private String firstName; @Column (name = "lastname" ) private String lastname; @Column (name = "email" ) private String email; @Column (name = "age" ) private Integer age; @Column (name = "salary" ) private Integer salary; public Long getId() { return id; } public void setId(Long id) { this .id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this .firstName = firstName; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this .lastname = lastname; } public String getEmail() { return email; } public void setEmail(String email) { this .email = email; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } public Integer getSalary() { return salary; } public void setSalary(Integer salary) { this .salary = salary; } }

下一步是創(chuàng)建spring數(shù)據(jù)存儲(chǔ)庫。

package com.gkatzioura.springdatareadreplica.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.gkatzioura.springdatareadreplica.entity.Employee; public interface EmployeeRepository extends JpaRepository<Employee,Long> { }

另外,我們將添加一個(gè)控制器。

package com.gkatzioura.springdatareadreplica.controller; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.gkatzioura.springdatareadreplica.entity.Employee; import com.gkatzioura.springdatareadreplica.repository.EmployeeRepository; @RestController public class EmployeeContoller { private final EmployeeRepository employeeRepository; public EmployeeContoller(EmployeeRepository employeeRepository) { this .employeeRepository = employeeRepository; } @RequestMapping ( "/employee" ) public List<Employee> getEmployees() { return employeeRepository.findAll(); } }

它所要做的只是在application.yaml中添加正確的屬性。

spring: datasource: platform: postgres driverClassName: org.postgresql.Driver username: db-user password: your-password url: jdbc:postgresql: //127.0.0.2:5432/postgres url: jdbc:postgresql: //127.0.0.2:5432/postgres url: jdbc:postgresql:

如今,Spring Boot使得不必理會(huì)任何JPA配置。

這是運(yùn)行該應(yīng)用程序所需的全部。 一旦您的應(yīng)用程序運(yùn)行,只需嘗試獲取員工。

curl http: //localhost :8080 /employee

如您所見,我們沒有進(jìn)行任何JPA配置。 由于Spring Boot 2指定數(shù)據(jù)庫url就足以啟動(dòng)自動(dòng)配置并為您完成所有此配置。

但是,在我們的情況下,我們希望具有多個(gè)數(shù)據(jù)源和實(shí)體管理器配置。 在下一篇文章中,我們將為我們的應(yīng)用程序配置實(shí)體管理器。

翻譯自: https://www.javacodegeeks.com/2019/10/read-replicas-and-spring-data-configuring-the-base-project.html

總結(jié)

以上是生活随笔為你收集整理的只读副本和Spring Data第2部分:配置基础项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。