[英]Mapping raw sql query into DTO object Spring
SPRING BOOT , JAVA 11 , POSTGRES弹簧靴 , JAVA 11 , POSTGRES
I have a big SQL query:我有一个很大的 SQL 查询:
select c.id,c.image_url,c.status,c.name, cat.id,cat.name
from client_organization o join organization c
on o.organization_id = c.id
join client w on o.client_id = w.id
join org_category cat on c.category_id = cat.id
where w.id = ?
So i need to convert the given result to List of Data Transfer Objects(DTO).所以我需要将给定的结果转换为数据传输对象列表(DTO)。 If it is important, here is my DTO(constructor, getters and setters are not included):如果这很重要,这是我的 DTO(不包括构造函数、getter 和 setter):
public class OrganizationListDto {
Long id;
String image;
Boolean status;
String name;
Long categoryId;
String categoryName;
}
I have been looking for solution for some days and found that this operation can be implemented in different ways(jdbc, hibernate etc) So please explain me what and when I need to use them?我一直在寻找解决方案几天,发现这个操作可以通过不同的方式实现(jdbc、hibernate等)所以请解释我什么时候需要使用它们? How to implement this operation?如何实现这个操作?
Follow the below approach have used spingJpa
@Service public class OrganizationServiceImpl @Service 公共类 OrganizationServiceImpl
{
@Autowired
OrganizationListRepository organizationListRepository;
public OrganizationListDto fetch(usrId)
{
OrganizationListDto orgListData = new OrganizationListDto();
List<OrganizationListMapping> orgData = organizationListRepository.fetchData(usrId);
BeanUtils.copy(orgData,orgListData)
return orgData;
}
}
@Repository public interface OrganizationListRepository extends JpaRepository{ @Repository 公共接口 OrganizationListRepository 扩展 JpaRepository{
@Query(native="true",value="select cat.id as id,cat.name as name
from client_organization o join organization c
on o.organization_id = c.id
join client w on o.client_id = w.id
join org_category cat on c.category_id = cat.id
where w.id = usrId"
OrganizationListMapping fetchData(@Param ("usrId") Integer usrId);
}
public interface OrganizationListMapping {
public Long getId();
public String getName();
}
public class OrganizationListDto {
Long id;
String name;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.