gpt4 book ai didi

java - 如何使用 JPA native 查询将 List 转换为 POJO
转载 作者:行者123 更新时间:2023-12-02 09:59:55 26 4
gpt4 key购买 nike

如何将对象List转换为josn(key,value)格式?

我有一个名为 ClientRT 的类,该类中有四个字段,即

res_nclient_room_type_id, res_sclient_rt_desc,res_sclient_rt_name, res_sclient_rt_code

服务等级

public List<Object> callSP() throws IOException {   

List<Object> crt=crtRepo.roomtype(60);

//ObjectMapper mapper = new ObjectMapper();

//String rtobject= mapper.writeValueAsString(crt);

return crt;
}

存储库

@Repository
public interface ClientRoomTypeRepository extends JpaRepository<ClientRoomType, Integer> {

@Query(value = "select * from roomtype(:int_inst_id)", nativeQuery = true)
List<Object> roomtype(@Param("int_inst_id")Integer int_inst_id);
}

对象列表

 [
[
1,
"TEMPORARILY NOT ASSIGNED",
"TEMPORARILY NOT ASSIGNED",
"000"
],
[
2,
"FACILITIES - AVAILABLE ROOM",
"FACILITIES - AVAILABLE ROOM",
"050"
],

如何转换成这种格式

[
{
"res_nclient_room_type_id":1 ,
"res_sclient_rt_desc": "TEMPORARILY NOT ASSIGNED",
"res_sclient_rt_name":"TEMPORARILY NOT ASSIGNED" ,
"res_sclient_rt_code":"000"

},
{
"res_nclient_room_type_id":2 ,
"res_sclient_rt_desc": "FACILITIES - AVAILABLE ROOM",
"res_sclient_rt_name":"FACILITIES - AVAILABLE ROOM" ,
"res_sclient_rt_code":"050"
},
]

有人可以建议我如何做到这一点吗?

最佳答案

我发现您的问题是 JPA 使用投影来执行此操作,创建一个带有字段 Interface-based Projections 的界面

interface ClientRT {

Long getResNclientRoomTypeId();
String getResSclientRtDesc();
String getResSclientRtName();
String getResSclientRtCode();
}

查询

 @Query(value = "select * from roomtype(:int_inst_id)", nativeQuery = true)
List<ClientRT> roomtype(@Param("int_inst_id")Integer int_inst_id);

关于java - 如何使用 JPA native 查询将 List<Object> 转换为 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55711283/

26 4 0