阅读全文,约 9 分钟
这是江帅帅的第012篇原创
案例3:NamedQuery 查询
使用 NameQuery 就是一个名称映射一个查询语句的查询操作。
1)编辑 pom.xml 文件(与 CrudRepository 接口案例一样)
2)创建 Student 和 Clazz 持久化类
先看 Student 类。
@Entity @Table(name="tb_student") // 查询班级下的学生 @NamedQuery(name="Student.findStudentsByClazzName",query="select s from Student s where s.clazz.name = ?1") public class Student implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name ; private String address ; private int age ; private char sex; // 学生与班级是多对一的关系,这里配置的是双向关联 @ManyToOne(fetch=FetchType.LAZY, targetEntity=Clazz.class ) @JoinColumn(name="clazzId",referencedColumnName="code") private Clazz clazz ; public Student() { } public Student(String name, String address, int age, char sex, Clazz clazz) { super(); this.name = name; this.address = address; this.age = age; this.sex = sex; this.clazz = clazz; } // setXxx 和 getXxx 方法 }
再来看看 Clazz 类。
@Entity @Table(name="tb_clazz") public class Clazz implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int code ; private String name ; // 班级与学生是一对多的关联 @OneToMany( fetch=FetchType.LAZY, targetEntity=Student.class, mappedBy="clazz" ) private Set<Student> students = new HashSet<>(); public Clazz() { } // 班级对象 public Clazz(String name) { this.name = name; } // setXxx 和 getXxx 方法 }
3)创建 StudentRepository 和 ClazzRepository 数据访问接口
先看 StudentRepository 类。
public interface StudentRepository extends JpaRepository<Student, Integer> { /** * 查询班级下的所有学生 */ List<Student> findStudentsByClazzName(String clazzName); }
再看 ClazzRepository 类。
public interface ClazzRepository extends JpaRepository<Clazz, Integer> { }
4)创建 ShcoolService 业务层
@Service public class ShcoolService { // 注入数据访问层接口对象 @Resource private StudentRepository studentRepository; @Resource private ClazzRepository clazzRepository; @Transactional public void saveClazzAll(List<Clazz> clazzs) { clazzRepository.saveAll(clazzs); } @Transactional public void saveStudentAll(List<Student> students) { studentRepository.saveAll(students); } public List<Map<String, Object>> getStusByClazzName(String clazzName) { // 查询班级下的所有学生 List<Student> students = studentRepository.findStudentsByClazzName(clazzName); List<Map<String, Object>> results = new ArrayList<>(); // 遍历查询出的学生对象,提取姓名,年龄,性别信息 for(Student student:students){ Map<String , Object> stu = new HashMap<>(); stu.put("name", student.getName()); stu.put("age", student.getAge()); stu.put("sex", student.getSex()); results.add(stu); } return results; } }
5)创建 StudentController 控制器类
@RestController @RequestMapping("/student") public class StudentController { @Resource private ShcoolService shcoolService; @RequestMapping("/save") public String save() { Clazz clazz1 = new Clazz("架构师001班"); Clazz clazz2 = new Clazz("架构师002班"); // 保存班级对象数据 List<Clazz> clazzs = new ArrayList<>(); clazzs.add(clazz1); clazzs.add(clazz2); shcoolService.saveClazzAll(clazzs); Student s1 = new Student("帅帅","广州",18,'男',clazz1); Student s2 = new Student("小黄","广州",17,'女',clazz1); Student s3 = new Student("小红","广州",15,'男',clazz2); List<Student> students = new ArrayList<>(); students.add(s1); students.add(s2); students.add(s3); shcoolService.saveStudentAll(students); return "保存学生成功"; } /** * 查询某个班级下所有的学生姓名,年龄,性别 */ @RequestMapping("/getClazzStus") public List<Map<String, Object>> getClazzStus(String clazzName){ return shcoolService.getStusByClazzName(clazzName); } }
6)测试
http://localhost:8080/student/save