MyBatis 標籤的一些問題

一、常見錯誤:java

There is no getter for property named 'parentId' in 'class java.lang.Long'(或者String)spring

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'parentId' in 'class java.lang.Long'sql

sql是這樣寫的:apache

sql1:mybatis

<select id="queryByParentId" parameterType="long" resultMap="beanMap">
        SELECT * FROM <include refid="t_pt_category"/>
        <where>
            isdel = 0
            <if test="parentId != null">
                AND parent_id = #{parentId}
            </if>
        </where>
        ORDER BY parent_id
    </select>

此時,就報了上面的錯誤。spa

解決1:code

把<if>標籤去掉傳參仍是直接傳遞傳遞long,以下:sql2blog

<select id="queryByParentId" parameterType="long" resultMap="beanMap">
        SELECT * FROM <include refid="t_pt_category"/>
        <where>
            isdel = 0
           AND parent_id = #{parentId}
            <!--<if test="parentId != null">
                AND parent_id = #{parentId}
            </if> -->
        </where>
        ORDER BY parent_id
    </select>    

這樣也sql2是能夠的。get

解決2:保持sql1不變,在dao層把參數換成map類型,io

Map<String, Object> params = new HashMap<>(1);
params.put("parentId", parentId);

此時再執行,是能夠的。

(通常通用的所有的參數都是放到map中,這種最經常使用)

 

解決3:改寫sql1,以下,所有的參數都換成"_parameter",以下sql3:

<select id="queryByParentId" parameterType="long" resultMap="beanMap">
        SELECT * FROM <include refid="t_pt_category"/>
        <where>
            isdel = 0
            <if test="_parameter != null">
                AND parent_id = #{_parameter}
            </if>
        </where>
        ORDER BY parent_id
    </select>

 

此時直接傳遞long也是能夠的。

 

ps:我仍是傾向於第二種解決方法,都用map來進行傳參。

相關文章
相關標籤/搜索