mybatis 返回Map類型key改為小寫的操作
默認情況下,當resultType=“java.util.Map”時,返回的key值都是大寫的。
現(xiàn)在想key改成自己想要的,只需為查詢出來的字段增加個別名即可。
如:
<select id="getStudentList" resultType="java.util.Map"> select t.name as "sName",t.sex as "sSex" from student </select>
as 后的雙引號很關鍵,否則不起作用。
補充知識:mybatis返回map key值大小寫去重,CaseInsensitiveMap、LinkedCaseInsensitiveMap源碼
今天在寫項目的時候遇見一個問題:
編寫的是一套完全解耦的模塊,用于利用freemarker模板動態(tài)拼接sql
然而拼接好的sql只能用LinkedHashMap返回結(jié)果集,保證數(shù)據(jù)有序,但是在數(shù)據(jù)輸出的時候,mybatis 返回了key 相同,但大小寫不同的數(shù)據(jù),
在處理這個數(shù)據(jù)的時候,首先我選用了利用value值相同去做處理,但是有些數(shù)據(jù)的value值相同但是key不同
看來只能用key去做處理,首先我用了CaseInsensitiveMap 將linkedHashMap的數(shù)據(jù)放入到CaseInsensitiveMap 中,
輸出的時候結(jié)果確實起到了去重的效果,但是在excel導出的時候要對應header標題頭,mybatis 查詢的順序跟header 抬頭是對應的,但是取出的順序卻不是對應的,
因此只能通過排序進行數(shù)據(jù)對應,但是排序的話只能使用LinkedHashMap去記住順序
因此查詢了資料發(fā)現(xiàn)了LinkedCaseInsensitiveMap
LinkedCaseInsensitiveMap 繼承了 LinkedHashMap,可以檢測關鍵字(不區(qū)分大小寫)的唯一性,所以 ok bug完美解決
附加 LinkedCaseInsensitiveMap 源碼
package org.springframework.util; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; public class LinkedCaseInsensitiveMap<V> extends LinkedHashMap<String, V> { private Map<String, String> caseInsensitiveKeys; private final Locale locale; public LinkedCaseInsensitiveMap() { this((Locale)null); } public LinkedCaseInsensitiveMap(Locale locale) { this.caseInsensitiveKeys = new HashMap(); this.locale = locale != null?locale:Locale.getDefault(); } public LinkedCaseInsensitiveMap(int initialCapacity) { this(initialCapacity, (Locale)null); } public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) { super(initialCapacity); this.caseInsensitiveKeys = new HashMap(initialCapacity); this.locale = locale != null?locale:Locale.getDefault(); } public V put(String key, V value) { String oldKey = (String)this.caseInsensitiveKeys.put(this.convertKey(key), key); if(oldKey != null && !oldKey.equals(key)) { super.remove(oldKey); } return super.put(key, value); } public void putAll(Map<? extends String, ? extends V> map) { if(!map.isEmpty()) { Iterator var2 = map.entrySet().iterator(); while(var2.hasNext()) { Entry entry = (Entry)var2.next(); this.put((String)entry.getKey(), entry.getValue()); } } } public boolean containsKey(Object key) { return key instanceof String && this.caseInsensitiveKeys.containsKey(this.convertKey((String)key)); } public V get(Object key) { if(key instanceof String) { String caseInsensitiveKey = (String)this.caseInsensitiveKeys.get(this.convertKey((String)key)); if(caseInsensitiveKey != null) { return super.get(caseInsensitiveKey); } } return null; } public V getOrDefault(Object key, V defaultValue) { if(key instanceof String) { String caseInsensitiveKey = (String)this.caseInsensitiveKeys.get(this.convertKey((String)key)); if(caseInsensitiveKey != null) { return super.get(caseInsensitiveKey); } } return defaultValue; } public V remove(Object key) { if(key instanceof String) { String caseInsensitiveKey = (String)this.caseInsensitiveKeys.remove(this.convertKey((String)key)); if(caseInsensitiveKey != null) { return super.remove(caseInsensitiveKey); } } return null; } public void clear() { this.caseInsensitiveKeys.clear(); super.clear(); } public Object clone() { LinkedCaseInsensitiveMap copy = (LinkedCaseInsensitiveMap)super.clone(); copy.caseInsensitiveKeys = new HashMap(this.caseInsensitiveKeys); return copy; } protected String convertKey(String key) { return key.toLowerCase(this.locale); } }
以上這篇mybatis 返回Map類型key改為小寫的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot集成Jpa對數(shù)據(jù)進行排序、分頁、條件查詢和過濾操作
這篇文章主要介紹了SpringBoot集成Jpa對數(shù)據(jù)進行排序、分頁、條件查詢和過濾操作,主要使用Jpa連接數(shù)據(jù)庫對數(shù)據(jù)進行排序、分頁、條件查詢和過濾操作,需要的朋友可以參考下2023-05-05詳解Spring MVC如何測試Controller(使用springmvc mock測試)
這篇文章主要介紹了詳解Spring MVC如何測試Controller(使用springmvc mock測試),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12