mybatis返回类型map时如何将key大写转为小写

时间:2022-08-30 20:14:21

亲测:

?
1
2
3
SELECT DEPTNO as "deptno",DEPTNAME,DEPTGRADE,PARENTDEPT
      FROM VMGR_DEPT
      ORDER BY DEPTGRADE,DEPTNO

mybatis返回类型map时如何将key大写转为小写

别人案例:

?
1
2
3
<select id="selectBlogRetHashMap" parameterType="int" resultType="map"
      SELECT id AS "id", title AS "title", content AS "content" FROM Blog WHERE id = #{id} 
  </select>

纯java实现方法(推荐):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Snippet {
    public static Map<String, Object> transformUpperCase(Map<String, Object> orgMap) {
        Map<String, Object> resultMap = new HashMap<>();
 
        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }
 
        Set<String> keySet = orgMap.keySet();
        for (String key : keySet) {
            String newKey = key.toLowerCase();
            newKey = newKey.replace("_", "");
 
            resultMap.put(newKey, orgMap.get(key));
        }
 
        return resultMap;
    }
}

mybatis映射map返回的全是大写

解决方法

在查询的字段后加别名

?
1
user_id as “userId”

注意:别名必须加双引号

?
1
2
3
4
5
6
7
8
9
<select id="queryMaxPriceAAndUser" parameterType="java.lang.String" resultType="java.util.HashMap">
    SELECT A.SELL_PRICE as "price", B.USER_ID as "userId"
      FROM QLYY_SELL_AUCTION_RECORD A, QLYY_SELL_APPLY B
     WHERE A.SELL_PRICE = (SELECT MAX(SELL_PRICE)
                             FROM QLYY_SELL_AUCTION_RECORD
                            WHERE SELL_PROGRAM_ID = #{programId})
       AND A.SELL_PROGRAM_ID = #{programId}
       AND B.APPLY_CODE = A.SELL_USER
  </select>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_36421955/article/details/80847018