當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
使用JPA和@NamedQueries的Spring数据
生活随笔
收集整理的這篇文章主要介紹了
使用JPA和@NamedQueries的Spring数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如果在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.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
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的使用JPA和@NamedQueries的Spring数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: guava 并发同步_Google Gu
- 下一篇: 微信视频美颜怎么开