MyBatis入门(九)

92 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第10天,点击查看活动详情

choose、when、otherwise

choose、when、otherwise相当于if...else if..else

when至少要有一个,otherwise最多只能有一个 多个分支条件中,仅执行一个。

otherwise表示条件都不满足执行的语句

  • 从上到下依次执行条件判断
  • 遇到的第一个满足条件的分支会被采纳
  • 被采纳分支后面的分支都将不被考虑
  • 如果所有的when分支都不满足,那么就执行otherwise分支
<!--List<Emp> getEmpListByChoose(Emp emp);-->
<select id="getEmpListByChoose" resultType="Emp">
select <include refid="empColumns"></include> from t_emp
<where>
<choose>
<when test="ename != '' and ename != null">
ename = #{ename}
</when>
<when test="age != '' and age != null">
age = #{age}
</when>
<when test="sex != '' and sex != null">
sex = #{sex}
</when>
<when test="email != '' and email != null">
email = #{email}
</when>
</choose>
</where>
</select>

foreach(循环)

  • 属性:

    • collection:设置要循环的数组或集合

    • item:表示集合或数组中的每一个数据

    • separator:设置循环体之间的分隔符,分隔符前后默认有一个空格,如,

    • open:设置foreach标签中的内容的开始符

    • close:设置foreach标签中的内容的结束符

批量更新时需要注意

上面批量插入的例子本质上是一条SQL语句,而实现批量更新则需要多条SQL语句拼起来,用分号分开。也就是一次性发送多条SQL语句让数据库执行。此时需要在数据库连接信息的URL地址中设置:

jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?allowMultiQueries=true

批量删除

    int deletMoreByArray(@Param("eids") Integer[] eids);
@Test
public  void Dynamrtest(){
    SqlSession sqlSession = SqlSessionUtils.getSqlSession();
    DynamincSQLMapper mapper = sqlSession.getMapper(DynamincSQLMapper.class);
    int i = mapper.deletMoreByArray(new Integer[]{6, 7, 8});
    System.out.println(i);
<!--     int deletMoreByArray(Integer[] eids);-->
<!--     collection:设置要循环的数组或集合-->
<!--     item:表示数组中的每一个元素-->
<!--     separator:表示分隔符,里面可以是空格或者,等-->
<!--    open:表示当前的内容以啥开始-->
<!--    close:表示当前的内容以结束-->
  <delete id="deletMoreByArray" >
      delete from t_emp where
      <!--    批量删除第二种写法-->
<foreach collection="eids" item="eid" separator="or" >
    eid = #{eid}
</foreach>
 运行结果:DEBUG 03-21 14:37:30,794 ==>  Preparing: delete from t_emp where eid = ? or eid = ? or eid = ?
      <!--    批量删除第一种写法-->
<!--      <foreach collection="eids" item="eid" separator="," close=")" open=" (">
                    #{eid}-->
<!--      </foreach>
运行结果:DEBUG 03-21 14:31:51,123 ==>  Preparing: delete from t_emp where eid in ( ? , ? , ? )-->
 </delete>

批量添加

/**
 * 通过list集合,实现批量添加
 * @param emps
 * @return
 */
int insertMoreByArray(@Param("emps") List<Emp> emps);
 @Test
    public  void DynamrInserttest(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamincSQLMapper mapper = sqlSession.getMapper(DynamincSQLMapper.class);
        Emp emp = new Emp(null,"张三",23,"男","123@qq.com");
        Emp emp1 = new Emp(null,"张三1",23,"男","123@qq.com");
        Emp emp2 = new Emp(null,"张三2",23,"男","123@qq.com");
        List<Emp> emps = Arrays.asList(emp,emp1,emp2);
        System.out.println(mapper.insertMoreByArray(emps));
    }
<!--    int insertMoreByArray(@Param("emps") List<Emp> emps);-->
<!--    由于参数存放在集合中,不能直接获取,先访问集合然后在访问empName,所以是emp.empName
DEBUG Preparing: insert into t_emp values (null ,?,?,?,?,null ) , (null ,?,?,?,?,null ) , (null ,?,?,?,?
-->
    <insert id="insertMoreByArray" >
        insert  into t_emp values 
        <foreach collection="emps" item="emp" separator=",">
            (null ,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null )
        </foreach>
    </insert>

SQL片段

sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入

将常用的sql设置成sql片段,方便后面调用
<sql id="empColumns" >eid,emp_name,age,sex,email</sql>
    <select id="getEmpCondition" resultType="Emp">
        select <include refid="empColumns"></include> from t_emp where
        运行结果:
DEBUG 03-21 15:07:50,646 ==>  Preparing: select eid,emp_name,age,sex,email from t_emp where emp_name = ?