Spring Data JPA關聯查詢和@Query
阿新 • • 發佈:2019-02-17
示例:關聯查詢和@Query
建立持久化類
程式清單:/jpa/src/main/java/com/dwx/bean/Student.java
package com.dwx.bean; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_student") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private String sex; private Integer age; private String address; @ManyToOne(fetch=FetchType.LAZY,targetEntity=Clazz.class) @JoinColumn(name="clazzId",referencedColumnName="code") private Clazz clazz; public Student() { } public Student(Integer id, String name, String sex, Integer age, String address,Clazz clazz) { super(); this.id = id; this.name = name; this.sex = sex; this.age = age; this.address = address; this.clazz=clazz; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Clazz getClazz() { return clazz; } public void setClazz(Clazz clazz) { this.clazz = clazz; } }
程式清單:/jpa/src/main/java/com/dwx/bean/Clazz.java
package com.dwx.bean; import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="t_class") public class Clazz { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer code; private String name; @OneToMany( fetch=FetchType.LAZY,targetEntity=Student.class,mappedBy="clazz") private List<Student> students=new ArrayList<>(); public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
定義資料訪問層介面
程式清單:/jpa/src/main/java/com/dwx/repository/StudentRepository.java
package com.dwx.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import com.dwx.bean.Student; public interface StudentRepository extends JpaRepository<Student, Integer> { List<Student> findByClazz_name(String clazzName); @Query("select s from Student s where s.clazz.name=?1") List<Student> findStudentsByClazzname(String clazzName); }
程式清單:/jpa/src/main/java/com/dwx/repository/ClazzRepository.java
package com.dwx.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.dwx.bean.Clazz;
public interface ClazzRepository extends JpaRepository<Clazz, Integer> {
}
定義業務層類
程式清單:/jpa/src/main/java/com/dwx/service/SchoolService.java
package com.dwx.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dwx.bean.Student;
import com.dwx.repository.ClazzRepository;
import com.dwx.repository.StudentRepository;
@Service
public class SchoolService {
@Resource
private StudentRepository studentRepository;
@Resource
private ClazzRepository clazzRepository;
public List<Map<String,Object>> getStusByClazzName(String clazzName){
//使用"_"和@Query查詢結果一致
List<Student> students=studentRepository.findByClazz_name(clazzName);
//List<Student> students=studentRepository.findStudentsByClazzname(clazzName);
List<Map<String,Object>> results=new ArrayList<Map<String,Object>>();
for(Student stu:students) {
Map<String,Object> map=new HashMap<>();
map.put("name", stu.getName());
map.put("sex", stu.getSex());
map.put("age", stu.getAge());
map.put("address", stu.getAddress());
results.add(map);
}
return results;
}
}
定義控制器類
程式清單:/jpa/src/main/java/com/dwx/controller/StudentController.java
package com.dwx.controller;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dwx.service.SchoolService;
@RestController
@RequestMapping("/student")
public class StudentController {
@Resource
private SchoolService schoolService;
@RequestMapping("/getClazzStus")
public List<Map<String,Object>> getClazzStus(String clazzName){
return schoolService.getStusByClazzName(clazzName);
}
}
測試應用