• JPA


    前言

    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 -
    梦想是咸鱼
    关注一下吧
    以上为本篇文章的主要内容,希望大家多提意见,如果喜欢记得点个推荐哦
    作者:Maggieq8324
    本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。
  • 相关阅读:
    Spring
    sikuli常用方法学习
    运行测试Caused by: java.lang.UnsatisfiedLinkError: no attach in java.library.path错误解决
    sikuli+java实例
    sikuli运行出现问题:Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform
    官网下载jdk
    java:jdk环境变量配置+tomcat环境变量配置
    Redis能干啥?细看11种Web应用场景
    计数场景的优化
    国内外三个领域巨头告诉你Redis怎么用
  • 原文地址:https://www.cnblogs.com/maggieq8324/p/15166419.html
Copyright © 2020-2023  润新知