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;
}
输入输出值
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);
}
}
这里调用update调用链的
package org.apache.ibatis.session.defaults;
public class DefaultSqlSession implements SqlSession {
调用jdk的反射基类
package java.lang.reflect;
public final class Method extends Executable {
SqlSessionInterceptor
package org.mybatis.spring;
public class SqlSessionTemplate implements SqlSession, DisposableBean {
SqlSessionTemplate
package org.mybatis.spring;
public class SqlSessionTemplate implements SqlSession, DisposableBean {
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);
其实主要是这里返回了自增的id值
3.int的传参
传参 单个 不能int需要intger,多个的话mybatis会自动map这个过程会int-intger