<script>select * from user <where> <if test="username != null and username != '' "> username like concat(concat('%',#{username},'%')) </if> <if test="status != null and status != '' "> and status = #{status} </if> <if test="logdate != null"><![CDATA[ and logdate > #{logdate} ]]></if> order by ${column} </where> </script>
1、动态SQL标签不能错,标签一一对应,不可缺少标签
2、like不能用以往的拼接方式,应用concat关键字进行拼接
3、当参数数据类型为Integer或int类型时,0会被判断为空
4、在if标签中使用 > < 符号时要用 <![CDATA[ ]]>关键字包裹起来,不然会被认为是前标签的尾标签,程序运行失败,提示标签不合法
5、#{}关键字会对传入参数进行包裹,排序或分类时应用${},虽然这样无法防止SQL注入
6、当参数只有一个时可以不用@Param,但当参数不止一个时必须使用@Param指定,或用实体传参,使用实体传参时注意实体字段名与参数名要保持一致(字段数量可多于参数,但不能少)