用ibatis 调用 sqlmapclient 来去除数据库元数据的大小写问题

刚接收一个项目,项目原先是基于oracle的,项目里面有一个webservice 直接通过sqlmapclient 来获取数据库的,但是oracle ,通过ibatis调用取得的map,元数据都是大写的,现在项目要支持多种数据库,例如MySQL、postgresql等数据库,元数据是小写的,通过参考spring框架,采用map进行转换成 LinkedCaseInsensitiveMap 来实现,原先框架用xfire实现的webservice,想直接引入spring的包,发现有冲突,后面把这个LinkedCaseInsensitiveMap的源妈引入,解决了。

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

 private final Map<String, String> caseInsensitiveKeys;

 private final Locale locale;


 /**
  * 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());
 }

 /**
  * 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());
 }


 @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);
 }

 @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());
  }
 }

 @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);
 }
}

转载于:https://my.oschina.net/u/228832/blog/497396

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值