zoukankan      html  css  js  c++  java
  • 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
    本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。
  • 相关阅读:
    Paxos算法理解
    JavaScript笔记
    JVM基础知识(二)
    JVM基础知识(一)
    书单
    centos6.3环境下升级python及MySQLdb的安装
    winform中button的image属性无法更改
    未能找到任何适合于指定的区域性或非特定区域性的资源
    Spring注解开发第十二讲--@Profile注解讲解
    Spring注解驱动第十一讲--引用Spring底层组件
  • 原文地址:https://www.cnblogs.com/maggieq8324/p/15166419.html
Copyright © 2011-2022 走看看