MyBatis为什么要使用Bind

使用bind拼接字符串不仅可以避免因更换数据库而修改 SQL,也能预防 SQL 注入

示例,oracle的concat只能支持2个参数相连,下面语句只能在mysql能成功:

<if test="userName != null and userName!=''">
          and user_name like concat('%',#{userName},'%')
        </if>

使用bind示例,各数据库通用,除了兼容性,还比上面的优势有预防SQL注入

 <if test="userName != null and userName!=''">
            <bind name="userNameLike" value="'%'+userName+'%'" />
          and user_name like #{userNameLike}
        </if>
原文地址:https://www.cnblogs.com/duanzq/p/14145622.html