您的位置:首页 > 数据库

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

2015-08-26 00:00 405 查看
摘要: ibatis、oracle、mysql

刚接收一个项目,项目原先是基于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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: