mybatis返回類型map時如何將key大寫轉為小寫

親測:

SELECT DEPTNO as "deptno",DEPTNAME,DEPTGRADE,PARENTDEPT 
      FROM VMGR_DEPT
      ORDER BY DEPTGRADE,DEPTNO

別人案例:

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

純java實現方法(推薦):

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返回的全是大寫

解決方法

在查詢的字段後加別名

user_id as “userId”

註意:別名必須加雙引號

<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>

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: