<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.study.mapper.EmpMapper">
<!--
1.if,通过test属性中的表达式判断标签中的内容是否有效(是否会拼接到sql中)
2.where
a.若where标签中有条件成立,会自动生成where关键字
b.会自动将where标签中内容多余的and去掉,但是其中内容后多余的and无法去掉
c.若where标签中没有任何一个条件成立,则where没有任何功能
3.trim
prefix,suffix:在标签中内容前面或者后面添加指定内容
prefixOverrides,suffixOverrides:在标签中内容前面或者后面去掉指定内容
4.choose,when,otherwise
相当于java中的if...else if...else
when至少设置一个,otherwise最多设置一个
5.foreach
collection: 设置要循环的数组或集合
item:用一个字符串表示设置或集合中的数据
separator:设置每次循环的数据之间的分割符
open:循环的所有内容以什么开始
close:循环的所有内容以什么结束
6.sql片段
可以记录一段sql,在需要用的地方使用include标签进行引用
<sql id="empColumns">
id,emp_name,age,sex
</sql>
<select id="selectAllEmp" resultType="emp">
select <include refid="empColumns"></include> from t_emp
</select>
-->
<sql id="empColumns">
id,emp_name,age,sex
</sql>
<select id="selectAllEmp" resultType="emp">
select * from t_emp
</select>
<!--List<Emp> selectEmpByEmp(Emp emp);-->
<select id="selectEmpByEmpOne" resultType="emp">
select * from t_emp
<where>
<if test="empName != null and empName !=''">
emp_name = #{empName}
</if>
<if test="age != null and age !=''">
and age =#{age}
</if>
<if test="sex != null and sex !=''">
and sex =#{sex}
</if>
</where>
</select>
<select id="selectEmpByEmp" resultType="emp">
select * from t_emp
<trim prefix="where" suffixOverrides="and">
<if test="empName != null and empName !=''">
emp_name = #{empName} and
</if>
<if test="age != null and age !=''">
age =#{age} and
</if>
<if test="sex != null and sex !=''">
sex =#{sex} and
</if>
</trim>
</select>
<!--List<Emp> selectEmpByChoose(Emp emp);-->
<select id="selectEmpByChoose" resultType="emp">
select * from t_emp
<where>
<choose>
<when test="empName !=null and empName !=''">
emp_name = #{empName}
</when>
<when test="age !=null and age !=''">
age = #{age}
</when>
<when test="sex !=null and sex !=''">
sex = #{sex}
</when>
</choose>
</where>
</select>
<!--void insertEmps(List<Emp> list);-->
<insert id="insertEmps" >
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.empName},#{emp.age},#{emp.sex})
</foreach>
</insert>
<!--void deleteMoreEmp(@Param("empIds") Integer[] empIds);-->
<delete id="deleteMoreEmp">
<!--
1.
delete from t_emp where id in
<foreach collection="empIds" item="id" separator="," open="(" close=")">
#{id}
</foreach>
2.
delete from t_emp where id in
(
<foreach collection="empIds" item="id" separator=",">
#{id}
</foreach>
)
3. separator 连接前后自动添加了空格
-->
delete from t_emp where
<foreach collection="empIds" item="id" separator="or">
id = #{id}
</foreach>
</delete>
</mapper>