通常來說,參數傳遞可以使用#與$進行編寫,但是使用#的效率更高,使用$方式,查看日志更方便些,尤其是當執行的sql語句非常麻煩的時候.
1) 接口 形式 以下方式 [傳遞參數是一個實體]
public List<Attachment> getAttachment(Attachment query);
xml文件配置如下
<select id="getAttachment" resultMap="baseMap"> SELECT <include refid="columns"/> FROM T_OA_ATTACH atta WHERE 1 = 1 <if test="name != null and name !='' "> AND atta.NAME like '%'||#{name,jdbcType=VARCHAR}||'%' </if> <if test="type != null and type !='' "> AND atta.TYPE = #{type,jdbcType=VARCHAR} </if> <if test="path != null and path !='' "> AND atta.PATH = #{path,jdbcType=VARCHAR} </if> <if test="createUser != null and createUser !='' "> AND atta.CREATE_BY = #{createUser,jdbcType=BIGINT} </if> </select>
2) 接口形如以下方式 [傳遞參數是基本類型數據]
@Update("UPDATE T_OA_ATTACH SET STATUS = '0' WHERE ID = #{id} ")
public int delAttachment(@Param("id") Long id);
上面為annotation配置方式
xml配置如下,其中,使用$的時候,控制台打印的sql語句,語句不會使用?作為占位符
<if test="id!= null and id!='' "> AND atta.id= #{id,jdbcType=BIGINT} </if>
或者如下
<if test="id!= null and id!='' ">
AND atta.id= ${id}
</if>
2) 接口形如以下方式 [傳遞參數是基本類型數據 和 對象]
public List<MyScheduling> getMyWapScheduling(@Param("curUserId")Long currentUserId,@Param("queryDayFirst")String dayFirst,@Param("query")MyScheduling query);
此時,需要使用@Param修飾參數變量,同時,對象中的參數,需要使用對象值進行前綴引導,否則綁定不了變量
and d.period_rq between to_date('${queryDayFirst}', 'yyyymmdd') and last_day(to_date('${queryDayFirst}', 'yyyymmdd'))
<if test="query.pId != null and query.pId !='' "> and a2.p_id = #{query.pId,jdbcType=BIGINT} </if>
