首先看下底层mysql是怎么实现返回自增id
insert into test (name) values ('name1'), ('name2'),('name3');
SELECT LAST_INSERT_ID(); 注意SELECT 在这个语句里面必须都是大写的
返回上述insert语句中第一条插入的id,而非一个列表
mybatis是怎么返回自增的id
先看用法
<insert id="insertTest" useGeneratedKeys="true" keyProperty="id" parameterType="TestInfo"> insert into test(name)values(#{name1}) </insert>
TestInfo中必须含属性值id 这样mapper调用后,对象中id就会被赋值
原理解析:
mybatis 执行mapper,首先会进入 MapperProxy这个代理类 执行invoke方法,然后调用org.mybatis.spring.SqlSessionTemplate#insert(java.lang.String, java.lang.Object)如下
/** * {@inheritDoc} */ @Override public int insert(String statement, Object parameter) { return this.sqlSessionProxy.insert(statement, parameter); }
sqlSessionProxy 这个代理,是一个内部类的切面,直接调用内部类的org.mybatis.spring.SqlSessionTemplate.SqlSessionInterceptor#invoke
经过内部类切面之后,调用
org.apache.ibatis.session.defaults.DefaultSqlSession#insert(java.lang.String, java.lang.Object)
@Override public int insert(String statement, Object parameter) { return update(statement, parameter); }
经过org.apache.ibatis.executor.BaseExecutor#update
org.apache.ibatis.executor.SimpleExecutor#doUpdate
org.apache.ibatis.executor.statement.SimpleStatementHandler#update
@Override public int update(Statement statement) throws SQLException { String sql = boundSql.getSql(); Object parameterObject = boundSql.getParameterObject(); KeyGenerator keyGenerator = mappedStatement.getKeyGenerator(); int rows; if (keyGenerator instanceof Jdbc3KeyGenerator) { statement.execute(sql, Statement.RETURN_GENERATED_KEYS); rows = statement.getUpdateCount(); keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject); } else if (keyGenerator instanceof SelectKeyGenerator) { statement.execute(sql); rows = statement.getUpdateCount(); keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject); } else { statement.execute(sql); rows = statement.getUpdateCount(); } return rows; }
keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject); 返回自增id