咱们都知道mybatis在进行参数判断的时候,直接能够用 就能够了,以下:java
一、常规代码sql
<sql id="query_items_id"> <if test="id != null"> id = #{VALUE} </if> </sql> <select id="findItemsById" parameterType="com.zd.pojo.ItemsCustom" resultType="com.zd.pojo.ItemsCustom"> SELECT * FROM items <where> <include refid="query_items_id"/> </where> </select>
可是单个参数和多参数的判断有个不一样点,当咱们的入参为entity实体,或者map的时候,使用if 参数判断没任何问题。apache
可是当咱们的入参为java.lang.Integer 或者 java.lang.String的时候,这时候就须要注意一些事情了mybatis
具体代码以下(我们看着代码说,先展现错误代码):code
二、错误代码xml
<sql id="query_items_id"> <if test="id != null"> id = #{VALUE} </if> </sql> <select id="findItemsById" parameterType="int" resultType="com.zd.pojo.ItemsCustom"> SELECT * FROM items <where> <include refid="query_items_id"/> </where> </select>
上述代码存在一些问题,首先入参是java.lang.Integer, 而不是map或者实体的入参方式,对于这类单个入参而后用if判断的,mybatis有本身的内置对象,对象
若是你在if判断里面 写的是你的入参的对象名,那就报异常:Internal error : nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'langId' in 'class java.lang.Integer'get
三、正确代码:it
这里就涉及到mybatis的内置对象_parameter,单个参数判断的时候,就不像一、 2那样直接用参数对象名判断了。还有就是数据类型最好加上io