动态查询: like 后面的 %% 串
除了使 bind
还可以使用mysql的字符串拼接函数 concat
还可以使用在sql语句中拼接通配符,会引起sql注入
<![CDATA[ and userAge <= #{userage2,jdbcType=VARCHAR} ]]>
可以使[]中的内容原样输出,避免<符号发生歧义
<select id="selectUserInfoByParams" parameterType="UserInfoBean" resultMap="BaseResultMap">
<!--<bind name="patternUserName" value="'%' + _parameter.getUsername() + '%'" />-->
<bind name="patternUserAddress" value="'%' + _parameter.getUseraddress() + '%'"/>
select
<include refid="Base_Column_List"/>
from userinfo
<where>
isDeleted = 0
<if test="username != null and username != ''">
<!--and userName like #{patternUserName}-->
<!--and userName like concat('%', #{username,jdbcType=VARCHAR},'%')-->
and userName like "%"#{username,jdbcType=VARCHAR}"%"
</if>
<if test="userage != null">
and userAge >= #{userage,jdbcType=VARCHAR}
</if>
<if test="userage2 != null">
<![CDATA[
and userAge <= #{userage2,jdbcType=VARCHAR}
]]>
</if>
<if test="useraddress != null and useraddress != ''">
and userAddress like #{patternUserAddress}
</if>
<if test="userregdate != null">
and userRegDate >= #{userregdate,jdbcType=TIMESTAMP}
</if>
<if test="userregdate2 != null">
and userRegDate <= #{userregdate2,jdbcType=TIMESTAMP}
</if>
</where>
</select>