<!-- public List<Employee> getEmpsByConditionIf(Employee employee); -->
<select id="getEmpsByConditionIf" resultType="com.finen.mybatis.bean.Employee">
select * from tbl_employee
<where>
<!-- test:判断表达式; c:if test
从参数中取值进行判断
-->
<if test="id != null">
id=#{id}
</if>
<if test="lastName != null and lastName != ''">
and last_name like #{lastName}
</if>
<if test="email != null and email.trim()!=''">
and email=#{email}
</if>
<if test="gender == 0 or gender == 1">
and gender=#{gender}
</if>
</where>
</select>
1
<!-- public List<Employee> getEmpsByConditionTrim(Employee employee); -->
<select id="getEmpsByConditionTrim" resultType="com.finen.mybatis.bean.Employee">
select * from tbl_employee
<!-- 后面多出的and或者or where不能解决 -->
<!--
prefix="" :前缀,trim标签体中是整个字符拼串后的结果
prefix给拼串后的整个字符串加一个前缀
prefixOverrides=""
前缀覆盖:去掉整个字符串前面多余的字符
suffix="" 后缀
suffix给拼串后的整个字符串多余的字符
suffixOverrides=""
后缀覆盖:去掉整个字符串后面多余的字符
-->
<!-- 自定义字符串的截取规则 -->
<trim prefix="where" suffixOverrides="and">
<!-- test:判断表达式; c:if test
从参数中取值进行判断
-->
<if test="id != null">
id=#{id} and
</if>
<if test="lastName != null and lastName != ''">
last_name like #{lastName} and
</if>
<if test="email != null and email.trim()!=''">
email=#{email} and
</if>
<if test="gender == 0 or gender == 1">
gender=#{gender}
</if>
</trim>
</select>
<!-- public List<Employee> getEmpsByConditionChoose(Employee employee); -->
<select id="getEmpsByConditionChoose" resultType="com.finen.mybatis.bean.Employee">
select * from tbl_employee
<where>
<!-- 如果带了id就用id查,如果带了lastName就用lastName查,只会进入其中一个 -->
<choose>
<when test="id != null">
id = #{id}
</when>
<when test="email != null">
last_name like #{lastName}
</when>
<when test="lastName != null">
email = #{email}
</when>
<otherwise>
gender = 0
</otherwise>
</choose>
</where>
</select>
<!-- public void updateEmp(Employee employee); -->
<update id="updateEmp" >
update tbl_employee
<!--
Set
<set>
<if test="lastName != null">
last_name = #{lastName},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="gender != null">
gender = #{gender}
</if>
</set>
-->
<!-- Trim -->
<trim prefix="set" suffixOverrides=",">
<if test="lastName != null">
last_name = #{lastName},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="gender != null">
gender = #{gender}
</if>
</trim>
where id=#{id}
</update>
<!-- 动态SQL-Foreach -->
<!-- public List<Employee> getEmpsByConditionForeach(List<Integer> list); -->
<select id="getEmpsByConditionForeach" resultType="com.finen.mybatis.bean.Employee">
select * from tbl_employee where id in
<!--
collection:指定要遍历的集合
list类型的参数会特殊处理封装在map中,map的key叫list
item:将遍历出来的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接拼接处一个开始字符
close:遍历出所有结果拼接拼接处一个结束字符
index:索引,遍历list的时候是index就是索引,item就是当前值
遍历map的时候index表示map的key,item就是map的值
#{变量名}就能取出变量的值也就是当前遍历出的元素
-->
<foreach collection="ids" item="item_id" separator=","
open="("
close=")">
#{item_id}
</foreach>
</select>
<!-- 批量保存 -->
<!-- public void addEmps(@Param("emps")List<Employee> emps); -->
<!-- MySQL下批量保存,可以foreach遍历 -->
<!-- 首选方式 -->
<insert id="addEmps">
insert into tbl_employee(last_name, email, gender, d_id)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName}, #{emp.email}, #{emp.gender}, #{emp.dept.id})
</foreach>
</insert>
<!-- 这种方式需要数据库连接属性allowMultiQueries=true
这种分号分隔多个sql可以用于其他的批量操作(删除,修改)
-->
<insert id="addEmps">
<foreach collection="emps" item="emp" separator=";">
insert into tbl_employee(last_name, email, gender, d_id)
values(#{emp.lastName}, #{emp.email}, #{emp.gender}, #{emp.dept.id})
</foreach>
</insert>