码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis 源码分析--crud

时间:2015-11-08 19:18:33      阅读:447      评论:0      收藏:0      [点我收藏+]

标签:

 增加源码分析-insert()

 

---------------------------------------------------------------------

public int insert(String statement, Object parameter) {         //statement就是传进来的要调用xml定义的哪个sql语句,parameter就是调用语句需要传进来的参数对象
return update(statement, parameter);
}

---------------------------------------------------------------------

进入到update方法中

public int update(String statement, Object parameter) {
try {
dirty = true;                         //
MappedStatement ms = configuration.getMappedStatement(statement);    //configuration通过传进来的参数,获取对应的映射statement
return executor.update(ms, wrapCollection(parameter));            //executor是个代理对象
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}

---------------------------------------------------------------

进入到invoke方法中

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {       //参数中的method是Executor.update
try {

//获取声明的方法集[Executor.query],这个集合是自己在mybatis配置文件plugin节点下定义的要拦截的方法集
Set<Method> methods = signatureMap.get(method.getDeclaringClass()); 
if (methods != null && methods.contains(method)) {              // methods.contains(method)==false
return interceptor.intercept(new Invocation(target, method, args));
}

//target是CachingExecutor类型的,其中有两个参数SimpleExecutor的delegate和TransactionalCacheManager类型的tcm

//args是Object数组,[0]=MappedStatement,[1]=Employee(上面update方法传进来的参数)
return method.invoke(target, args);        //交给target所指定的方法继续执行程序           
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}

----------------------------------------------------------------

public int update(MappedStatement ms, Object parameterObject) throws SQLException {
flushCacheIfRequired(ms);                            //如果有缓存,则清除。
return delegate.update(ms, parameterObject);
}

-----------------------------------------------------------

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

---------------------------------------------------------------------------------------------------------

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); //这里新建一个statement处理程序
stmt = prepareStatement(handler, ms.getStatementLog());  //statement准备阶段,获取到有关数据库的信息等
return handler.update(stmt);
} finally {
closeStatement(stmt);
}
}

------------------------------newStatementHandler具体执行过程-------------------------------------------------------------------------------------------------------

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds

rowBounds, ResultHandler resultHandler, BoundSql boundSql) { 

                                        
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);                              
return statementHandler;

}

------------------------

public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {

switch (ms.getStatementType()) {                            //PREPARED(默认)
case STATEMENT:
delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
case PREPARED:
delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
case CALLABLE:
delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
default:
throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
}

}

---------------------

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

---------------------------

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) { // issue #435, get the key before calculating the statement
generateKeys(parameterObject);
boundSql = mappedStatement.getBoundSql(parameterObject);    //获取boundSql
}

this.boundSql = boundSql;

this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);  //参数处理器
this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql);//结果处理器
}

------------------真正执行插入的核心代码在PreparedStatementHandler类中------------------------

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;                          //返回执行的结果
}

-----------------------------------------------------------------------------------------------------

修改源码分析--update()同增加操作

删除源码分析--delete()同update()操作

public int delete(String statement, Object parameter) {
return update(statement, parameter);
}

Mybatis 源码分析--crud

标签:

原文地址:http://www.cnblogs.com/yydeyi/p/4947788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!