mybatis xml文件中传入参数和if结合使用时要注意的地方

mybatis中如果用了if那么传进来的参数不能直接单独传入,要封装到map或bo中传入,要么去了if

mybatis xml文件中传入参数和if结合使用时要注意的地方:

这里直接传入int但是又用了if所以出错

Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'customerKey' in 'class java.lang.Integer'

<select id="getYJSL" parameterType="Integer" resultType="int">

   SELECT count(*) yjsl    

   FROM tb_con_obj A, TB_CON_ORD B 

   WHERE A.CONOBJ_KEY = B.CONOBJ_KEY  

   AND A.IS_CONFERPRICE = 'Y'

   AND B.ORDER_STATUS = 'A' 

   AND A.CONOBJ_STATUS <![CDATA[<>]]> 'E'

   <if test="customerKey !=null and customerKey!='' ">

   AND (B.CUSTOMER_KEY = #{customerKey} or a.customer_key = #{customerKey})

   </if>

  </select>

====

  使用SpringMVC+Mybatis框架写项目的时候,在mapper里面的sql语句处出现了这个错误

<if test="agoTime != null">

and updateTime &gt; #{agoTime}

</if>

原因是:

如果将and语句中的#{属性}写在if语句中,mybatis会去对象中从它的get方法中取值,而我之前是直接传递的值,并没有封装对象。

解决办法:

将属性封装到一个对象中,设置其get方法即可

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2337833