Mapper.xml中写:
<select id="selectIdsByDate" resultType="java.lang.Long"> select id from emp where cdate<#{date,jdbcType=TIMESTAMP} limit 10000 </select> <delete id="deleteByIds"> delete from emp where id in <foreach collection="list" open="(" close=")" separator="," item="id" index="i"> #{id} </foreach> </delete>
接口中这样写:
List<Long> selectIdsByDate(String date); int deleteByIds(List<Long> ids);
代码中则这样用:
EmpMapper mapper=session.getMapper(EmpMapper.class); int totalChanged=0; int index=0; while(true) { List<Long> ids=mapper.selectIdsByDate("2018-02-02"); if(ids.size()==0) { break; } int changed=mapper.deleteByIds(ids); System.out.println("#"+index+" deleted="+changed); session.commit(); totalChanged+=changed; index++; }
--END-- 2019年10月14日09:30:52