MyBatis查詢的原始碼分析

2021-08-27 22:34:02 字數 4383 閱讀 8323

如下為mybatis的乙個集合查詢:

string resource = "mybatis.cfg.xml";  

reader reader = resources.getresourceasreader(resource);

sqlsessionfactory ssf = new sqlsessionfactorybuilder().build(reader);

sqlsession sqlsession = ssf.opensession();

try

} catch (exception e) finally

public class defaultsqlsession implements sqlsession 

@override

public listselectlist(string statement, object parameter)

@override

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

}... ...

}

/**

* 行數限制

*/public class rowbounds

public rowbounds(int offset, int limit)

... ...

}

public class cachingexecutor implements executor 

... ...

@override

boundsql boundsql = ms.getboundsql(parameterobject);

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

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

} @override

throws sqlexception

return list;}}

return delegate.query(ms, parameterobject, rowbounds, resulthandler, key, boundsql);

}... ...

}

public class reuseexecutor extends baseexecutor 

... ...

}

public class routingstatementhandler implements statementhandler 

}

public class preparedstatementhandler extends basestatementhandler 

... ...

}

public inte***ce resultsethandler
public class defaultresultsethandler implements resultsethandler 

/*---------該方法中的以下**暫且不做分析------------*/

if (resultsets != null)

rsw = getnextresultset(stmt);

cleanupafterhandlingresultset();

resultsetcount++;}}

return collapsesingleresultlist(multipleresults);

}... ...

try else else

}} finally

}... ...

/*** 處理所有行記錄中的值

*/ if (resultmap.hasnestedresultmaps()) else

}... ...

/*** 處理行值集到簡單結果的對映

*/throws sqlexception

}... ...

/*** 處理行值集到巢狀結果的對映

*/final defaultresultcontextresultcontext = new defaultresultcontext();

skiprows(rsw.getresultset(), rowbounds);

object rowvalue = null;

while (shouldprocessmorerows(resultcontext, rowbounds) && rsw.getresultset().next())

rowvalue = getrowvalue(rsw, discriminatedresultmap, rowkey, rowkey, null, partialobject);//獲取行值

} else }}

}}... ...

/*** 建立快取的key(具體key的生成策略可詳見文末)

*/final cachekey cachekey = new cachekey();

cachekey.update(resultmap.getid());

if (map.class.isassignablefrom(resultmap.gettype())) else

} else

return cachekey;

}... ...

/*** 從查詢的一條行記錄中獲取需要對映為結果屬性的值

*/final string resultmapid = resultmap.getid();

object resultobject = partialobject;

/*如果傳過來的原物件不為空,則向其中設定屬性值;

否則,建立乙個新結果物件作為原物件,然後設定屬性值*/

if (resultobject != null) else

//屬性對映

putancestor(absolutekey, resultobject, resultmapid, columnprefix);

//巢狀對映

ancestorobjects.remove(absolutekey);

foundvalues = lazyloader.size() > 0 || foundvalues;

resultobject = foundvalues ? resultobject : null;

}if (combinedkey != cachekey.null_cache_key)

}return resultobject;

}... ...

/*** 應用屬性對映

*/throws sqlexception

if (value != defered

&& property != null

&& (value != null || (configuration.iscallsettersonnulls() && !metaobject.getsettertype(property).isprimitive())))

if (value != null || value == defered) }}

return foundvalues;

}... ...

/*** 應用巢狀結果對映

*/boolean foundvalues = false;

//迴圈結果對映中所有的屬性對映,如果屬性為物件,即巢狀結果對映,則遞迴呼叫getrowvalue(...)方法

try

if (ancestorobject != null)

} else }}

} catch (sqlexception e) }}

return foundvalues;

}}

/**

* 結果對映

*/public class resultmap

... ...

}

對映時生成的快取key依次形如:

richresultmap:id:16942

可見被標註的屬性和字段值都會作為cachekey的一部分

Mybatis 原始碼分析

mybatis解析 2 sqlsessionfactorybean 繼承了 initializingbean 介面,在bean解析的finishbeanfactoryinitialization 的方法對 initializingbean介面中的 afterpropertiesset 方法進行呼叫,...

Mybatis原始碼分析

我們都知道mybatis它是一款優秀的持久層框架,它支援定製化sql 儲存過程以及高階對映,幫我們節省了許多時間與精力,今天我們主要來對mybatis的底層原始碼進行分析,主要是以下4行 首先呼叫resources中的單個string引數的getresuorceasstream string res...

MyBatis 原始碼分析

sqlsession inte ce,代表乙個連線資料庫的會話,提供了乙個預設的 defaultsqlsession sqlsessionfactory inte ct,建立 sqlsessionfactory 的工廠,提供了預設的 defaultsqlsessionfactory建立 defaul...