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>

 

posted @ 2020-12-16 19:15  小段-长沙  阅读(241)  评论(0编辑  收藏  举报
可以转载,转载需注明出处