在上一篇文章中,我们使用相同的数据设置了多个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就足以启动自动配置并为您完成所有此配置。
但是,在我们的情况下,我们希望具有多个数据源和实体管理器配置。 在下一篇文章中,我们将为我们的应用程序配置实体管理器。