批量删除主要借助了MySql的limit函数,其次用了in删除。
代码如下:
package com.hy.action; import java.io.Reader; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.log4j.Logger; import com.hy.entity.Employee; import com.hy.mapper.EmpMapper; public class BatchDelete1 { private static Logger logger = Logger.getLogger(SelectById.class); public static void main(String[] args) throws Exception{ long startTime = System.currentTimeMillis(); Reader reader=Resources.getResourceAsReader("mybatis-config.xml"); SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(reader); reader.close(); SqlSession session=ssf.openSession(); try { 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++; } System.out.println("All deleted="+totalChanged); }catch(Exception ex) { logger.error(ex); session.rollback(); }finally { session.close(); long endTime = System.currentTimeMillis(); logger.info("Time elapsed:" + toDhmsStyle((endTime - startTime)/1000) + "."); } } // format seconds to day hour minute seconds style // Example 5000s will be formatted to 1h23m20s private static String toDhmsStyle(long allSeconds) { String DateTimes = null; long days = allSeconds / (60 * 60 * 24); long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60); long minutes = (allSeconds % (60 * 60)) / 60; long seconds = allSeconds % 60; if (days > 0) { DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s"; } else if (hours > 0) { DateTimes = hours + "h" + minutes + "m" + seconds + "s"; } else if (minutes > 0) { DateTimes = minutes + "m" + seconds + "s"; } else { DateTimes = seconds + "s"; } return DateTimes; } }
而Mapper中的SQL语句是:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.hy.mapper.EmpMapper"> <select id="selectById" resultType="com.hy.entity.Employee"> select id,name,age,cdate as ctime from emp where id=#{id} </select> <insert id="batchInsert"> insert into emp(name,age,cdate) values <foreach collection="list" item="emp" separator=","> (#{emp.name},#{emp.age},#{emp.ctime,jdbcType=TIMESTAMP}) </foreach> </insert> <insert id="singleInsert"> insert into emp(name,age,cdate) values (#{name},#{age},#{ctime,jdbcType=TIMESTAMP}) </insert> <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> </mapper>
与之对应的接口是:
package com.hy.mapper; import java.util.List; import org.apache.ibatis.annotations.Param; import com.hy.entity.Employee; public interface EmpMapper { Employee selectById(long id); int batchInsert(List<Employee> emps); // 用@Param标签指明和SQL的参数对应能避免出现org.apache.ibatis.binding.BindingException异常 int singleInsert(@Param("name")String name,@Param("age")int age,@Param("ctime")String ctime); List<Long> selectIdsByDate(String date); int deleteByIds(List<Long> ids); }
以上粗体红字为涉及代码。
控制台输出如下:
#792 deleted=10000 #793 deleted=10000 #794 deleted=10000 #795 deleted=10000 #796 deleted=10000 #797 deleted=10000 #798 deleted=10000 #799 deleted=10000 #800 deleted=10000 #801 deleted=10000 #802 deleted=10000 #803 deleted=5199 All deleted=8035199 INFO [main] - Time elapsed:4m7s.
数据库中情况:
--END-- 2019年10月14日09:24:07