💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**1. 实体类建立关系** ```java public class Country { //country 对应 Province.country 属性名 @OneToMany(mappedBy = "country", fetch = FetchType.EAGER) private List<Province> provinceList; } ``` ```java public class Province { //Country -> Province 是一对多,反过来 Province -> Country 就是多对一 @ManyToOne(targetEntity = Country.class) //country_id 对应 Country.id 属性名 //将会在 Province 表中建立 country_id 的物理外键 @JoinColumn(name = "country_id", referencedColumnName = "id") private Country country; } ``` **2. @Query多表查询** ```java /** * HSQL查询 */ @Query("select country from Country country left join Province province " + "on country.id=province.country.id where country.continent=?1 and province.level=?2") List<Country> findAll01(String continent, Integer level); /** * SQL查询 */ @Query(value = "select c.* from country c left join province p " + "on c.id=p.country_id where c.continent=?1 and p.level=?2", nativeQuery = true) List<Country> findAll02(String continent, Integer level); ``` ```java /** * HSQL查询 */ @Query("select province from Province province left join Country country " + "on province.country.id=country.id where province.level=?1 and country.name=?2") List<Province> findAll01(Integer level, String name); /** * SQL查询 */ @Query(value = "select p.* from province p left join country c " + "on p.country_id=c.id where p.level=?1 and c.name=?2", nativeQuery = true) List<Province> findAll02(Integer level, String name); ```