Mybatis实现批量删除(两种常用方法)

1.第一种方式:

        将任意多个 id 拼接成字符串,以参数形式传递进去,通过 in 函数 的方式来删除

        ①首先定义接口类

    /*** 批量删除 * @param ids
     * @return
     * */
    //通过id所组成的字符串实现批量删除
    public void deleteId(@Param("ids") String ids);

        ②在实现类中配置Mapper.xml

    <delete id="deleteId">
        delete from accounts where id in (${ids})
    </delete>

        ③测试类

    @Test
    public void testDeleteIds() {
        ad.deleteId("25,26,27");
        sqlSession.commit();
        MyBatisUtil.close(sqlSession);
    }

        ④结果

DEBUG [main] - Logging initialized using 'org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Openning JDBC Connection
DEBUG [main] - Created connection 331510866.
DEBUG [main] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@13c27452]
DEBUG [main] - ==>  Preparing: delete from accounts where id in (25,26,27) 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@13c27452]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@13c27452]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@13c27452]
DEBUG [main] - Returned connection 331510866 to pool.

        注意: #{}中字符串类型会使用单引号,${} 则无需。这里只能使用${}方式,不能使用#{}


2.第二种方式如下:

        使用 foreach 标签来进行删除

        ①首先定义接口类

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

         ②在实现类中配置Mapper.xml

<!--    public void deleteByIds(@Param("ids") List<Integer> ids);-->
    <delete id="deleteByIds" parameterType="Integer">
        delete from accounts where id in
        <foreach collection="ids" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

        ③测试类 

    @Test
    public void testDeleteByIds() throws IOException {
        List<Integer> List = new ArrayList<>();
        List.add(22);
        List.add(23);
        List.add(24);
        ad.deleteByIds(List);
        sqlSession.commit();
        MyBatisUtil.close(sqlSession);
    }

        ④结果

DEBUG [main] - Logging initialized using 'org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Openning JDBC Connection
DEBUG [main] - Created connection 249155636.
DEBUG [main] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@ed9d034]
DEBUG [main] - ==>  Preparing: delete from accounts where id in ( ? , ? , ? ) 
DEBUG [main] - ==> Parameters: 28(Integer), 29(Integer), 30(Integer)
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@ed9d034]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@ed9d034]
DEBUG [main] - Returned connection 249155636 to pool.

        总结:两种方式均可,可根据自身熟悉度进行选择 

  • 21
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis实现批量更新有多种方式,下面介绍几种常用实现方式。 1. 使用foreach标签 使用foreach标签可以方便地实现批量更新。具体实现步骤如下: 在Mapper XML文件中定义批量更新的SQL语句,例如: ``` <update id="batchUpdate"> <foreach collection="list" item="item" separator=";"> update table_name set column_name1 = #{item.columnName1}, column_name2 = #{item.columnName2} where id = #{item.id} </foreach> </update> ``` 在Java代码中调用该SQL语句,例如: ``` List<Entity> entityList = new ArrayList<>(); // 添加需要更新的实体对象到集合中 int result = sqlSession.update("namespace.batchUpdate", entityList); ``` 2. 使用BatchExecutor BatchExecutor是MyBatis提供的批量执行器,可以批量执行SQL语句。具体实现步骤如下: 在MyBatis配置文件中配置BatchExecutor,例如: ``` <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> <executor type="BATCH"/> </environment> </environments> </configuration> ``` 在Java代码中使用BatchExecutor执行批量更新,例如: ``` List<Entity> entityList = new ArrayList<>(); // 添加需要更新的实体对象到集合中 SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); try { for (Entity entity : entityList) { sqlSession.update("namespace.update", entity); } sqlSession.commit(); } finally { sqlSession.close(); } ``` 注意事项: - BatchExecutor需要在MyBatis配置文件中进行配置,且需要使用JDBC事务管理器。 - 使用BatchExecutor进行批量更新时,需要手动管理事务和提交事务。 - 批量更新的数量受到数据库服务器和网络环境等多方面因素的影响,可能会出现性能瓶颈。建议在具体应用中进行测试和优化。 除了以上两种方式,还可以使用MyBatis提供的批量更新方法,例如: ``` List<Entity> entityList = new ArrayList<>(); // 添加需要更新的实体对象到集合中 int result = sqlSession.update("namespace.batchUpdate", entityList); ``` 这种方式需要在Mapper XML文件中定义批量更新的SQL语句,并且使用`List`类型作为参数传入SQL语句中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值