gpt4 book ai didi

java - 在java spring boot应用程序中将projectionlist转换为json格式

转载 作者:行者123 更新时间:2023-12-01 09:32:01 25 4
gpt4 key购买 nike

如何在 spring 中将投影列表转换为 json 格式?

    Session session = getSession();
Criteria criteria = session.createCriteria(StudentFeedback.class);
ProjectionList projlist =Projections.projectionList();
projlist.add(Projections.property("feedbackStatus"));
projlist.add(Projections.property("totalFeedback"));
criteria.setProjection(projlist);

我希望获取的数据库数据如下所示

{"feedName":value,"qty":value}

最佳答案

您可以按以下方式使用:query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP),

    String HQL_QUERY = "select comp.id as comp_id, comp.name as comp_name from Company comp";
Query query = session.createQuery(HQL_QUERY);
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

List<?> rows = query.list();
System.out.println("Selected row count : " + rows.size());

for (Object row : rows) {
System.out.println(row); //Each row is a map of properties in the query
//with key as alias and property as value
}

引用点击here

关于java - 在java spring boot应用程序中将projectionlist转换为json格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39329946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com