mybatis3原始碼2 查詢

2021-09-18 06:27:07 字數 4695 閱讀 5063

查詢舉例

public class main 

} finally

}}

defaultsqlsessionfactory

@override

public sqlsession opensession()

private sqlsession opensessionfromdatasource(executortype exectype, transactionisolationlevel level, boolean autocommit) catch (exception e) finally

}

建立事務,簡單執行器,執行器新增***鏈,返回defaultsqlsession

@override

}

configuration} }

try catch (exception e)

}

public t newinstance(sqlsession sqlsession) 

@suppresswarnings("unchecked")

}

@override

public object invoke(object proxy, method method, object args) throws throwable else if (isdefaultmethod(method))

} catch (throwable t)

} }

}

public object execute(sqlsession sqlsession, object args) 

case update:

case delete:

case select:

if (method.returnsvoid() && method.hasresulthandler()) else if (method.returnsmany()) else if (method.returnsmap()) else if (method.returnscursor()) else

break;

case flush:

result = sqlsession.flushstatements();

break;

default:

throw new bindingexception("unknown execution method for: " + command.getname());

}if (result == null && method.getreturntype().isprimitive() && !method.returnsvoid())

return result;

}

defaultsqlsession查詢單個selectone,呼叫selectlist方法,呼叫執行器查詢

@override

public t selectone(string statement, object parameter) else if (list.size() > 1) else

} @override

public listselectlist(string statement, object parameter)

@override

public listselectlist(string statement, object parameter, rowbounds rowbounds) catch (exception e) finally

}

}

if (validateincompletestatements)

} /*

* parses all the unprocessed statement nodes in the cache. it is recommended

* statement validation.

*/protected void buildallstatements()

}if (!incompletecacherefs.isempty())

}if (!incompletestatements.isempty())

}if (!incompletemethods.isempty())

}}

@override

boundsql boundsql = ms.getboundsql(parameter);

cachekey key = createcachekey(ms, parameter, rowbounds, boundsql);

return query(ms, parameter, rowbounds, resulthandler, key, boundsql);

} @override

if (closed)

cachekey cachekey = new cachekey();

cachekey.update(ms.getid());

cachekey.update(rowbounds.getoffset());

cachekey.update(rowbounds.getlimit());

cachekey.update(boundsql.getsql());

typehandlerregistry typehandlerregistry = ms.getconfiguration().gettypehandlerregistry();

// mimic defaultparameterhandler logic

object value;

if (boundsql.hasadditionalparameter(propertyname)) else if (parameterobject == null) else if (typehandlerregistry.hastypehandler(parameterobject.getclass())) else

cachekey.update(value);}}

if (configuration.getenvironment() != null)

return cachekey;

} @suppresswarnings("unchecked")

@override

errorcontext.instance().resource(ms.getresource()).activity("executing a query").object(ms.getid());

if (closed)

if (querystack == 0 && ms.isflushcacherequired())

listlist;

try else

} finally

if (querystack == 0)

// issue #601

deferredloads.clear();

if (configuration.getlocalcachescope() == localcachescope.statement)

}return list;

}//快取中查詢

if (ms.getstatementtype() == statementtype.callable) }}

}}//從資料庫查詢,然後放入快取中

listlist;

localcache.putobject(key, execution_placeholder);

try finally

localcache.putobject(key, list);

if (ms.getstatementtype() == statementtype.callable)

return list;

}

******executor查詢資料庫

@override

statement stmt = null;

try finally

}

******statementhandler查詢,封裝結果集

@override

public listquery(statement statement, resulthandler resulthandler) throws sqlexception

defaultresultsethandler封裝結果集

@override

public listhandleresultsets(statement stmt) throws sqlexception

if (resultsets != null)

rsw = getnextresultset(stmt);

cleanupafterhandlingresultset();

resultsetcount++;}}

return collapsesingleresultlist(multipleresults);

} @suppresswarnings("unchecked")

private listcollapsesingleresultlist(listmultipleresults)

Mybatis3學習筆記 2 補充

1.首先看看這個例子中所用到的mybatis的介面和類 resources類 org.apache.ibatis.io.resources 顧名思義就是資源,用來讀取資源檔案,讀取mybatis的主配置檔案 sqlsession介面 用於持久化操作,乙個sqlsession對應著一次資料庫會話,一次...

Mybatis(3) 引數處理

單個引數 mybatis不會做特殊處理,取出引數值。多個引數 mybatis會做特殊處理。多個引數會被封裝成 乙個map,key param1.paramn,或者引數的索引也可以 value 傳入的引數值 就是從map中獲取指定的key的值 命名引數 明確指定封裝引數時map的key param i...

mybatis原始碼解讀(3)

string resource mybatis config.xml inputstream inputstream resources.getresourceasstream resource sqlsessionfactory sqlsessionfactory new sqlsessionfa...