MyBatis调试insert得到返回值和自增的id

1.insert

package org.apache.ibatis.executor.statement;
...
public class PreparedStatementHandler extends BaseStatementHandler {

  public PreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
    super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
  }

  @Override
  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;
  }


MyBatis调试insert得到返回值和自增的id_第1张图片


输入输出值

public class SimpleExecutor extends BaseExecutor {

  public SimpleExecutor(Configuration configuration, Transaction transaction) {
    super(configuration, transaction);
  }

  @Override
  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);
    }
  }

MyBatis调试insert得到返回值和自增的id_第2张图片


这里调用update调用链的

package org.apache.ibatis.session.defaults;
public class DefaultSqlSession implements SqlSession {

MyBatis调试insert得到返回值和自增的id_第3张图片


调用jdk的反射基类

package java.lang.reflect;
public final class Method extends Executable {
MyBatis调试insert得到返回值和自增的id_第4张图片


SqlSessionInterceptor

package org.mybatis.spring;
public class SqlSessionTemplate implements SqlSession, DisposableBean {

MyBatis调试insert得到返回值和自增的id_第5张图片

SqlSessionTemplate

package org.mybatis.spring;
public class SqlSessionTemplate implements SqlSession, DisposableBean {
MyBatis调试insert得到返回值和自增的id_第6张图片

2.代码写法

@Insert("insert into table(XXX,XXX,XXX) values(#{demo.XXX},#{demo.XXX},#{demo.XXX}) ")        
@SelectKey(keyProperty = "demo.id", keyColumn = "id", statement = "select @@identity as id",resultType = Integer.class, before = false)  
public int insertdemo(@Param("demo") DemoBean demo);

调用

MyBatis调试insert得到返回值和自增的id_第7张图片


其实主要是这里返回了自增的id值

MyBatis调试insert得到返回值和自增的id_第8张图片


3.int的传参

传参 单个 不能int需要intger,多个的话mybatis会自动map这个过程会int-intger

你可能感兴趣的:(MyBatis调试insert得到返回值和自增的id)