3. 如何封装查询条件与查询结果到map中

时间:2024-11-10 20:36:26
     public Map<String, Object> queryOrderStatus(String orderNo) { // 查询到的结果与查询的条件一一对应,封装到map中!
String[] fields = {
"salesman", "no", "status", "VATInvoiceNo", "courier", "tracking", "forAR", "mark"
, "dunning_courier", "dunning_tracking"
};
Query query = entityManager.createNativeQuery("select " + StringUtils.join(fields, ",") +
"\n from selleck.selleck_order o" +
"\n left join selleck.selleck_order_extended e on e.orderId=o.id" +
"\n where no=:orderNo"
);
query.setParameter("orderNo", orderNo);
List<Object[]> list = query.getResultList();
if (list.isEmpty()) {
return new HashMap<>();
} else {
Object[] values = list.get(0);
Map<String, Object> valueMap = new HashMap<>();
for (int i = 0; i < fields.length; i++) {
valueMap.put(fields[i], values[i]);
}
return valueMap;
}
}