if:判断
choose (when, otherwise):分支选择
trim :字符串截取 (where :封装查询条件 , set:封装修改条件) where和set都可以用trim替代
foreach:循环遍历
1、 if
test:判断表达式(OGNL)
OGNL参照PPT或者官方文档。
c:if test
从参数中取值进行判断
遇见特殊符号应该去写转义字符:
&&:
查询的时候如果某些条件没有带sql,拼装可能有问题
方案:1 给where后添加1=1 以后条件都加上and
2 mybatis推荐使用where标签将所有的查询条件包括在内,mybatis会将where标签中的拼装的sql多出来的and或者or去 除掉。 但是where标签只能去掉第一个多出来的and后者or,如果是加载条件后面是没有用的。
<!--public List<Employee> getEmpByConditionIf(Employee employee) ;
测试if判断条件
-->
<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
<select id="getEmpByConditionIf" resultType="com.mybatis.entity.Employee">
select * from tb_employee
<!-- test:判断表达式(OGNL)
OGNL参照PPT或者官方文档。 c:if test从参数中取值进行判断,遇见特殊符号应该去写转义字符:&&:-->
<where>
<if test="id != null">
id = #{id}
</if>
<if test="lastName != null and lastName != ''">
and last_name like #{lastName}
</if>
<if test="gender != null and gender != ''">
and gender like #{gender}
</if>
<if test="email != null and email != ''">
and email like #{email}
</if>
</where>
</select>
2 、where 自动在添加关键字where
where 封装查询条件,mybatis会将where标签中的拼装的sql多出来的and或者or去除掉但是where标签只能去掉第一个多出来的and后者or,如果是加载条件后面是没有用的
3、set:用于update语句中,可以代替set字段,同时将多余的逗号后面去除,(where,和set都可以被trim替代)
<!--public Integer updateEmpByConditionSet(Employee employee);-->
<update id="updateEmpByConditionSet" >
update tb_employee
<set>
<if test="lastName != null">
last_name = #{lastName},
</if>
<if test="email != null" >
email = #{email},
</if>
<if test="gender != null" >
gender = #{gender}
</if>
</set>
<where>
id = #{id}
</where>
</update>
<!--用trim替换set标签:一般更新还是推荐用set-->
<update id="updateEmpByConditionSetTrim" >
update tb_employee
<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}
</where>
</update>
4、 trim 字符串截取
<!-- 后面多出的and或者or where标签不能解决
prefix="":前缀:trim标签体中是整个字符串拼串 后的结果。
prefix给拼串后的整个字符串加一个前缀
prefixOverrides="":
前缀覆盖: 去掉整个字符串前面多余的字符
suffix="":后缀
suffix给拼串后的整个字符串加一个后缀
suffixOverrides=""
后缀覆盖:去掉整个字符串后面多余的字符
-->
<!-- public List<Employee> getEmpByConditionTrim(Employee employee);-->
<select id="getEmpByConditionTrim" resultType="com.mybatis.entity.Employee">
select * from tb_employee
<!-- 后面多出的and或者or where标签不能解决
prefix="":前缀:trim标签体中是整个字符串拼串 后的结果。
prefix给拼串后的整个字符串加一个前缀
prefixOverrides="":
前缀覆盖: 去掉整个字符串前面多余的字符
suffix="":后缀
suffix给拼串后的整个字符串加一个后缀
suffixOverrides=""
后缀覆盖:去掉整个字符串后面多余的字符
-->
<!-- 自定义字符串的截取规则 -->
<trim prefix="where" suffixOverrides="and">
<if test="id != null">
id = #{id} and
</if>
<if test="lastName != null and lastName != ''">
last_name like #{lastName} and
</if>
<if test="gender != null and gender != ''">
gender like #{gender} and
</if>
<if test="email != null and email != ''">
email like #{email}
</if>
</trim>
</select>
5、 choose
when 当条件满足就进入,只会进入一个分支,如果都不满足就进入otherwise分支,跟if不同,choose只会进入一个分支
<!-- public List<Employee> getEmpByConditionChoose(Employee employee);-->
<select id="getEmpByConditionChoose" resultType="com.mybatis.entity.Employee">
select * from tb_employee
<!--
choose :when 当条件满足就进入,只会进入一个分支,如果都不满足就进入otherwise分支
-->
<where>
<choose>
<when test="id != null">
id = #{id}
</when>
<when test="lastName != null and lastName != ''">
last_name like #{lastName}
</when>
<when test="email != null and email != ''">
email like #{email}
</when>
<otherwise>
gender like #{gender}
</otherwise>
</choose>
</where>
</select>
6、foreach: 遍历
<!--
collection:指定要遍历的集合:也就是后台传递的参数,比如有注解就用命名参数,否则就用paramN等
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候是index就是索引,item就是当前值
遍历map的时候index表示的就是map的key,item就是map的值
#{变量名}就能取出变量的值也就是当前遍历出的元素
-->
<!--public List<Employee> getEmpByConditionForeach(List<Integer> ids);-->
<select id="getEmpByConditionForeach" resultType="com.mybatis.entity.Employee">
select * from tb_employee where id in
<!--
collection:指定要遍历的集合:也就是后台传递的参数,比如有注解就用命名参数,否则就用paramN等
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>
7、两个内置参数_parameter _databaseId
<!-- 两个内置参数:
不只是方法传递过来的参数可以被用来判断,取值。。。
mybatis默认还有两个内置参数:
_parameter:代表整个参数
单个参数:_parameter就是这个参数
多个参数:参数会被封装为一个map;_parameter就是代表这个map
_databaseId:如果配置了databaseIdProvider标签。
_databaseId就是代表当前数据库的别名oracle
-->
<select id="getEmpTestInnerParameter" resultType="com.mybatis.entity.Employee">
<if test="_databaseId=='mysql'">
select id , last_name from tb_employee
<if test="_parameter!=null">
where last_name like #{lastName} and email = #{_parameter.email}
</if>
</if>
<if test="_databaseId=='oracle'">
select id ,email from tb_employee
<if test="_parameter!=null">
where email like #{email}
</if>
</if>
</select>
8、 bind 可以绑定临时变量
<!-- bind:可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 -->
<!--public List<Employee> getEmpTestInnerParameter(Employee employee);-->
<select id="getEmpTestInnerParameter" resultType="com.mybatis.entity.Employee">
<bind name="_lastName" value="'%'+lastName+'%'"/>
<if test="_databaseId=='mysql'">
select id , last_name from tb_employee
<if test="_parameter!=null">
where last_name like #{lastName} and email = #{_parameter.email}
</if>
</if>
<if test="_databaseId=='oracle'">
select id ,email,last_name,gender from tb_employee
<if test="_parameter!=null">
where email like #{email} and last_name like #{_lastName}
</if>
</if>
</select>
9、sql标签提取sql片段,在用的地方用include引用就可以
<!--
抽取可重用的sql片段。方便后面引用
1、sql抽取:经常将要查询的列名,或者插入用的列名抽取出来方便引用
2、include来引用已经抽取的sql:
3、include还可以自定义一些property,sql标签内部就能使用自定义的属性
include-property:取值的正确方式${prop},
#{不能使用这种方式
-->
include自定义
<include refid="insetColumn" >
<propoty name="textColumn" value="abc"/>
</include>
<sql id="includeColumn" >
id,last_name,email,gender,d_id ,${textColumn}
</sql>
<sql id="includeColumn" >
<if test="_databaseId='oracle'">
id,last_name,email,gender,d_id
</if>
<if test="_databaseId='mysql'">
id,last_name,email,gender,d_id
</if>
</sql>
--引用
<insert id="addEmpByForeach" databaseId="oracle" >
<!--oracle第一种批量保存方式begin end-->
<!--<foreach collection="emps" item="emp" open="begin" close="end;" >
insert into tb_employee (id,last_name,email,gender,d_id) values (seq_employee.nextval,#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id});
</foreach>-->
<!--oracle第二种批量保存方式:中间表-->
insert into TB_EMPLOYEE(
<include refid="includeColumn" />
)
select seq_employee.nextval,lastName,email ,gender,dId from(
<foreach collection="emps" item="emp" separator="union" >
select #{emp.lastName} lastName,#{emp.email} email,#{emp.gender} gender ,#{emp.dept.id} dId from dual
</foreach>
)
</insert>
10、批量插入
mysql中批量插入
<!--public Integer addEmpByForeach(@Param("emps")List<Employee> emps) ;
mysql下批量保存数据
-->
<!-- mysql下两种批量保存 -->
<!--public void addEmps(@Param("emps")List<Employee> emps); -->
<!--MySQL下批量保存:可以foreach遍历 mysql支持values(),(),()语法-->
<insert id="addEmpByForeach" databaseId="mysql" >
insert into tb_employee(last_name,gender,email,d_id)
values
<foreach collection="emps" separator="," item="emp" >
(#{emp.lastName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert>
<!-- 这种方式需要数据库连接属性allowMultiQueries=true;
这种分号分隔多个sql可以用于其他的批量操作(删除,修改) -->
<insert id="addEmpByForeach" databaseId="mysql">
<foreach collection="emps" item="emp" separator=";">
insert into tb_employee(last_name,gender,email,d_id)
values
(#{emp.lastName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert>
oralcle中批量插入
<!-- Oracle数据库批量保存:
Oracle不支持values(),(),()
Oracle支持的批量方式
1、多个insert放在begin - end里面
begin
insert into employees(employee_id,last_name,email)
values(employees_seq.nextval,'test_001','test_001@atguigu.com');
insert into employees(employee_id,last_name,email)
values(employees_seq.nextval,'test_002','test_002@atguigu.com');
end;
2、利用中间表:
insert into TB_EMPLOYEE(id,last_name,email,gender,d_id)
select seq_employee.nextval,lastName,email ,gender,dId from(
select 'test_a_01' lastName,'test_a_e01' email,'1' gender ,1 dId from dual
union
select 'test_a_02' lastName,'test_a_e02' email,'0' gender ,2 dId from dual
union
select 'test_a_03' lastName,'test_a_e03' email ,'1' gender ,1 dId from dual
)
)
-->
<!--oracle两种批量保存方式-->
<insert id="addEmpByForeach" databaseId="oracle" >
<!--oracle第一种批量保存方式begin end-->
<!--<foreach collection="emps" item="emp" open="begin" close="end;" >
insert into tb_employee (id,last_name,email,gender,d_id) values (seq_employee.nextval,#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id});
</foreach>-->
<!--oracle第二种批量保存方式:中间表-->
insert into TB_EMPLOYEE(
<include refid="includeColumn" />
)
select seq_employee.nextval,lastName,email ,gender,dId from(
<foreach collection="emps" item="emp" separator="union" >
select #{emp.lastName} lastName,#{emp.email} email,#{emp.gender} gender ,#{emp.dept.id} dId from dual
</foreach>
)
</insert>