Mybatis if judgment of a single parameter (for exceptions: There is no getter for property..)

We all know that when mybatis is judging parameters, it can be used directly, as follows:

1. Regular code

<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>

However, there is a difference between the judgment of a single parameter and multiple parameters. When our input parameter is an entity entity or a map, there is no problem using the if parameter to judge.

But when our input parameter is java.lang.Integer or java.lang.String, then we need to pay attention to some things

The specific code is as follows (let's look at the code and say, show the error code first):

2. Error code

<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>

There are some problems in the above code. First of all, the input parameter is java.lang.Integer, not the input parameter method of map or entity. For such a single input parameter and then judged by if, mybatis has its own built-in object.

 

If you write the object name of your input parameter in the if judgment, then report an exception: Internal error : nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'langId' in ' class java.lang.Integer'

3. Correct code:

This involves the built-in object _parameter of mybatis. When judging a single parameter, it is not directly judged by the parameter object name like 1 and 2. Also, the data type is best to add

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813861&siteId=291194637