🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
多对多查询可以用注解`@ManyToMany`来完成,默认 sql 语句。 ```sql from t1 left join (tb_mid join t2 on ...) on ... -- t1 主表 -- tb_mid 中间表 -- t2 从表 ``` ```java public class Assort implements Serializable { /** * assortList:对应 Book#assortList 变量名称. */ @ManyToMany(mappedBy = "assortList", fetch = FetchType.EAGER) private List<Book> bookList; } ``` ```java public class Book implements Serializable { @ManyToMany(targetEntity = Assort.class, fetch = FetchType.LAZY) @JoinTable( //多对多查询将会添加一张中间表,该中间表SpringDataJpa会自动创建 name = "tb_assort_book_mid", //book_id 对应 Book#id 主键变量名,将会在中间表中存储 book_id 物理外键 joinColumns = {@JoinColumn(name = "book_id", referencedColumnName = "id")}, //assort_id 对应 Assort#id 主键变量名,将会在中间表中存储 assort_id 物理外键 inverseJoinColumns = {@JoinColumn(name = "assort_id", referencedColumnName = "id")} ) private List<Assort> assortList; } ```