1、动态sql
多条件查询
.xml配置文件中sql语句书写
<select id="getEmpByCondition",resultType="Emp">
select * from t_emp where
<if test="empName != null and empName!=' ' ">
empName=#{empName}
</if>
<if test="age != null and age != ' ' "
and age=#{age}
</if>
</select>
2、where标签
<where>
<if >
and1 sex=#{sex} and2
</if>
</where>
若where中无内容则where标签也不会生成where并且where标签会自动调节语句中的and是否出现,不过只能调节and1不能调节and2
3、Trim、Choose、When标签
当trim标签中有数据才会生效
属性prefix/suffix:将trim标签内容前/后添加指定内容
prefixOverrides/suffixOverrides:将trim标签内容前/后删除指定内容
<select id=" " resultTypde=" ">
select * from t_user
<where>
<choose>
<when> sex != null and sex != ' '
sex=#{sex}
</when>
......
<othewise>
id=1
</otherwise>
</choose>
</where>
</select>
4、Foreach标签——批量删除
foreach标签
(1)in关键字
<delete id=" ">
delete from t_emp where eid in
(_1
<foreach collection="eids_集合或数组名" item="eid_元素" separator="," open="(_2",close=")_2"
#{eid}
</foreach>
)_1
</delete>
(_1与(_2两个留一个即可
(2)where关键字
<delete id=" ">
delete from t_emp where
<foreach collection=" " item=" " seperate="or" open="(" close=")" >
eid=#{eid}
</foreach>
</delete>
5、批量插入——sql标签
<insert id=" ">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.empName},#{emp.empSex})
</foreach>
</insert>
sql标签
<sql id="xxx">字段名1,字段名2</sql>
<select id=" " resultType=" ">
select <include refid="xxx"></include> from t_emp
<
动态sql格式
>
</select>