interface
import com.ibatis.sqlmap.client.event.RowHandler; public interface BatchRowHandler<T> extends RowHandler { /** * After a certain number of objects are met, perform a batch data operation * @param list */ void handBatch(); /** * Execute the operation of the last batch of data, DAO calls the RowHandler before executing the method */ void handLast (); }
abstract class
import java.util.ArrayList; import java.util.List; /**Batch handler processing, abstract class * Note that this class is thread-unsafe * * @author tangwei001 * * @param <T> */ public abstract class AbstractBatchRowHandler<T> implements BatchRowHandler<T> { private List<T> list; /** * Batch size setting, the default is 20 */ private int size=20; /** * * @param size set the batch size, when the size is reached, trigger the batch */ public AbstractBatchRowHandler(int size) { if(size>0){ this.size=size; list=new ArrayList<T>(size); }else{ list=new ArrayList<T>(size); } } @Override public void handleRow(Object obj) { list.add((T)obj); if(list.size()>=size){ handBatch(); list=new ArrayList<T>(size); } } @Override public void handLast() { handBatch(); } /** * Get batch data * @return */ protected List<T> getBatchDate(){ return list; }
Use to implement the handBatch() method
class ProGetBatchRowHander extends AbstractBatchRowHandler<Proget> { public ProGetBatchRowHander(int size) { super(size); } @Override public void handBatch() { List<Proget> list=getBatchDate(); //Business implementation } }
Call
ProGetBatchRowHander hander=new ProGetBatchRowHander(1000);
Finally, remember to call hander.handLast();