流程图 https://www.processon.com/view/link/655098457b996f7b645b6afe
更新
定位 : org.apache.ibatis.session.defaults.DefaultSqlSession#update
1 2 3 4 5 6 7 8 9 10 11 12 13 public int update (String statement, Object parameter) { try { dirty = true ; MappedStatement ms = configuration.getMappedStatement(statement); return executor.update(ms, wrapCollection(parameter)); } catch (Exception e) { throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
org.apache.ibatis.executor.CachingExecutor#update
1 2 3 4 5 public int update (MappedStatement ms, Object parameterObject) throws SQLException { flushCacheIfRequired(ms); return delegate.update(ms, parameterObject); }
org.apache.ibatis.executor.BaseExecutor#update
1 2 3 4 5 6 7 8 9 10 11 12 public int update (MappedStatement ms, Object parameter) throws SQLException { ErrorContext.instance().resource(ms.getResource()).activity("executing an update" ).object(ms.getId()); if (closed) { throw new ExecutorException ("Executor was closed." ); } clearLocalCache(); return doUpdate(ms, parameter); }
org.apache.ibatis.executor.SimpleExecutor#doUpdate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public int doUpdate (MappedStatement ms, Object parameter) throws SQLException { Statement stmt = null ; try { Configuration configuration = ms.getConfiguration(); StatementHandler handler = configuration.newStatementHandler(this , ms, parameter, RowBounds.DEFAULT, null , null ); stmt = prepareStatement(handler, ms.getStatementLog()); return handler.update(stmt); } finally { closeStatement(stmt); } }
org.apache.ibatis.executor.statement.PreparedStatementHandler#update
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public int update (Statement statement) throws SQLException { PreparedStatement ps = (PreparedStatement) statement; ps.execute(); int rows = ps.getUpdateCount(); Object parameterObject = boundSql.getParameterObject(); KeyGenerator keyGenerator = mappedStatement.getKeyGenerator(); keyGenerator.processAfter(executor, mappedStatement, ps, parameterObject); return rows; }
插入 org.apache.ibatis.session.defaults.DefaultSqlSession#insert
1 2 3 4 5 6 7 8 9 @Override public int insert (String statement) { return insert(statement, null ); } @Override public int insert (String statement, Object parameter) { return update(statement, parameter); }
PreparedStatementHandler 更新
定位 : org.apache.ibatis.executor.statement.BaseStatementHandler#BaseStatementHandler
作用 : 语句处理器的基类, 其子类有PreparedStatementHandler, SimpleStatementHandler, CallableStatementHandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 protected BaseStatementHandler (Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { this .configuration = mappedStatement.getConfiguration(); this .executor = executor; this .mappedStatement = mappedStatement; this .rowBounds = rowBounds; this .typeHandlerRegistry = configuration.getTypeHandlerRegistry(); this .objectFactory = configuration.getObjectFactory(); if (boundSql == null ) { generateKeys(parameterObject); boundSql = mappedStatement.getBoundSql(parameterObject); } this .boundSql = boundSql; this .parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql); this .resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql); } protected void generateKeys (Object parameter) { KeyGenerator keyGenerator = mappedStatement.getKeyGenerator(); ErrorContext.instance().store(); keyGenerator.processBefore(executor, mappedStatement, null , parameter); ErrorContext.instance().recall(); }
org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator
主键生成器, 默认使用数据库的生成策略, 将生成后的主键赋值到入参对应的字段中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @Override public void processBefore (Executor executor, MappedStatement ms, Statement stmt, Object parameter) { } @Override public void processAfter (Executor executor, MappedStatement ms, Statement stmt, Object parameter) { processBatch(ms, stmt, parameter); } public void processBatch (MappedStatement ms, Statement stmt, Object parameter) { final String[] keyProperties = ms.getKeyProperties(); if (keyProperties == null || keyProperties.length == 0 ) { return ; } try (ResultSet rs = stmt.getGeneratedKeys()) { final ResultSetMetaData rsmd = rs.getMetaData(); final Configuration configuration = ms.getConfiguration(); if (rsmd.getColumnCount() < keyProperties.length) { } else { assignKeys(configuration, rs, rsmd, keyProperties, parameter); } } catch (Exception e) { throw new ExecutorException ("Error getting generated key or setting result to parameter object. Cause: " + e, e); } }
删除 org.apache.ibatis.session.defaults.DefaultSqlSession#delete
1 2 3 4 5 6 7 8 9 @Override public int delete (String statement) { return update(statement, null ); } @Override public int delete (String statement, Object parameter) { return update(statement, parameter); }