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 后的雙引號很關(guān)鍵,否則不起作用。
補充知識: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導(dǎo)出的時候要對應(yīng)header標題頭,mybatis 查詢的順序跟header 抬頭是對應(yīng)的,但是取出的順序卻不是對應(yīng)的,
因此只能通過排序進行數(shù)據(jù)對應(yīng),但是排序的話只能使用LinkedHashMap去記住順序
因此查詢了資料發(fā)現(xiàn)了LinkedCaseInsensitiveMap
LinkedCaseInsensitiveMap 繼承了 LinkedHashMap,可以檢測關(guān)鍵字(不區(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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot2.0整合jackson配置日期格式化和反序列化的實現(xiàn)
這篇文章主要介紹了SpringBoot2.0整合jackson配置日期格式化和反序列化的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
這篇文章主要介紹了詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Java Quartz觸發(fā)器CronTriggerBean配置用法詳解
這篇文章主要介紹了Java Quartz觸發(fā)器CronTriggerBean配置用法詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
Spring Cloud EureKa Ribbon 服務(wù)注冊發(fā)現(xiàn)與調(diào)用
這篇文章主要介紹了Spring Cloud EureKa Ribbon 服務(wù)注冊發(fā)現(xiàn)與調(diào)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
SpringData整合ElasticSearch實現(xiàn)CRUD的示例代碼(超詳細)
本文主要介紹了SpringData整合ElasticSearch實現(xiàn)CRUD的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-07-07

