💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
多对多查询可以用注解`@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; } ```