JeePlus--Mybatis返回map数据转换成json

本文展示了如何在使用JeePlus的Mybatis查询中,将查询结果HashMap转换为JSON对象,具体涉及到DAO、Service、Controller层的处理,并在JSP中应用。Controller通过遍历查询结果,构建JSON字符串供前端使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybatis:
<select id="cb" resultType="java.util.HashMap">
    SELECT VALUE,LABEL
    FROM SYS_DICT
    WHERE type=#{parm}
</select>

Dao:
public List<Map<String, String>> cb(@Param(value="parm")String parm);

Service:
public  List<Map<String, String>> cb(String parm){
    return super.dao.cb(parm);
}

Controller:
@ResponseBody
@RequestMapping(value = "cb")
public String cb(String parm) {
   List<Map<String, String>> regionMap = vTNMService.cb(parm);
   String jsonReasult = "[";
   String jsonReasult1 = null, jsonReasult2 = null;
   boolean isAdd = false;
   for (Map<String, String> map : regionMap) {
      for (Map.Entry<String, String> entry : map.entrySet()) {
         if ("VALUE".equals(entry.getKey())) {
            jsonReasult1 = "{\"id\":\"" + entry.getValue() + "\",";
         } else if ("LABEL".equals(entry.getKey())) {
            jsonReasult2 = "\"text\":\"" + entry.getValue() + "\"},";
         }
         if (isAdd) {
            jsonReasult += jsonReasult1 + jsonReasult2;
            isAdd = false;
         } else {
            isAdd = true;
         }
      }
   }
   jsonReasult = jsonReasult.substring(0, jsonReasult.length() - 1);
   jsonReasult += "]";
   return jsonReasult;
}

Jsp:
    editor: {
              type: 'combobox',
              options: {
                         url: '${ctx}/attdef/sqe/cb',
                          multiple: false,
                          valueField: 'id',
                          textField: 'text'
                       }
           }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值