<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>
<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="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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
④choose、when、otherwise解决选择单个条件
即choose内部只会有一个条件被添加则where后面(满足则跳出)
<selectid="getEmpListByChoose"resultType="Emp">
select <includerefid="empColumns">include> from t_emp
<where><choose><whentest="ename != '' and ename != null">
ename = #{ename}
when><whentest="age != '' and age != null">
age = #{age}
when><whentest="sex != '' and sex != null">
sex = #{sex}
when><whentest="email != '' and email != null">
email = #{email}
when>choose>where>select>
<insertid="insertMoreEmp">
insert into t_emp values
<foreachcollection="emps"item="emp"separator=",">
(null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null)
foreach>insert><deleteid="deleteMoreByArray">
delete from t_emp where
<foreachcollection="eids"item="eid"separator="or">
eid = #{eid}
foreach>delete><deleteid="deleteMoreByArray">
delete from t_emp where eid in
<foreachcollection="eids"item="eid"separator=","open="("close=")">
#{eid}
foreach>delete>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
⑥sql片段(解决某些字段用次数过多)
<sqlid="empColumns">
eid,ename,age,sex,did
sql>
select <includerefid="empColumns">include> from t_emp