如果在JPA实体上使用Spring Data和@NamedQuery批注,则可以使用spring数据存储库以更方便的方式轻松使用它们。
在先前的博客中,我们使用spring boot和docker 创建了spring数据项目。 我们将使用相同的项目并增强存储库的功能。
我们将实现一个命名查询,该查询将仅在雇员的姓氏与指定的字符数相等时才提取雇员。
package com.gkatzioura.springdata.jpa.persistence.entity;import javax.persistence.*;/*** Created by gkatzioura on 6/2/16.*/
@Entity
@Table(name = "employee", schema="spring_data_jpa_example")
@NamedQuery(name = "Employee.fetchByLastNameLength",query = "SELECT e FROM Employee e WHERE CHAR_LENGTH(e.lastname) =:length "
)
public class Employee {@Id@Column(name = "id")@GeneratedValue(strategy = GenerationType.SEQUENCE)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;}
}
请特别注意查询名称和我们遵循@ {EntityName}。{queryName}的约定。
然后,将方法添加到我们的spring数据存储库中。
package com.gkatzioura.springdata.jpa.persistence.repository;import com.gkatzioura.springdata.jpa.persistence.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;import java.util.List;/*** Created by gkatzioura on 6/2/16.*/
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Long>, EmployeeRepositoryCustom {List<Employee> fetchByLastNameLength(@Param("length") Long length);
}
最后但并非最不重要的一点是向我们的控制器添加一些功能。
package com.gkatzioura.springdata.jpa.controller;import com.gkatzioura.springdata.jpa.persistence.entity.Employee;
import com.gkatzioura.springdata.jpa.persistence.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** Created by gkatzioura on 6/2/16.*/
@RestController
public class TestController {@Autowiredprivate EmployeeRepository employeeRepository;@RequestMapping("/employee")public List<Employee> getTest() {return employeeRepository.findAll();}@RequestMapping("/employee/filter")public List<Employee> getFiltered(String firstName,@RequestParam(defaultValue = "0") Double bonusAmount) {return employeeRepository.getFirstNamesLikeAndBonusBigger(firstName,bonusAmount);}@RequestMapping("/employee/lastnameLength")public List<Employee> fetchByLength(Long length) {return employeeRepository.fetchByLastNameLength(length);}}
您可以在github上找到源代码。
翻译自: https://www.javacodegeeks.com/2017/03/spring-data-jpa-namedqueries.html