JPA - JPQL完成复杂的多表查询

前言

JPA中可以通过设置实体的导航属性 + JPQL完成复杂的多表查询,简化SQL的编写。


示例

建表语句

  • Table
CREATE TABLE `school` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

CREATE TABLE `student` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

CREATE TABLE `school_student` (
  `id` int(11) DEFAULT NULL,
  `school_id` int(11) DEFAULT NULL,
  `student_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  • 上述表中Studen和School呈现多对多关系

Entity实体

  • School.java
@Entity
@Setter
@Getter
public class School {

    @Id
    private Integer id;
    private String name;

    /**
     * 多对多关系配置
     */
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "schoolList")
    private List<Student> studentList;

}
  • Student.java
@Entity
public class Student {

    @Id
    private Integer id;
    private String name;

    /**
     * 多对多关系配置
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "school_student",
            joinColumns = @JoinColumn(name = "student_id"),
            inverseJoinColumns = @JoinColumn(name = "school_id"))
    private List<School> schoolList;

}

JPQL多表查询

  • 在这里,通过Studen ID 查询出关联的 School
  • 常规SQL关联查询:
select
        sch.id ,
        sch.name 
    from
        school sch
    inner join
        school_student ss 
            on sch.id = ss.school_id 
    inner join
        student stu 
            on ss.student_id = stu.id 
    where
        stu.id = 1
  • JPQL多表查询:
@Query("select s from School s " +
            "join s.studentList ss " +
            "where ss.id = :id ")

完整代码

  • TestController.java
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping(value = "/test")
    public List<Student> test(@RequestParam Integer id){

        return studentRepository.findStudenBySchool(id);
    }

}
  • StudentRepository.java
@Repository
public interface StudentRepository extends JpaRepository<School, Long> {

    @Query("select s from School s " +
            "join s.studentList ss " +
            "where ss.id = :id ")
    List<Student> findStudenBySchool(Integer id);

}

调用结果

在这里插入图片描述


- End -
梦想是咸鱼
关注一下吧
posted @ 2021-08-20 14:49  Maggieq8324  阅读(475)  评论(0编辑  收藏  举报