1.如何传递多个参数
mybatis中,如果接口有多个参数,那么在mapper.xml中,可以通过#{0,jdbcType=VARCHAR},#{1,jdbcType=VARCHAR}或#{param1,jdbcType=VARCHAR},#{param2,jdbcType=VARCHAR}来获取。
2.加强版的分支、选择判断
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
3.避免Where 空条件的尴尬
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG <where> <if test="state != null"> and state = #{state} </if> </where> </select>
4.$与#的区别
1 select * from T_PRINT_LAYOUT where D_RECID = ${recId}
最后生成的SQL为:
1 select * from T_PRINT_LAYOUT where D_RECID = 1
即:直接将参数值替换到了原来${recId}的位置,相当于硬拼SQL
1 select * from T_PRINT_LAYOUT where D_RECID = #{recid,jdbcType=DECIMAL}
最后生成的SQL为:
1 select * from T_PRINT_LAYOUT where D_RECID = ?
即:#{...}被识别为一个SQL参数