mybatis的${}和#{}的用法剖析

在实际项目用到的,搞了半天,决定总结下。

1. #{}的用法。

 

<select id="findAppUserById" parameterType="map" resultType="au">
		select * from sys_app_user
		where user_id = #{id}
	</select>

	<select id="getAppUser" parameterType="map" resultType="au">
		select * from sys_app_user where  type=#{type}
		 <if test="id != 'null' and  id !='' and id != null">
			and ascriptionId = #{id} 
		</if>
		<if test="school_info_id != null and school_info_id != ''">
			and school_info_id = #{school_info_id}
		</if>
		<if test="name != 'null' and name !='' and name != null">
			and  username like concat(concat('%', #{name}),'%')
		</if>
		<if test="list != null">
			and user_id not in
			<foreach collection="list" item="user_id" index="index" open="("
				separator="," close=")">
				#{user_id}
				</foreach>
		</if>
		limit #{start},#{pageCount}
</select>open="("
				separator="," close=")">
				#{user_id}
				</foreach>
		</if>
		limit #{start},#{pageCount}
</select>

 ==>Preparing: select * from sys_app_user where type=? and ascriptionId = ? and school_info_id = ? and user_id not in ( ? , ? , ? , ? ) limit ?,? 

==>Parameters: 2(Integer), 6bf6007c71c54d15b8b2e03181efa2fc(String), 70bcfcf6dc3a47a79f68c1c12caef54e(String), 85c58bf8-7b7c-11e8-b249-f832e46fa70c(String), 928c6616-7b77-11e8-b249-f832e46fa70c(String), f707a232-7b7a-11e8-b249-f832e46fa70c(String), 7c515b1c-7b7a-11e8-b249-f832e46fa70c(String), 0(Integer), 10(Integer)

 

2.${}的用法举例:

<select id="getAppUser" parameterType="map" resultType="au">
		select * from sys_app_user where  type=#{type}
		 <if test="id != 'null' and  id !='' and id != null">
			and ascriptionId = #{id} 
		</if>
		<if test="school_info_id != null and school_info_id != ''">
			and school_info_id = #{school_info_id}
		</if>
		<if test="name != 'null' and name !='' and name != null">
			and  username like concat(concat('%', #{name}),'%')
		</if>
		<if test="list != null">
			and user_id not in
			<foreach collection="list" item="user_id" index="index" open="('"
				separator="','" close="')">${user_id}</foreach>
		</if>
		limit #{start},#{pageCount}
</select><foreach collection="list" item="user_id" index="index" open="('"
				separator="','" close="')">${user_id}</foreach>
		</if>
		limit #{start},#{pageCount}
</select>

打印的日志:

 ==>Preparing: select * from sys_app_user where type=? and ascriptionId = ? and school_info_id = ? and user_id not in  ( ' 85c58bf8-7b7c-11e8-b249-f832e46fa70c ' , ' 928c6616-7b77-11e8-b249-f832e46fa70c ' , ' f707a232-7b7a-11e8-b249-f832e46fa70c ' , ' 7c515b1c-7b7a-11e8-b249-f832e46fa70c ' ) limit ?,? 

==>Parameters: 2(Integer), 6bf6007c71c54d15b8b2e03181efa2fc(String), 70bcfcf6dc3a47a79f68c1c12caef54e(String), 0(Integer), 10(Integer)

3.实际,类似的用法还有一个

<select id="userlistPage" parameterType="page" resultType="pd">
        select u.user_id,
        u.username,
        u.password,
        u.name,
	u.ascriptionId,        
        u.status,
        u.email,
        u.phone,
        u.number,
        u.gender,
        u.id_type,
        u.id_number,
        u.role_ids,
        u.school_info_id,
        u.role_id
        from
        <include refid="tableName"></include>
        u
        left join base_ascription a on u.ascriptionId=a.id
        <where>
            <if test="pd.sessionUser!=null and pd.sessionUser.school_info_id!=null">
                and u.school_info_id=#{pd.sessionUser.school_info_id}
            </if>
            <if test="pd.ascriptionId !=null ">
                and u.ascriptionId in
                <foreach collection="pd.ascriptionId" item="item" open="('" 
                separator="','" close="')">${item}</foreach>
            </if>
            <if test="pd.type!=null ">
                and u.type=#{pd.type}
            </if>
            <if test="pd.keywords != null and pd.keywords!= ''"><!-- 关键词检索 -->
                and (
                u.username like concat(concat('%', #{pd.keywords}),'%')
                or
                u.name like concat(concat('%', #{pd.keywords}),'%')
                )
            </if>
        </where>
        order by u.user_id
    </select>item="item" open="('" 
                separator="','" close="')">${item}</foreach>
            </if>
            <if test="pd.type!=null ">
                and u.type=#{pd.type}
            </if>
            <if test="pd.keywords != null and pd.keywords!= ''"><!-- 关键词检索 -->
                and (
                u.username like concat(concat('%', #{pd.keywords}),'%')
                or
                u.name like concat(concat('%', #{pd.keywords}),'%')
                )
            </if>
        </where>
        order by u.user_id
    </select>

总结:第一种是最正宗的用法,其实大多数情况都是第一种,第二种和第三种,其实是一样的用法,只是里面有一个参数的不同,关键是${}原样输入,和参数无关并且注意空格问题,第二三种${}的前后没有空格,是紧挨着的!不这样会出问题的哦!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

退役人员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值