公司工程用的是Mybatis的example的类,自动生成了对数据库的操作,批量操作的今天用到了,两种方式,一种需要拓展它生成的类,另一种自带的。
1、拓展新的方法:
1)在生成的Mapper类中增加一个方法:
[java] view plain copy
- int deleteBathById(String[] ids)
2)在Mapper的配置文件中增加sql语句
[html] view plain copy
- <delete id="deleteBathById" parameterType="java.lang.String">
- delete from tb_province where id in
- <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
- #{item}
- </foreach>
- </delete>
2、自带的方法:
与第一种不同的是,批量删除的id是以集合List传递
[java] view plain copy
- Criteria criteria = example.createCriteria();
- criteria.andIdIn(id);
再调用deleteByExample(example)这个方法即可。
时间: 2024-08-01 21:17:40