3 回答
TA贡献1820条经验 获得超10个赞
@Override
protected Statement instantiateStatement(Connection connection) throws SQLException {
String sql = boundSql.getSql();
if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
String[] keyColumnNames = mappedStatement.getKeyColumns();
if (keyColumnNames == null) {
return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
} else {
return connection.prepareStatement(sql, keyColumnNames);
}
} else if (mappedStatement.getResultSetType() != null) {
return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
} else {
return connection.prepareStatement(sql);
}
}
这一段是Mybatis中PreparedStatementHandler的源码。大概意思是如果使用自动生成主键,并且值keyProperty不为空,Statement最后执行的时候会把keyProperty涉及到的列值返回
TA贡献1943条经验 获得超7个赞
在insert标签中添加如下代码:
<selectKey resultType="int" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
添加之后,会在对象添加之后自动填入id
TA贡献1895条经验 获得超3个赞
<selectKey resultType="int" order="AFTER" keyProperty="id">
SELECT ...
</selectKey>
添加回答
举报