Mybatis SQL转义字符与like 查询

Mybatis Sql语句里一些特殊符号是必须转义的,否则无法正常运行,有些会在静态语法检查上IDE会提示,有些则只会在运行时抛出异常。需要转义的符号如下

   &lt;          < 
    &gt;          >  
    &lt;&gt;  <>
    &amp;      & 
    &apos;      '
    &quot;      "

<sql id="getAllWhere">
	
		<if test="condition != null">
			<if test="condition.type >0">
				<!-- 歌手类别 -->
				and g.type = #{condition.type}
			</if>
			<if test="condition.mediaName != null">
				<!-- 歌名汉字 ,下面三种写法都可以--> 
					and media_name like '%${condition.mediaName}%'
<!-- 				and media_name like '%${condition.mediaName}%' -->
<!-- 				and media_name like CONCAT(CONCAT('%',#{condition.mediaName}),'%') -->
			</if>
			<if test="condition.geQuLeng > 0">
				<!-- 歌名字数,UTF-8一个汉字的长度为3 -->
				<if test="condition.geQuLeng > 9"> and length(media_name) >= #{condition.geQuLeng}	</if>
				<if test="condition.geQuLeng < 10"> and length(media_name) = #{condition.geQuLeng}	</if>
			</if>
			
		</if>
		<!-- 时间排序 -->
		<if test="sort != null ">
			<if test="order != null"> order by #{sort} #{order} </if>
			<if test="order == null"> order by #{sort} desc </if>
		</if>
	</sql>


原文地址:https://www.cnblogs.com/xinyuyuanm/p/2993462.html