MyBatis(五)动态SQL 之 批量操作(删除)

一、批量删除

  删除记录的时候,大多时候都是根据记录的 id 去删除,所以当实现批量删除的时候,只要我们想法传进去多个 id 值就可以了。

  思路:

第一种方式:delete from tbl_employee where id in (1,2,3)
第二种方式:delete from tbl_employee where id = 1 or id = 2 or id = 3

  

二、方式一

  可以将多个 id 值拼接成字符串,然后传递进去,通过 id in (ids) 的方式来删除。

  1、在接口中声明方法

    //通过id所组成的字符串实现批量删除
    public void deleteMoreEmp(String eids);

  2、在对应的 xml 文件中配置

    <!--
        public void deleteMoreEmp(String ids);
    -->
    <delete id="deleteMoreEmp">
         delete from tbl_employee where id in (#{ids})
    </delete>

  3、测试

    @Test
     public void testDeleteMore() throws Exception {
         SqlSessionFactory sqlSessionFactory =  getSqlSessionFactory();
         SqlSession sqlSession =  sqlSessionFactory.openSession(true);
         EmpMapper mapper =  sqlSession.getMapper(EmpMapper.class);
         String eids = "19,20,21";
         mapper.deleteMoreEmp(eids);
     }

   可以发现,这时并没有全部删除,而只是删除了 19 这条记录,这是为什么呢?

   执行的SQL语句为:

delete from tbl_employee where id in (?)

    如果这时把 ids 传递过去,因为使用的是 #{} 获取值的方式,

    对于字符串类型来说,默认会给字符串加上单引号,就成了下面这种形式:

delete from tbl_employee where id in ('19,20,21')

    对于这条SQL语句来说,只会匹配到第一个 id,即id=19的记录,后面的将不会再执行了,所以只删除了第一条记录。

    

    如果把 #{} 取值改为 ${} 取值呢?

    <!--
        public void deleteMoreEmp(String ids);
    -->
    <delete id="deleteMoreEmp">
         delete from tbl_employee where id in (${value})
    </delete>

    这时再进行测试,执行的 SQL 语句是:

delete from tbl_employee where id in (19,20,21)

    可以发现,这才是我们想要的结果,并且也在数据库中成功的删除了。

    

    注意:一定要注意 #{} 取值与 ${} 取值的区别和特点,#{}对字符串类型会加单引号,而 ${} 不会。

三、方式二

  使用 foreach 标签来进行删除

  1、在接口中声明方法

    //通过list集合实现批量删除
    public void deleteMoreByList(List<Integer> ids);

  2、在 xml 中进行配置

    <!-- public void deleteMoreByList(List<Integer> ids);  -->
    <delete id="deleteMoreByList">
        delete from tbl_employee where id in
        <foreach collection="list" item="id"  separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

  3、测试

    @Test
     public void testBatchDelete() throws IOException {
          //1、获取 sqlSessionFactory
          SqlSessionFactory sqlSessionFactory = getsqlSessionFactory();
          //2、获取 sqlSession 实例,能直接执行已经映射的 SQL 语句
          SqlSession sqlSession = sqlSessionFactory.openSession();
          try {
               EmployeeMapperBatch mapper = sqlSession.getMapper(EmployeeMapperBatch.class);
               mapper.deleteMoreByList(Arrays.asList(1, 2, 3));

               sqlSession.commit();
          } finally {
               sqlSession.close();
          }
     }

  4、执行的SQL语句

Preparing: delete from tbl_employee where id in ( ? , ? , ? ) 
Parameters: 19(Integer), 20(Integer), 23(Integer)

  这时也可以执行成功。

四、方式三

  使用 foreach 标签来批量删除(思路二,使用 or)

  1、在接口中声明方法

//通过list集合实现批量删除
public void deleteMoreByList(List<Integer> ids);

  2、在xml中进行配置

    <!-- public void deleteMoreByList(List<Integer> ids);  -->
    <delete id="deleteMoreByList">
        delete from tbl_employee where
        <foreach collection="list" item="id"  separator="or">
            id = #{id}
        </foreach>
    </delete>

    把多个 id=value 进行拼接,用 or 作为分隔符。

  3、测试

    @Test
     public void testBatchDelete() throws IOException {
          //1、获取 sqlSessionFactory
          SqlSessionFactory sqlSessionFactory = getsqlSessionFactory();
          //2、获取 sqlSession 实例,能直接执行已经映射的 SQL 语句
          SqlSession sqlSession = sqlSessionFactory.openSession();
          try {
               EmployeeMapperBatch mapper = sqlSession.getMapper(EmployeeMapperBatch.class);
               mapper.deleteMoreByList(Arrays.asList(19, 20, 23));

               sqlSession.commit();
          } finally {
               sqlSession.close();
          }
     }

    执行的SQL语句:

Preparing: delete from tbl_employee where id = ? or id = ? or id = ?
Parameters: 19(Integer), 20(Integer), 23(Integer)

    此时也可以运行成功。

    注意:上面的 List 也可以更换成 Array 数组,但是取值的时候要注意,key 为 array。

原文地址:https://www.cnblogs.com/niujifei/p/15242445.html