用ibatis 調用 sqlmapclient 來去除數據庫元數據的大小寫問題

剛接收一個項目,項目原先是基於oracle的,項目裏面有一個webservice 直接經過sqlmapclient 來獲取數據庫的,可是oracle ,經過ibatis調用取得的map,元數據都是大寫的,如今項目要支持多種數據庫,例如MySQL、postgresql等數據庫,元數據是小寫的,經過參考spring框架,採用map進行轉換成 LinkedCaseInsensitiveMap 來實現,原先框架用xfire實現的webservice,想直接引入spring的包,發現有衝突,後面把這個LinkedCaseInsensitiveMap的源媽引入,解決了。java

@SuppressWarnings("serial")
public class LinkedCaseInsensitiveMap<V> extends LinkedHashMap<String, V> {web

 private final Map<String, String> caseInsensitiveKeys;spring

 private final Locale locale;sql


 /**
  * Create a new LinkedCaseInsensitiveMap for the default Locale.
  * @see java.lang.String#toLowerCase()
  */
 public LinkedCaseInsensitiveMap() {
  this(null);
 }數據庫

 /**
  * Create a new LinkedCaseInsensitiveMap that stores lower-case keys
  * according to the given Locale.
  * @param locale the Locale to use for lower-case conversion
  * @see java.lang.String#toLowerCase(java.util.Locale)
  */
 public LinkedCaseInsensitiveMap(Locale locale) {
  super();
  this.caseInsensitiveKeys = new HashMap<String, String>();
  this.locale = (locale != null ? locale : Locale.getDefault());
 }oracle

 /**
  * Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
  * with the given initial capacity and stores lower-case keys according
  * to the default Locale.
  * @param initialCapacity the initial capacity
  * @see java.lang.String#toLowerCase()
  */
 public LinkedCaseInsensitiveMap(int initialCapacity) {
  this(initialCapacity, null);
 }框架

 /**
  * Create a new LinkedCaseInsensitiveMap that wraps a {@link LinkedHashMap}
  * with the given initial capacity and stores lower-case keys according
  * to the given Locale.
  * @param initialCapacity the initial capacity
  * @param locale the Locale to use for lower-case conversion
  * @see java.lang.String#toLowerCase(java.util.Locale)
  */
 public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) {
  super(initialCapacity);
  this.caseInsensitiveKeys = new HashMap<String, String>(initialCapacity);
  this.locale = (locale != null ? locale : Locale.getDefault());
 }ide


 @Override
 public V put(String key, V value) {
  String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
  if (oldKey != null && !oldKey.equals(key)) {
   super.remove(oldKey);
  }
  return super.put(key, value);
 }post

 @Override
 public void putAll(Map<? extends String, ? extends V> map) {
  if (map.isEmpty()) {
   return;
  }
  for (Map.Entry<? extends String, ? extends V> entry : map.entrySet()) {
   put(entry.getKey(), entry.getValue());
  }
 }this

 @Override
 public boolean containsKey(Object key) {
  return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
 }

 @Override
 public V get(Object key) {
  if (key instanceof String) {
   return super.get(this.caseInsensitiveKeys.get(convertKey((String) key)));
  }
  else {
   return null;
  }
 }

 @Override
 public V remove(Object key) {
  if (key instanceof String ) {
   return super.remove(this.caseInsensitiveKeys.remove(convertKey((String) key)));
  }
  else {
   return null;
  }
 }

 @Override
 public void clear() {
  this.caseInsensitiveKeys.clear();
  super.clear();
 }

 /**  * Convert the given key to a case-insensitive key.  * <p>The default implementation converts the key  * to lower-case according to this Map's Locale.  * @param key the user-specified key  * @return the key to use for storing  * @see java.lang.String#toLowerCase(java.util.Locale)  */ protected String convertKey(String key) {  return key.toLowerCase(this.locale); }}

相關文章
相關標籤/搜索