mybatis配置文件查询参数的传递

通常来说,参数传递可以使用#与$进行编写,但是使用#的效率更高,使用$方式,查看日志更方便些,尤其是当执行的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>
原文地址:https://www.cnblogs.com/zkongbai/p/5976871.html