Java 放入 <result> 标签时未执行 MyBatis 自定义 TypeHandler
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20122157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MyBatis custom TypeHandler not executed when put in <result> tag
提问by Micha? Rybak
I have a <resultMap>
with custom typeHandler
for one of the result properties:
我对结果属性之一有一个<resultMap>
自定义typeHandler
:
<resultMap id="foo" type="hashmap">
...
<result property="SERVICES_XML" javaType="string" jdbcType="CLOB" typeHandler="com.foo.bar.OracleClobTypeHandler" />
...
</resultMap>
No matter which property I attach my handler to (I mean this is not CLOB-specific issue, tried with VARCHAR
, too), the handler won't get called when I fetch results from database.
无论我将处理程序附加到哪个属性(我的意思是这不是特定于 CLOB 的问题,也尝试VARCHAR
过),当我从数据库中获取结果时,处理程序都不会被调用。
I have set breakpoints in all methods of my custom handler:
我在自定义处理程序的所有方法中设置了断点:
public class OracleClobTypeHandler implements TypeHandler<String> {
@Override
public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
log.debug("setParameter called"); <================ BREAKPOINT HERE
}
@Override
public String getResult(ResultSet rs, String columnName)
throws SQLException {
log.debug("getResult 2 called"); <================ BREAKPOINT HERE
return "";
}
@Override
public String getResult(ResultSet rs, int columnIndex)
throws SQLException {
log.debug("getResult 2 called"); <================ BREAKPOINT HERE
return "";
}
@Override
public String getResult(CallableStatement cs, int columnIndex)
throws SQLException {
log.debug("getResult 3 called"); <================ BREAKPOINT HERE
return "";
}
}
Clearly none of the above methods is executed.
显然上述方法都没有执行。
I've tried to put <typeHandler javaType="java.lang.String" jdbcType="CLOB" handler="com.foo.bar.OracleClobTypeHandler"/>
in myBatis <configuration>
, but this didn't work too.
Neither did anything else, including extending TypeHandler<Object>
and so on.
我试过放入<typeHandler javaType="java.lang.String" jdbcType="CLOB" handler="com.foo.bar.OracleClobTypeHandler"/>
myBatis <configuration>
,但这也不起作用。
也没有做任何其他事情,包括扩展TypeHandler<Object>
等等。
What am I doing wrong?
我究竟做错了什么?
采纳答案by Micha? Rybak
After digging a long, long while I have finally found the answer.
挖了很久很久,终于找到了答案。
This seems to be a bug in myBatis.
这似乎是 myBatis 中的一个错误。
In order to get your handlers to work for <result>
elements, you have to specify column
attribute explicitly even if property
attribute already matches column name and field name in bean.
In my case, it looks like this:
为了让您的处理程序为<result>
元素工作,您必须column
明确指定属性,即使property
属性已经与 bean 中的列名和字段名匹配。
就我而言,它看起来像这样:
<result property="SERVICES_XML" column="SERVICES_XML" javaType="string" jdbcType="CLOB" typeHandler="com.foo.bar.OracleClobTypeHandler" />
Note that the above change will also cause handlers defined in <configuration>
tag to work, so inline typeHandler
may be no longer necessary - that was my case. I ended up with:
请注意,上述更改还会导致<configuration>
标记中定义的处理程序起作用,因此内联typeHandler
可能不再需要 - 这就是我的情况。我结束了:
<configuration>
<typeHandlers>
<typeHandler javaType="java.lang.String" jdbcType="CLOB" handler="com.foo.bar.OracleClobTypeHandler"/>
</typeHandlers>
</configuration>
and
和
<result property="SERVICES_XML" column="SERVICES_XML" javaType="string" jdbcType="CLOB" />
回答by blackwizard
Although this question is 3 years old now, I'd rather bet the issue is related to the fact that Oracle 'uppercases' columns name retrieved from result metadata, while case sensitive java object properties name usually respect camel case.
尽管这个问题现在已有 3 年历史了,但我更愿意打赌这个问题与从结果元数据中检索到的 Oracle '大写' 列名称有关,而区分大小写的 java 对象属性名称通常尊重驼峰式大小写。
Then explicit column-property mapping is required unless you use quoted aliases in the SQL query. The question has been asked here. And unlike what is said in the latest comment, I have observed that ORDER BY quoted alias works.
然后需要显式列属性映射,除非您在 SQL 查询中使用带引号的别名。 问题已被问到here。与最新评论中所说的不同,我观察到 ORDER BY 引用的别名有效。