Mybatis执行BatchExecutor(四)

编程入门 行业动态 更新时间:2024-10-09 02:30:25

<a href=https://www.elefans.com/category/jswz/34/1769966.html style=Mybatis执行BatchExecutor(四)"/>

Mybatis执行BatchExecutor(四)

BatchExecutor:顾名思义就是进行批量操作,通过批量操作来提高性能

public class BatchExecutor extends BaseExecutor {public static final int BATCH_UPDATE_RETURN_VALUE = Integer.MIN_VALUE + 1002;/* Statement链表**/private final List<Statement> statementList = new ArrayList<Statement>();/* batch结果链表**/private final List<BatchResult> batchResultList = new ArrayList<BatchResult>();private String currentSql;private MappedStatement currentStatement;public BatchExecutor(Configuration configuration, Transaction transaction) {super(configuration, transaction);}@Overridepublic int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {//获得配置信息final Configuration configuration = ms.getConfiguration();//获得StatementHandlerfinal StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, RowBounds.DEFAULT, null, null);final BoundSql boundSql = handler.getBoundSql();//获得Sql语句final String sql = boundSql.getSql();final Statement stmt;//如果sql语句等于当前sql MappedStatement 等于当前Map碰到Statementif (sql.equals(currentSql) && ms.equals(currentStatement)) {int last = statementList.size() - 1;//获得最后一个stmt = statementList.get(last);handler.parameterize(stmt);//fix Issues 322//有相同的MappedStatement和参数BatchResult batchResult = batchResultList.get(last);batchResult.addParameterObject(parameterObject);} else {//如果不存在就创建一个批处理操作Connection connection = getConnection(ms.getStatementLog());stmt = handler.prepare(connection);handler.parameterize(stmt);    //fix Issues 322currentSql = sql;currentStatement = ms;//添加批量处理操作statementList.add(stmt);batchResultList.add(new BatchResult(ms, sql, parameterObject));}// handler.parameterize(stmt);//最终是调用jdbc的批处理操作handler.batch(stmt);return BATCH_UPDATE_RETURN_VALUE;}@Overridepublic <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)throws SQLException {Statement stmt = null;try {flushStatements();//获得配置信息Configuration configuration = ms.getConfiguration();//获得StatementHandlerStatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);//获得连接Connection connection = getConnection(ms.getStatementLog());stmt = handler.prepare(connection);//获得Statementhandler.parameterize(stmt);return handler.<E>query(stmt, resultHandler);} finally {closeStatement(stmt);}}/* 刷新Statement,记录执行次数*/@Overridepublic List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {try {List<BatchResult> results = new ArrayList<BatchResult>();if (isRollback) {return Collections.emptyList();}//如果进行了批量处理for (int i = 0, n = statementList.size(); i < n; i++) {Statement stmt = statementList.get(i);BatchResult batchResult = batchResultList.get(i);try {//记录批量处理执行操作的条数batchResult.setUpdateCounts(stmt.executeBatch());MappedStatement ms = batchResult.getMappedStatement();//参数对象集合List<Object> parameterObjects = batchResult.getParameterObjects();//生成keyKeyGenerator keyGenerator = ms.getKeyGenerator();if (Jdbc3KeyGenerator.class.equals(keyGenerator.getClass())) {Jdbc3KeyGenerator jdbc3KeyGenerator = (Jdbc3KeyGenerator) keyGenerator;jdbc3KeyGenerator.processBatch(ms, stmt, parameterObjects);} else if (!NoKeyGenerator.class.equals(keyGenerator.getClass())) { //issue #141for (Object parameter : parameterObjects) {keyGenerator.processAfter(this, ms, stmt, parameter);}}} catch (BatchUpdateException e) {StringBuilder message = new StringBuilder();message.append(batchResult.getMappedStatement().getId()).append(" (batch index #").append(i + 1).append(")").append(" failed.");if (i > 0) {message.append(" ").append(i).append(" prior sub executor(s) completed successfully, but will be rolled back.");}throw new BatchExecutorException(message.toString(), e, results, batchResult);}//记录操作results.add(batchResult);}return results;} finally {for (Statement stmt : statementList) {closeStatement(stmt);}currentSql = null;statementList.clear();batchResultList.clear();}}}


更多推荐

Mybatis执行BatchExecutor(四)

本文发布于:2024-02-07 04:34:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mybatis   BatchExecutor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!