<!--
// 携带 哪个字段查询条件 就带上这个字段的值
public List<Employee> getEmpsByConditionIf(Employee employee);
-->
<select id="getEmpsByConditionIf" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee
<!-- where 将其替换为 where 标签
mybatis 会将where标签中拼装的sql,多出来的and 或者 or 去掉-->
<where>
<!-- id=#{id} and email=#{email} and last_name like #{lastName}
and gender=#{gender} -->
<!-- test :判断表达式(OGNL) 从参数中取值进行判断, 即id 是参数中的值
-->
<if test="id!=null">
id=#{id}
</if>
<!-- 可以查(w3school)特殊符号对应的实体名称 "" "
lastName 不为空
and &&: & -->
<if test="lastName!=null && lastName!=""">
and last_name like #{lastName}
</if>
<!-- email 修剪以后不为空串 -->
<if test="email!=null and email.trim()!=""">
and email=#{email}
</if>
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</where>
</select>
<!--
public List<Employee> getEmpsByConditionTrim(Employee employee);
-->
<select id="getEmpsByConditionTrim" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee
<!-- 前缀 后缀,前缀覆盖,后缀覆盖
自定义字符串截取规则 -->
<trim prefix="where" suffixOverrides="and">
<if test="id!=null">
id=#{id} and
</if>
<if test="lastName!=null && 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>
<!--
// #################### choose #########################
public List<Employee> getEmpsByConditionChoose(Employee employee);
-->
<select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee
<where>
<!-- 如果又id 就用 id 查, 带 last Name 就用lastName, 只会进入其中一个 -->
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name like #{lastName}
</when>
<when test="email!=null">
email=#{email}
</when>
<otherwise>
gender="0"
</otherwise>
</choose>
</where>
</select>
<!--
// Foreach
public List<Employee> getEmpsByConditionForeach(List<Integer> ids);
-->
<select id="getEmpsByConditionForeach" resultType="com.atguigu.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 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 标签 -->
<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 片段;
<!--
// 批量添加数据
public void addEmps(@Param("emps")List<Employee> emps);
MySQl 下批量保存,可以 foreach 遍历, mysql 支持 values (),(),() 语法
-->
<insert id="addEmps">
insert into tbl_employee(
<!-- 引用外部定义的 sql -->
<include refid="insertColumn"></include>
)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName}, #{emp.email}, #{emp.gender}, #{emp.dept.id})
</foreach>
</insert>
<!-- 抽取可重用的 sql 片段,方便以后引用 -->
<sql id="insertColumn">
last_name, email, gender, d_id
</sql>
这个不推荐:
<!-- 多发几次 sql allowMultiQueries=true 需要设置
这种可以用于 其他的批量操作: 删除,修改
jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true-->
<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>
<!--
mybatis 有 两个内置参数;
_parameter: 代表整个参数
多个参数时,会被封装为一个 map
_databaseId; 代表当前数据库的别名
-->
<!--
public List<Employee> getEmpsTestInnerParameter(Employee employee);
-->
<select id="getEmpsTestInnerParameter" resultType="com.atguigu.mybatis.bean.Employee">
<!-- bind 可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 -->
<bind name="_lastName" value="'%'+lastName+'%'"/>
<if test="_databaseId=='mysql'">
select * from tbl_employee
<!-- 如果为 null 全部查询 -->
<if test="_parameter!=null">
where last_name like #{_lastName}
</if>
</if>
</select>