用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);
}
}
刚接收一个项目,项目原先是基于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);
}
}
相关文章推荐
- 使用powerdesigner建模
- MySQL的redo log结构和SQL Server的log结构对比
- 数据库连接池配置 - Oracle,SQL Server,DB2,MYSQL,SQLLITE3
- sql server 使用nolock提升性能
- SQL 在查询结果中增加标记字段
- linux下memcache的运用,和php结合小案例。
- mybatis+mysql返回插入值后的主键id
- 三种方法链接MySQL
- 可视化配置以及Net应用MemCache在win7
- 数据库优化
- oracle对象
- Mysql安装及1067错误相关
- MySQL优化---DBA对MySQL优化的一些总结
- MySQL数据类型和常用字段属性总结
- Sql 中 不等于'<>'与 NULL
- 数据库中索引的优缺点
- mysql 架构分层
- mysql 5.5 手册目录
- oracle 数据备份之exp优化
- mysql到底用哪一款存储引擎呢