*一、if和where标签
如果传入属性,就判断相等。不传入不加对应的条件。
if
判断传入的参数,最终是否添加语句
test
属性 : 内部做比较运算,最终TRUE将标签内的sql语句进行拼接,FALSE不拼接
判断语句:“key 比较运算符 值 and | or key 比较运算符 值”
大于和小于 不推荐直接写符号-> 使用 大于(>)>
, 小于(<)<
where 标签 作用:
1. 自动添加where 关键字 , where内部有任何一个if 满足,就自动添加 where关键字,不满足就会去掉where。
2. 自动去掉多余的 and 和 or 关键字
- 接口
/**
* 根据名字或薪资查询 员工表
* @param name
* @param salary
* @return
*/
List<Employee> query(@Param("name") String name, @Param("salary") Double salary);
- xml
where
标签会自动去掉“标签体内前面多余的and/or”
<mapper namespace="com.wake.mapper.EmployeeMapper">
<select id="query" resultType="employee">
select * from t_emp
<where>
<if test="name != null">
emp_name = #{name}
</if>
<if test="salary != null and salary > 100">
and emp_salary = #{salary}
</if>
</where>
</select>
</mapper>
- 测试
@Test
public void testDynamicCondition(){
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
List<Employee> query = employeeMapper.query(null, 147.60000);
System.out.println(query);
}
二、set标签
set
标签作用 :
- 自动去掉多余的 逗号(
,
) - 自动添加
set
关键字
- 接口
/**
* 根据员工ID更新员工数据,传入的name和salary不为空才可以更新
* @param employee
* @return
*/
int update(Employee employee);
- xml
<update id="update">
update t_emp
<set>
<if test="empName != null">
emp_name = #{empName},
</if>
<if test="empSalary != null">
emp_salary = #{empSalary}
</if>
</set>
where emp_id = #{empId};
</update>
- 测试
@Test
public void testDynamicCondition(){
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
List<Employee> query = employeeMapper.query(null, 147.60000);
Employee employee = query.get(0);
employee.setEmpName("艾伦");
employee.setEmpSalary(999.99);
employeeMapper.update(employee);
}
三、trim标签
使用trim标签控制条件部分两端是否包含某些字符
- prefix属性:指定要动态添加的前缀
- suffix属性:指定要动态添加的后缀
- prefixOverrides属性:指定要动态去掉的前缀,使用“|”分隔有可能的多个值
- suffixOverrides属性:指定要动态去掉的后缀,使用“|”分隔有可能的多个值
四、choose/when/otherwise标签
对比:
前面的if和where 是可以两个条件都满足 都执行。
choose…是不能都满足,when只满足第一个或是第二个,或都不满足执行otherwise。
在多个分支条件中,仅执行一个。
<!-- List<Employee> selectEmployeeByConditionByChoose(Employee employee) -->
<select id="selectEmployeeByConditionByChoose" resultType="com.wake.mybatis.entity.Employee">
select emp_id,emp_name,emp_salary from t_emp
where
<choose>
<when test="empName != null">emp_name=#{empName}</when>
<when test="empSalary < 3000">emp_salary < 3000</when>
<otherwise>1=1</otherwise>
</choose>
<!--
第一种情况:第一个when满足条件 where emp_name=?
第二种情况:第二个when满足条件 where emp_salary < 3000
第三种情况:两个when都不满足 where 1=1 执行了otherwise
-->
</select>
*五、foreach标签 ( 批量操作
sql语句:delete from emp where id in (1,2,3);
xml映射文件:
- 使用
<foreach>
遍历 批量增删改查 方法中传递的参数ids集合
<foreach collection="集合名称" item="集合遍历出来的元素/项" separator="每一次遍历使用的分隔符"
open="遍历开始前拼接的片段" close="遍历结束后拼接的片段">
</foreach>
案例:
- Mapper 接口
/**
* 根据id批量查询
*/
List<Employee> queryBatch(@Param("ids") List<Integer> ids);
/**
* 根据ID批量删除
* @param ids
* @return
*/
int deleteBatch(@Param("ids") List<Integer> ids);
/**
* 批量插入对象信息
* @param employeeList
* @return
*/
int insertBatch(@Param("list") List<Employee> employeeList);
/**
* 根据ID批量修改对象信息
* @param employeeList
* @return
*/
int updateBatch(@Param("list") List<Employee> employeeList);
- xml
<select id="queryBatch" resultType="employee">
select * from t_emp
where emp_id in
<!--
遍历的数据 : ( 1,2,3 )
collection="ids | arg0 | list"
open="(" 遍历之前需要的字符串
separator="," 每次遍历的分割符号,最后一次不会追加
close=")" 遍历结束要添加的字符串
item="id" 获取每个遍历项
-->
<foreach collection="ids" open="(" separator="," close=")" item="id">
<!-- 遍历内容:#{遍历项 item指定的key} -->
#{id}
</foreach>
</select>
<delete id="deleteBatch">
delete from t_emp where emp_id in
<foreach collection="ids" open="(" separator="," close=")" item="id">
#{id}
</foreach>
</delete>
<update id="updateBatch">
<foreach collection="list" item="emp">
update t_emp set emp_name = #{emp.empName} , emp_salary = #{emp.empSalary}
where emp_id = #{emp.empId};
</foreach>
</update>
<insert id="insertBatch">
insert into t_emp(emp_name,emp_salary)
values
<foreach collection="list" separator="," item="employee">
(#{employee.empName},#{employee.empSalary})
</foreach>
</insert>
批量更新时需要注意:
标签设计多个语句,需要设置允许指定多个语句。
<property name="url" value="jdbc:mysql://localhost:3306/mybatis-example?allowMultiQueries=true"/>
- 批量插入的例子本质上是一条SQL语句,而实现批量更新则需要多条SQL语句拼起来,用分号分开。
- 一次性发送多条SQL语句让数据库执行。此时需要在数据库连接信息的URL地址中设置。
六、sql片段
抽取重复的SQL片段:
<!-- 使用sql标签抽取重复出现的SQL片段 -->
<sql id="mySelectSql">
select emp_id,emp_name,emp_age,emp_salary,emp_gender from t_emp
</sql>
引用已抽取的SQL片段:
<!-- 使用include标签引用声明的SQL片段 -->
<include refid="mySelectSql"/>
总结
-
<if>
-
用于判断条件是否成立,如果条件为true,则拼接SQL
-
形式:
<if test="name != null"> … </if>
-
-
<where>
- where元素只会在子元素有内容的情况下才插入where子句,而且会自动去除子句的开头的AND或OR
-
<set>
- 动态地在行首插入 SET 关键字,并会删掉额外的逗号。(用在update语句中)