mybatis- 3.1.1.如何覆盖mybatis返回的结果映射 [英] mybatis- 3.1.1. how to override the resultmap returned from mybatis

查看:156
本文介绍了mybatis- 3.1.1.如何覆盖mybatis返回的结果映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用mybatis 3.1.1.

we using mybatis 3.1.1.

对于oracle,我们发现返回的结果图包含以大写字母表示的列名,对于mySql,返回的结果图包含以小写字母表示的列名.

we found for oracle the result map returned contains column name in Capital letters and in case of mySql the result map returned contains column name in small letters.

我的问题是:有什么办法可以编写某种拦截器,以便修改结果图返回的结果.

My question is : Is there is any way i can to write some sort of interceptor so that i can modify the result returned by result map.

谢谢.

推荐答案

恐怕答案是MyBatis不提供任何直接方法来控制结果图中键的大小写.我最近在MyBatis Google网上论坛上问了这个问题: https://groups.google.com/forum/?fromgroups#!topic/mybatis-user/tETs_JiugNE

I'm afraid the answer is that MyBatis doesn't provide any direct way to control the case of the keys in a result map. I asked this question recently on the MyBatis Google Group: https://groups.google.com/forum/?fromgroups#!topic/mybatis-user/tETs_JiugNE

结果取决于JBDC驱动程序的行为.

The outcome is dependent on the behavior of the JBDC driver.

还发现,@ jddsantaella建议的列别名并非在所有情况下都有效.过去,我已经用三个数据库(MySQL,PostgreSQL和H2)测试了MyBatis-3.1.1,并得到了不同的答案.对于MySQL,列别名的大小写确实决定了哈希图中键的大小写.但是对于PostgreSQL,它始终是小写字母;对于H2,它始终是大写字母.我没有测试列别名是否适用于Oracle,但是默认情况下它似乎返回大写字母.

It also turns out that doing column aliasing as suggested by @jddsantaella doesn't work in all cases. I've tested MyBatis-3.1.1 with three databases in the past: MySQL, PostgreSQL and H2 and got different answers. With MySQL, the case of the column alias does dictate the case of the key in the hashmap. But with PostgreSQL it is always lowercase and with H2, it is always uppercase. I didn't test whether column aliases will work with Oracle, but by default it appears to return capital letters.

我看到两个选项:

选项1 :创建一些辅助方法,您的代码将始终使用该方法将数据从返回的地图中拉出.例如:

Option 1: Create some helper method that your code will always use to pull the data out of the returned map. For example:

private Object getFromMap(Map<String, Object> map, String key) {
  if (map.containsKey(key.toLowerCase())) {
    return map.get(key.toLowerCase());
  } else {
    return map.get(key.toUpperCase());
  }
}


选项2:编写一个从java.util.AbstractMapjava.util.HashMap扩展的LowerCaseMap类,并将所有对putputAll和/或get的调用包装为小写.然后,指定MyBatis在填充查询中的数据时应使用特定的LowerCaseMap而不是标准的HashMap.


Option 2: Write a LowerCaseMap class that extends from java.util.AbstractMap or java.util.HashMap and wrappers all calls to put, putAll and/or get to always be lower case. Then specify that MyBatis should use your specific LowerCaseMap rather than a standard HashMap, when populating the data from the query.

如果您喜欢这个想法,并希望获得有关如何告诉MyBatis如何使用其他具体集合类的帮助,请参阅我对StackOverflow问题的回答:

If you like this idea and want help on how to tell MyBatis how to use a different concrete collection class, see my answer to this StackOverflow question: https://stackoverflow.com/a/11596014/871012

这篇关于mybatis- 3.1.1.如何覆盖mybatis返回的结果映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆