<select id="selectPageList" resultType="com.shop.cms.category.vo.CmsCategoryListVO"> SELECT <include refid="pageListVO"></include> FROM cms_content_category <where> <if test="param.categoryName != null and param.categoryName != ‘‘">AND `name` LIKE CONCAT(‘%‘, #{param.categoryName}, ‘%‘)</if> </where> </select>
<select id="selectPageList" resultType="com.shop.cms.category.vo.CmsCategoryListVO"> SELECT <include refid="pageListVO"></include> FROM cms_content_category <where> <if test="param.categoryName != null and param.categoryName != ‘‘">AND `name` LIKE #{param.categoryName}</if> </where> </select>
注意:因为#{...}解析成sql语句时候,会在变量外侧自动加单引号‘ ‘,所以这里 % 需要使用双引号" ",不能使用单引号 ‘ ‘,不然会查不到任何结果。
<select id="selectPageList" resultType="com.shop.cms.category.vo.CmsCategoryListVO"> SELECT <include refid="pageListVO"></include> FROM cms_content_category <where> <if test="param.categoryName != null and param.categoryName != ‘‘">AND `name` LIKE ‘%${param.categoryName}%‘</if> </where> </select>
注意:由于$是参数直接注入的,导致这种写法,大括号里面不能注明jdbcType,不然会报错
弊端:可能会引起sql的注入,平时尽量避免使用${...}
原文:https://www.cnblogs.com/Yaoson-Heyi/p/15141553.html