當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
阅读副本和Spring Data第2部分:配置基础项目
生活随笔
收集整理的這篇文章主要介紹了
阅读副本和Spring Data第2部分:配置基础项目
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在上一篇文章中,我們使用相同的數據設置了多個PostgreSQL實例。
下一步將是使用這兩個服務器來配置spring項目。
如前所述,由于我們使用完全相同的數據庫,因此我們將使用Spring Boot JPA帖子中的一些代碼。
這將是我們的gradle構建文件
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' }現在,讓我們基于上一個博客上創建的表來創建模型。
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; } }下一步是創建spring數據存儲庫。
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> { }另外,我們將添加一個控制器。
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使得不必理會任何JPA配置。
這是運行該應用程序所需的全部。 應用程序運行后,只需嘗試獲取員工即可。
curl http: //localhost :8080 /employee如您所見,我們沒有進行任何JPA配置。 由于Spring Boot 2指定數據庫url就足以啟動自動配置并為您完成所有此配置。
但是,在我們的情況下,我們希望具有多個數據源和實體管理器配置。 在下一篇文章中,我們將為我們的應用程序配置實體管理器。
翻譯自: https://www.javacodegeeks.com/2019/10/read-replicas-and-spring-data-configuring-the-base-project.html
總結
以上是生活随笔為你收集整理的阅读副本和Spring Data第2部分:配置基础项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 舍己为人的意思是什么 舍己为人的解释
- 下一篇: 倪妮怎么读 倪妮的读音和介绍