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);
}
④结果文章来源:https://www.toymoban.com/news/detail-585274.html
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.
总结:两种方式均可,可根据自身熟悉度进行选择 文章来源地址https://www.toymoban.com/news/detail-585274.html
到了这里,关于Mybatis实现批量删除(两种常用方法)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!