<!--List<Emp> getEmpListByMoreTJ(Emp emp);--> <selectid="getEmpListByMoreTJ"resultType="Emp"> select * from t_emp where 1=1 <iftest="ename != '' and ename != null"> and ename = #{ename} </if> <iftest="age != '' and age != null"> and age = #{age} </if> <iftest="sex != '' and sex != null"> and sex = #{sex} </if> </select>
2、where
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<selectid="getEmpListByMoreTJ2"resultType="Emp"> select * from t_emp <where> <iftest="ename != '' and ename != null"> ename = #{ename} </if> <iftest="age != '' and age != null"> and age = #{age} </if> <iftest="sex != '' and sex != null"> and sex = #{sex} </if> </where> </select>
<selectid="selectSelective"resultType="com.secbro.entity.User"> select * from t_user <where> <iftest="username != null and username != ''"> and username = #{username} </if> <iftest="idNo != null and idNo != ''"> /* and id_no = #{idNo}*/ and id_no = #{idNo} </if> </where> </select>
上述SQL语句中添加了 /**/的注释,生成的SQL语句为:
1
select * from t_user WHERE username = ? /* and id_no = ?*/ and id_no = ?
执行时,直接报错。
还有一个示例:
1 2 3 4 5 6 7 8 9 10 11 12
<selectid="selectSelective"resultType="com.secbro.entity.User"> select * from t_user <where> <iftest="username != null and username != ''"> -- and username = #{username} and username = #{username} </if> <iftest="idNo != null and idNo != ''"> and id_no = #{idNo} </if> </where> </select>
生成的SQL语句为:
1
select * from t_user WHERE -- and username = ? and username = ? and id_no = ?
同样会导致报错。
这是因为我们使用 XML 方式配置 SQL 时,如果在 where 标签之后添加了注释,那么当有子元素满足条件时,除了 < !– –> 注释会被 where 忽略解析以外,其它注释例如 // 或 /**/ 或 – 等都会被 where 当成首个子句元素处理,导致后续真正的首个 AND 子句元素或 OR 子句元素没能被成功替换掉前缀,从而引起语法错误。
<selectid="getEmpListByMoreTJ"resultType="Emp"> select * from t_emp <trimprefix="where"suffixOverrides="and"> <iftest="ename != '' and ename != null"> ename = #{ename} and </if> <iftest="age != '' and age != null"> age = #{age} and </if> <iftest="sex != '' and sex != null"> sex = #{sex} </if> </trim> </select>
<!--int insertMoreEmp(List<Emp> emps);--> <insertid="insertMoreEmp"> insert into t_emp values <foreachcollection="emps"item="emp"separator=","> (null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null) </foreach> </insert> <!--int deleteMoreByArray(int[] eids);--> <deleteid="deleteMoreByArray"> delete from t_emp where <foreachcollection="eids"item="eid"separator="or"> eid = #{eid} </foreach> </delete> <!--int deleteMoreByArray(int[] eids);--> <deleteid="deleteMoreByArray"> delete from t_emp where eid in <foreachcollection="eids"item="eid"separator=","open="("close=")"> #{eid} </foreach> </delete>
属性:
collection:设置要循环的数组或集合
item:表示集合或数组中的每一个数据
separator:设置循环体之间的分隔符
open:设置foreach标签中的内容的开始符
close:设置foreach标签中的内容的结束符
6、SQL片段
sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入
1 2 3 4
<sqlid="empColumns"> eid,ename,age,sex,did </sql> select <includerefid="empColumns"></include> from t_emp
7、bind
bind 元素允许你在 OGNL 表达式以外创建一个变量,并将其绑定到当前的上下文。比如:
1 2 3 4 5
<selectid="selectBlogsLike"resultType="Blog"> <bindname="pattern"value="'%' + _parameter.getTitle() + '%'" /> SELECT * FROM BLOG WHERE title LIKE #{pattern} </select>