我有那些实体
@Entity
@Data
@Table(name = "CITY")
public class CityEntity {
@Id private int id;
private String name;
private String postcode;
@OneToMany(fetch = FetchType.LAZY) @JoinColumn(name="ID_CITY_FK")
private List<SchoolEntity> schools;
}
字符串
和
@Entity
@Data
@Table(name = "SCHOOL")
public class SchoolEntity {
@Id private int id;
private String name;
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="ID_CITY_FK")
private CityEntity city;
}
型
我想根据学校名称进行选择,并输出如下所示
[
{
"id": 1,
"name": "Paris",
"postcode": "75000",
"schools": [
{
"id": 10,
"name": "Java School 1"
},
{
"id": 11,
"name": "Java School 2"
}
]
},
{
"id": 2,
"name": "Brussels",
"postcode": "1000",
"schools": [
{
"id": 12,
"name": "Java School 3"
},
{
"id": 13,
"name": "Java School 4"
}
]
}
]
型
我认为唯一的办法是从学校实体开始
public interface SchoolEntityRepository extends JpaRepository<SchoolEntity, Integer> {
List<SchoolEntity> findByNameContaining(String name);
}
型
然后用我的控制器我就可以检索学校
@GetMapping("/from-school")
public List<City> fromSchool2() {
List<SchoolEntity> schoolEntities = schoolEntityRepository.findByNameContaining("Java");
return ...;
}
型
我也是一个MapstructMap器
@Mapper
public interface CityEntityMapper {
CityEntityMapper INSTANCE = Mappers.getMapper(CityEntityMapper.class);
List<City> map(List<SchoolEntity> schools);
}
型
但是...?我不知道该怎么做?
1条答案
按热度按时间unhi4e5o1#
使用SchoolEntity Repository根据学校名称获取SchoolEntity示例。
字符串
按关联的CityEntity对学校进行分组
型
使用MapStruct将结果数据结构Map到所需的DTO结构
型
DTO:
型