1、常見錯誤:
There is no getter for property named 'parentId' in 'class java.lang.Long'(或者String)
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是這樣寫的:
sql1:
<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>
此時,就報了上面的錯誤。
解決1:
把<if>標簽去掉傳參還是直接傳遞傳遞long,如下:sql2
<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是可以的。
解決2:保持sql1不變,在dao層把參數換成map類型,
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來進行傳參。