我正在进行从 iBatis 到 myBatis 的迁移,在转换过程中,我的查询曾经有效,但现在无效。我用头撞墙的时间比我愿意承认的要长。
iBatis 中的查询是:
<select id="countForACol" parameterClass="java.lang.Long" resultClass="java.lang.Long">
SELECT
COUNT(1) AS 'val'
FROM
someTable WITH(NOLOCK)
<isParameterPresent prepend="WHERE">
someCol = #colId#
</isParameterPresent>
</select>
现在,我已将其翻译成如下所示的查询:
<select id="selectTotalRegionCountForGbs" parameterType="Long" resultType="java.lang.Long">
SELECT
COUNT(1) AS 'val'
FROM
someTable WITH(NOLOCK)
<where>
<if test="colId != null">
someCol = #{colId}
</if>
</where>
</select>
然而,这不起作用。我在尝试运行时收到的错误是:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'colId' in 'class java.lang.Long'
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'colId' in 'class java.lang.Long'
我可以看出它正在尝试将 Long 对象视为一个对象,该对象具有一个名为“getColId”的 setter/getter ,可预见的是它不存在,但我不知道如何向 MyBatis 发出信号以使用 Long 的值.
我如何让它工作?
最佳答案
我通过这样做能够解决问题:
<select id="selectTotalRegionCountForGbs" parameterType="Long" resultType="java.lang.Long">
SELECT
COUNT(1) AS 'val'
FROM
someTable WITH(NOLOCK)
<where>
<if test="value != null">
someCol = #{colId}
</if>
</where>
</select>
我将测试更改为使用“value”而不是“colId”并且它运行良好。
我相信@jdevelop 和@Michal Rybak 的回答同样有效。
关于java - 使用 Longs 的 MyBatis 动态 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848861/