欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis 返回Map類型key改為小寫的操作

 更新時間:2020年12月02日 09:18:13   作者:caixiajia  
這篇文章主要介紹了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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論