网上商城网站 找什么做,凡科做商品网站的教学视频,纯静态网站模板,cida室内设计师资格证如果在JPA实体上使用Spring Data和NamedQuery批注#xff0c;则可以使用spring数据存储库以更方便的方式轻松使用它们。 在先前的博客中#xff0c;我们使用spring boot和docker 创建了spring数据项目。 我们将使用相同的项目并增强存储库的功能。 我们将实现一个命名查询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, schemaspring_data_jpa_example)
NamedQuery(name Employee.fetchByLastNameLength,query SELECT e FROM Employee e WHERE CHAR_LENGTH(e.lastname) :length
)
public class Employee {IdColumn(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 JpaRepositoryEmployee,Long, EmployeeRepositoryCustom {ListEmployee 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 ListEmployee getTest() {return employeeRepository.findAll();}RequestMapping(/employee/filter)public ListEmployee getFiltered(String firstName,RequestParam(defaultValue 0) Double bonusAmount) {return employeeRepository.getFirstNamesLikeAndBonusBigger(firstName,bonusAmount);}RequestMapping(/employee/lastnameLength)public ListEmployee fetchByLength(Long length) {return employeeRepository.fetchByLastNameLength(length);}} 您可以在github上找到源代码。 翻译自: https://www.javacodegeeks.com/2017/03/spring-data-jpa-namedqueries.html