當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
使用JPA和@NamedQueries的Spring数据
生活随笔
收集整理的這篇文章主要介紹了
使用JPA和@NamedQueries的Spring数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
如果在JPA實(shí)體上使用Spring Data和@NamedQuery批注,則可以使用spring數(shù)據(jù)存儲(chǔ)庫(kù)以更方便的方式輕松使用它們。
在先前的博客中,我們使用spring boot和docker 創(chuàng)建了spring數(shù)據(jù)項(xiàng)目。 我們將使用相同的項(xiàng)目并增強(qiáng)存儲(chǔ)庫(kù)的功能。
我們將實(shí)現(xiàn)一個(gè)命名查詢,該查詢將僅在雇員的姓氏與指定的字符數(shù)相等時(shí)才提取雇員。
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;} } 請(qǐng)?zhí)貏e注意查詢名稱和我們遵循@ {EntityName}。{queryName}的約定。
然后,將方法添加到我們的spring數(shù)據(jù)存儲(chǔ)庫(kù)中。
最后但并非最不重要的一點(diǎn)是向我們的控制器添加一些功能。
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
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的使用JPA和@NamedQueries的Spring数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: guava 并发同步_Google Gu
- 下一篇: 微信视频美颜怎么开