Mybatis中的mapper是如何和XMl關聯(lián)起來的
從源碼來分析,通過Mybatis的都知道,必須指定nameSpace為Mapper的全限定類名。這樣就能關聯(lián)起來。Mapper的實現(xiàn)肯定是動態(tài)dialing,在InvocationHandler中做增強。
這里就來分析分析具體是怎么做的?在下面分析的時候,源碼看起來比較枯燥,并且涉及到的東西很多。
分析的時候設計的東西多,容易走偏。我盡量回歸主題。
1. XML文件解析
這里的xml解析比較繁瑣,如果逐行來分析的話,很多很多,這里就挑主線來分析了。之后會分塊來分話題來做分析。
解析總的配置文件
如果從經典的Mybatis創(chuàng)建SqlSessionFactory開始,那肯定能看到下面的代碼
代碼里面的有的注釋,是我看源碼的時候寫的,有的寫的比較離譜。有的記錄我之前看的時候的困惑。之后看的時候又看懂了。所以就保留在這里了。
private void parseConfiguration(XNode root) {
try {
// issue #117 read properties first
propertiesElement(root.evalNode("properties")); //解析properties標簽,并把他放在 parser和config的 Variables 里面
Properties settings = settingsAsProperties(root.evalNode("settings"));//加載setting標簽
loadCustomVfs(settings); //lcnote 這里的vfs是啥?怎么用 我知道這個
//我現(xiàn)在知道他的vfs是什么了,vfs(virtual file system)他抽象出了幾個api,通過這些api就可以訪問文件系統(tǒng)上的資源;比如在
// 在解析 mapperElement(root.evalNode("mappers"));的時候,如果指定package,就可以通過VFS來獲取包路徑下面所有的class文件。
// 并且會將他添加到mappe里面,和spring中的classPathSacnner一樣差不多,可以指定過濾器。
loadCustomLogImpl(settings);
typeAliasesElement(root.evalNode("typeAliases"));
//從這里開始,都是解析具體的標簽,new出對象,將標簽下面的屬性設置進去,
// 從解析的這里基本也能看出mybatis里面重要的幾個點,首先是objectFactory,objectFactory。objectFactory,plugins
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectFactory"));
reflectorFactoryElement(root.evalNode("objectFactory"));
//這里就具體設置setting標簽了
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
//lcnote 這里是重點,解析mapper文件,
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}直接看是怎么解析mapper文件的。
從這個代碼里面可以看到,mappers標簽下面是可以寫兩種標簽。package和mapper標簽。對于兩種有不同的解析方法。
解析package標簽
這里只是截取了部分的源碼。還會將好幾個源碼都拼接在一塊,便于看
if ("package".equals(child.getName())) {
String mapperPackage = child.getStringAttribute("name");
configuration.addMappers(mapperPackage);
}
//下面是 configuration.addMappers(mapperPackage)的方法
public void addMappers(String packageName) {
//mapperRegistry是一個注冊mapper的注冊器,并且里面維護了很多的所有的mapper組成的對象。
mapperRegistry.addMappers(packageName);
}
//下面是mapperRegistry.addMappers(packageName);
public void addMappers(String packageName) {
addMappers(packageName, Object.class);
}
// addMappers(packageName, Object.class); 方法,
//packageName表示要掃描的包的路徑
//superType表示要找的類是這個類的子類。
public void addMappers(String packageName, Class<?> superType)
{
//resolverUtil就是一個在指定包下,找指定的類的子類集合的一個工具類。
ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
//找到了合適的Class,將他添加到mapper里面。
Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
for (Class<?> mapperClass : mapperSet) {
addMapper(mapperClass);
}
}
//resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
//掃描給定包下(包括子路徑下面的所有的類。調用Test方法來匹配,匹配到的class調用getClasses就可以獲取的到。)
public ResolverUtil<T> find(Test test, String packageName) {
String path = getPackagePath(packageName);
try {
List<String> children = VFS.getInstance().list(path);
for (String child : children) {
if (child.endsWith(".class")) {
//加載class對象,調用ResolverUtil里面的靜態(tài)內部類IsA(實現(xiàn)了Test接口)做匹配。
addIfMatching(test, child);
}
}
} catch (IOException ioe) {
log.error("Could not read package: " + packageName, ioe);
}
return this;
}
//addIfMatching(test, child);
protected void addIfMatching(Test test, String fqn) {
try {
String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.');
ClassLoader loader = getClassLoader();
if (log.isDebugEnabled()) {
log.debug("Checking to see if class " + externalName + " matches criteria [" + test + "]");
}
Class<?> type = loader.loadClass(externalName);
if (test.matches(type)) {
matches.add((Class<T>) type);
}
} catch (Throwable t) {
log.warn("Could not examine class '" + fqn + "'" + " due to a "
+ t.getClass().getName() + " with message: " + t.getMessage());
}
}
//****************************重點***********************************
// public <T> void addMapper(Class<T> type) 方法,將上面找的,合適的class實例化之后要加載到mapperRegistry里面去。
// 并且這個方法是mapperRegistry里面的。
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {
if (hasMapper(type)) {//mapper只能注冊一次
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
// 用mapper new出MapperProxyFactory,放在knownMappers里面
knownMappers.put(type, new MapperProxyFactory<>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
// 這個是很重要的,在解析之前的添加類型,因此,他會自動嘗試綁定解析mapper。如果類型知道,沒啥事,
// 這里我覺得是解析mapper里面的注解。
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}這里面出現(xiàn)的幾個重要的類
MapperRegistry: mapper的注冊器,保存所有的mapper。Configuration:Configuration對象報錯了Mybatis運行期間能用到的所有的數(shù)據(jù)。MapperProxyFactory:代理mapper的創(chuàng)建工廠,這里面沒有啥特殊的,就是調用創(chuàng)建代理對象的方式來創(chuàng)建對象。MapperAnnotationBuilder:解析mapper里面的注解。這些注解和XMl的功能是一樣的,但是不推薦使用。
解析mapper標簽
看源碼的時候有這種感覺,哇哦,這居然可以這樣用,這個框架居然還有這種功能。
{
String resource = child.getStringAttribute("resource");
String url = child.getStringAttribute("url");
String mapperClass = child.getStringAttribute("class");
if (resource != null && url == null && mapperClass == null) {
ErrorContext.instance().resource(resource);
//resource和url的加載操作是一直的,就是resource的來源不一樣。
// 這里就會加載resource,解析mapper文件,構建mapperStatement對象,
try(InputStream inputStream = Resources.getResourceAsStream(resource)) {
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
mapperParser.parse();//lcnote 這里的解析操作和配置文件解析操作是一樣的。都是構建XMLMapperBuilder,然后調用parse方法
}
} else if (resource == null && url != null && mapperClass == null) {
ErrorContext.instance().resource(url);
try(InputStream inputStream = Resources.getUrlAsStream(url)){
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
mapperParser.parse();
}
} else if (resource == null && url == null && mapperClass != null) {
//這里沒有什么特殊,就是什么解析package標簽,得到mapper之后加載的過程,
Class<?> mapperInterface = Resources.classForName(mapperClass);
configuration.addMapper(mapperInterface);
} else {
throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
}
}從這里可以看到,支持三種屬性,resource,url和class,并且加載的順序也是resource優(yōu)先,url和class,并且三個不能同時指定。
從上面可以看出,resource和url的加載操作是一致的,就是resource的來源不一樣。
class的加載和解析package標簽,得到mapper之后加載的過程,是一致的,這里就直接看
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
開始了
構建XMLMapperBuilder
這里要說說BaseBuilder,這個類在Mybatis中是很基礎的類。好多解析都是繼承與他,才開始做解析的。

還有一點點的說明,MapperBuilderAssistant確實是一個工具類,先看看他的構造
public class MapperBuilderAssistant extends BaseBuilder {
private String currentNamespace; // 當前解析的nameSpace
private final String resource; // 當前nameSpace對應的resource文件
private Cache currentCache; // 當前的緩存,對應的mapper標簽里面的cache標簽。
private boolean unresolvedCacheRef; // issue #676
}這個類對應的就是一個mapper文件解析時候產生的所有的東西。比如resultMap,sql,select,update,等等。這些相關的東西。都會通過這個對象添加到BaseBuilder里面去。
XMLMapperBuilder繼承與BaseBuilder,XMLMapperBuilder主要是用來解析配置文件中的mappers中的mapper標簽。
// 看看構造類
public XMLMapperBuilder(InputStream inputStream, Configuration configuration, String resource, Map<String, XNode> sqlFragments) {
this(new XPathParser(inputStream, true, configuration.getVariables(), new XMLMapperEntityResolver()),
configuration, resource, sqlFragments);
}
private XMLMapperBuilder(XPathParser parser, Configuration configuration, String resource, Map<String, XNode> sqlFragments) {
super(configuration);
this.builderAssistant = new MapperBuilderAssistant(configuration, resource);
this.parser = parser;
this.sqlFragments = sqlFragments;
this.resource = resource;
}
//super的構造方法
public BaseBuilder(Configuration configuration) {
this.configuration = configuration;
this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();//從配置文件中獲取 typeAliases標簽相關內容
this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();//從配置文件中獲取typeHandlers
}
構造函數(shù)沒有什么可說的,重點是下面的Parse方法上面出現(xiàn)的幾個重要的類
- XMLMapperEntityResolver :xmlMapper的實體解析器。繼承與
EntityResolver。它是org.xml.sax包中的對象。 - BaseBuilder:所有xml解析的基礎類。
調用XMLMapperBuilder的parse方法
public void parse() {
//configuration里面保存了加載過的resource集合,這里先判斷一下
if (!configuration.isResourceLoaded(resource)) { //會去configuration里面的一個set里面去查找
// 這里是重點,重點就是解析mapper標簽
configurationElement(parser.evalNode("/mapper"));//解析mapper標簽
configuration.addLoadedResource(resource);//添加到已經加載過的集合中
bindMapperForNamespace(); //嘗試通過nameSpace來加載配置文件。
//注意,這里說的是嘗試,nameSpace并不必須和Mapper接口保持一致。
}
//下面的操作也很有意思。
//解析xml的時候,如果報錯(IncompleteElementException)不會立即拋出,而是會將這些報錯的緩存起來,在上面的都解析完成之后,在嘗試一下。
//
parsePendingResultMaps();
parsePendingCacheRefs();
parsePendingStatements();
}這里主要就是解析mapper標簽,并且嘗試通過nameSpace來加載對應的mapper。如果加載到了,就會調用上面的MapperRegistry將mapper注冊到里面。
這里的解析操作和之前解析configuration標簽的操作很類似,先解析父標簽在解析子標簽。下面看看具體是怎么解析的
private void configurationElement(XNode context) {
try {
// 解析namespace
String namespace = context.getStringAttribute("namespace");
if (namespace == null || namespace.isEmpty()) {
throw new BuilderException("Mapper's namespace cannot be empty");
}
builderAssistant.setCurrentNamespace(namespace);
cacheRefElement(context.evalNode("cache-ref"));//解析各個標簽元素
// 解析cache標簽
cacheElement(context.evalNode("cache"));
parameterMapElement(context.evalNodes("/mapper/parameterMap"));
resultMapElements(context.evalNodes("/mapper/resultMap"));
//解析sql
sqlElement(context.evalNodes("/mapper/sql"));
//waring 這里很重要,真正的開始解析select|insert|update|delete標簽
buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
} catch (Exception e) {
throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
}
}說明
1.解析parameterMap,在經過上面的解析之后,構建 ParameterMapping,添加到builderAssistant里面。在builderAssistant里面會轉換成 ParameterMap,最后添加到 Configuration對象parameterMaps屬性里面。這個Configuration就是全局通用。并且一個mapper里面能有多個parameterMap標簽。
2.解析cache,得到對應的屬性元素的值,構建Cache對象,添加到configuration里面,將builderAssistant中的currentCache賦值為當前的cache對象。并且一個Mapper只能有一個cache標簽。
3.解析resultMap,這里的解析相比前面兩個就比較復雜了,resultMap下面有很多標簽。
for (XNode resultChild : resultChildren) {
if ("constructor".equals(resultChild.getName())) {
//這個很簡單了,通過構造方法來設置參數(shù)
processConstructorElement(resultChild, typeClass, resultMappings);
} else if ("discriminator".equals(resultChild.getName())) {
// lcnote 對應的Discriminator對象,現(xiàn)實中Discriminator標簽沒有用過,之后看看,看起來這個標簽能實現(xiàn)swtich case的功能,而且還可以搭配resultMap
// 來做一些有趣的事情。之前這個確實么有用過
discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
} else {
List<ResultFlag> flags = new ArrayList<>();
if ("id".equals(resultChild.getName())) {
flags.add(ResultFlag.ID);
}
resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
}
}- 對于constructor標簽,構建ResultMapping對象添加到ResultMapping集合中。
- 對于discriminator標簽 我沒用過,之后在寫寫吧。構建Discriminator對象。
- 對于別的標簽,構建ResultMapping對象添加到ResultMapping集合中。
這都是一個resultMap標簽下面的東西,在解析完一個resultMap之后,會將上面相關的對象組裝成ResultMapResolver,調用resultMapResolver.resolve();方法,構建成ResultMap對象,放在Configuration中。所以ResultMap就是resultmap標簽對應的實體類
4.解析sql標簽。將xnode和id(sql標簽指定的id)放在XMLMapperBuilder對象的sqlFragments中,sqlFragments是一個StrictMap繼承與HashMap,重寫了里面的put,和get方法,主要是在put和get的時候增加了判斷。sqlFragments存放的是sql片段,注意,解析這里的時候并沒有處理sql里面的動態(tài)標簽的部分。要知道動態(tài)標簽是隨著參數(shù)來確定的。這里只是一個簡單的把他存起來了。并且sql標簽是多個。
5.解析select|insert|update|delete標簽。這是重點。為了清楚,還是對著源碼來看吧,select|insert|update|delete標簽是多個。所以這里是循環(huán)解析,下面的代碼只是循環(huán)體里面的解析操作。
public void parseStatementNode() {
String id = context.getStringAttribute("id");
String databaseId = context.getStringAttribute("databaseId");
//知道的,在mybatis里面是可以指定dataBase的,并且也可以在標簽里面指定要應用的databaseid。
// 這里就是一個判斷,如果不是當前要應用的,就不會解析。。
if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {
return;
}
String nodeName = context.getNode().getNodeName();
// 通過標簽的名字來判斷sql的類型。
SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
//如果沒有指定flushCache,并且是select類型,默認是false。
boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
//如果沒有指定 useCache ,并且是select類型,默認是true。
boolean useCache = context.getBooleanAttribute("useCache", isSelect);
// 這個標簽是啥意思,我沒用過。這種還是建議看看mybatis的官方文檔。
boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false);
// Include Fragments before parsing
// 在解析sql之前,想將includ標簽解析??催@個名字也能看得出來。這就是用來處理<include>標簽的。
// 這也就解釋了,之前在解析sql標簽的時候為啥這么簡單了。sql標簽最終是要用在 Statement里面的。
// 在Statement里面也是要寫動態(tài)sql的,所以,在真正開始解析標簽之前,就先把他包含進來。一塊放在
// 后面的解析操作里面。一塊解析
XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant);
includeParser.applyIncludes(context.getNode());
String parameterType = context.getStringAttribute("parameterType");
Class<?> parameterTypeClass = resolveClass(parameterType);
String lang = context.getStringAttribute("lang");
LanguageDriver langDriver = getLanguageDriver(lang);
// Parse selectKey after includes and remove them.
// 解析selectkey
processSelectKeyNodes(id, parameterTypeClass, langDriver);
//lcnote 解析sql selectKey在解析之前已經remove掉了
// Parse the SQL (pre: <selectKey> and <include> were parsed and removed)
//這里判斷是否需要使用useGeneratedKeys,這里還維護了一個緩存??梢钥纯?,id就是selcet標簽的id
KeyGenerator keyGenerator;
String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX;
keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true); // eg:org.apache.ibatis.domain.mybatis.mapper.StudentMapper.listAllStudent!selectKey
if (configuration.hasKeyGenerator(keyStatementId)) {
keyGenerator = configuration.getKeyGenerator(keyStatementId);
} else {
keyGenerator = context.getBooleanAttribute("useGeneratedKeys",
configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType))
? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
}
// 說實話,Mybatis的langDriver我還真不知道是什么,之后在分析分析
SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType", StatementType.PREPARED.toString()));
Integer fetchSize = context.getIntAttribute("fetchSize");
Integer timeout = context.getIntAttribute("timeout");
String parameterMap = context.getStringAttribute("parameterMap");
String resultType = context.getStringAttribute("resultType");
Class<?> resultTypeClass = resolveClass(resultType);
String resultMap = context.getStringAttribute("resultMap");
String resultSetType = context.getStringAttribute("resultSetType");
ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
if (resultSetTypeEnum == null) {
resultSetTypeEnum = configuration.getDefaultResultSetType();
}
String keyProperty = context.getStringAttribute("keyProperty");
String keyColumn = context.getStringAttribute("keyColumn");
String resultSets = context.getStringAttribute("resultSets");
//看到這里就知道,肯定是通過builderAssistant,將組裝好的MappedStatement添加到
// configuration里面維護了statement的map,key就是namespace+mapper的id、
builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
resultSetTypeEnum, flushCache, useCache, resultOrdered,
keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
}調用XMLMapperBuilder的parse方法,會解析Mapper.xml文件
- 首先要知道,每一個xml元素組成的數(shù)據(jù)范圍,在Mybatis中肯定是要有一個對應的對象的。
- 在處理xml的時候,肯定要用到別名和類型處理器。這倆是通用的。之后還會見到。
- 將解析好的對象都要添加到configuration里面。
- 在解析mapper文件的時候還是比較有意思的。比如,在sql標簽里面可以用${}來引用環(huán)境變量。還支持嵌套引用。
- 確實,在看源碼的時候發(fā)現(xiàn)居然有這種用法,有的東西沒有用過。之后在詳細的看看吧
- 在解析的時候如果報錯了,不會立即拋出。而是把他放在一個集合里面,等所有的xml文件解析完成了,在嘗試解析一下。
嘗試通過nameSpace綁定mapper
private void bindMapperForNamespace() {
// 前面說過,builderAssistant對應的是一個mapper解析期間的工具類。拿到namespace
String namespace = builderAssistant.getCurrentNamespace();
if (namespace != null) {
Class<?> boundType = null;
try {
//嘗試通過全限定類名加載
boundType = Resources.classForName(namespace);
} catch (ClassNotFoundException e) {
// ignore, bound type is not required
}
if (boundType != null && !configuration.hasMapper(boundType)) {
// Spring may not know the real resource name so we set a flag
// to prevent loading again this resource from the mapper interface
// look at MapperAnnotationBuilder#loadXmlResource
configuration.addLoadedResource("namespace:" + namespace);
configuration.addMapper(boundType);
}
}
}到這里就很明確了,在解析xml文件的時候會生成對應的標簽,然后將它們添加到configuration里面,然后通過nameSpace加載class類,如果nameSpace要和Mapper對應起來,還是必須要一樣的,如果不需要對應的話,那沒事了, 隨便寫。
將加載到的class添加到configuration里面。configuration里面維護著一個map,key是class,value是MapperProxyFactory。 要注意 configuration.addMapper(boundType);方法。下面我們會看看這個方法。
通過mapper標簽里面的nameSpace做緩存。并且生成代理對象創(chuàng)建工廠。
這個方法是MapperRegistry里面的。
public <T> void addMapper(Class<T> type) {
if (type.isInterface()) {
if (hasMapper(type)) {//mapper只能注冊一次
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
knownMappers.put(type, new MapperProxyFactory<>(type));
// It's important that the type is added before the parser is run
// otherwise the binding may automatically be attempted by the
// mapper parser. If the type is already known, it won't try.
// 這個是很重要的,在解析之前的添加類型,因此,他會自動嘗試綁定解析mapper。如果類型知道,沒啥事,
// 解析mapper里面的注解。
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}重點是MapperProxyFactory
/**
* @author Lasse Voss
* lcnote mapper代理對象的創(chuàng)建工廠
*/
public class MapperProxyFactory<T> {
// 需要代理的接口,也就是mapper
private final Class<T> mapperInterface;
//保存的緩存,避免new處重復對象。
private final Map<Method, MapperMethodInvoker> methodCache = new ConcurrentHashMap<>();
public MapperProxyFactory(Class<T> mapperInterface) {
this.mapperInterface = mapperInterface;
}
public Class<T> getMapperInterface() {
return mapperInterface;
}
public Map<Method, MapperMethodInvoker> getMethodCache() {
return methodCache;
}
// 這個new操作,沒有啥特殊的,就簡單的調用創(chuàng)建代理對象的方法來創(chuàng)建。
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
}2. Mapper動態(tài)代理的創(chuàng)建
添加的 InvocationHandler長什么樣子?
下面就是重點中的重點 MapperProxy,這里的太長了,我就挑重要的講了,MapperProxy實現(xiàn)了InvocationHandler,那肯定是動態(tài)代理。mapper肯定是利用反射來和xml文件關聯(lián)的。
下面的這個方法不是在new MapperProxyFactory時候調用的,在調用的時候會通過MapperProxyFactory創(chuàng)建出來,這里就順著上面的看下來了。
在方法調用的時候,對于default的方法,將方法封裝成MapperMethod,然后再用 PlainMethodInvoker包裝調用。
/** waring, 這也是比較重要的,在mapper調用的時候實現(xiàn)的InvocationHandler
* @author Clinton Begin
* @author Eduardo Macarron
*/
public class MapperProxy<T> implements InvocationHandler, Serializable {
private static final long serialVersionUID = -4724728412955527868L;
private static final int ALLOWED_MODES = MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
| MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC;
private static final Constructor<Lookup> lookupConstructor;
private final SqlSession sqlSession;
private final Class<T> mapperInterface;
private final Map<Method, MapperMethodInvoker> methodCache;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {//如果是object類里面的方法,直接調用就好了
return method.invoke(this, args);
} else {
return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {
try {
return MapUtil.computeIfAbsent(methodCache, method, m -> {
if (m.isDefault()) {//如果接口里面的方法是default的
try {
if (privateLookupInMethod == null) {
return new DefaultMethodInvoker(getMethodHandleJava8(method));
} else {
return new DefaultMethodInvoker(getMethodHandleJava9(method));
}
} catch (IllegalAccessException | InstantiationException | InvocationTargetException
| NoSuchMethodException e) {
throw new RuntimeException(e);
}
} else {
//lcnote MapperMethod代表一個mapper方法。里面包括方法對應的dataId,還有對應的sql類型,還有方法的具體的簽名信息,包括方法返回值,param參數(shù)。mapkey注解
return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
}
});
} catch (RuntimeException re) {
Throwable cause = re.getCause();
throw cause == null ? re : cause;
}
}
// mapper方法調用接口,
interface MapperMethodInvoker {
Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable;
}
private static class PlainMethodInvoker implements MapperMethodInvoker {
private final MapperMethod mapperMethod;
// 這只是實現(xiàn)mapper調用的工具類而已.
public PlainMethodInvoker(MapperMethod mapperMethod) {
super();
this.mapperMethod = mapperMethod;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable {
return mapperMethod.execute(sqlSession, args);
}
}
}什么時候創(chuàng)建對象?
從SqlSession的getMapper方法開始。從這里就開始創(chuàng)建代理對象了。
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
// 調用MapperProxyFactory來創(chuàng)建mapper實例。
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}newInstance就會創(chuàng)建出MapperProxy對象。MapperProxy對象在上面介紹了。
可見,這里是利用動態(tài)代理來創(chuàng)建了一個MapperProxy對象。
MapperProxy里面有什么?
SqlCommand
表示關聯(lián)的mapper和這方法關聯(lián)的sql的類型
會通過Mapper的全限定類名從Configuration里面找出MapperStatement。賦值id。
判斷此方法是那種sql類型。
MethodSignature
表示一個方法的主要信息。
private final boolean returnsMany;
private final boolean returnsMap; //返回值是不是一個map,只要mapKey不為null,這就是true
private final boolean returnsVoid; //標志位,是否沒有返回值
private final boolean returnsCursor; //是否返回了一個Cursor
private final boolean returnsOptional;
private final Class<?> returnType; //這個方法真正返回的類型,比如List<Student>真實返回的類型就是List
private final String mapKey; //mapkey就是MapKey注解里面的value,關于這個MapKey的作用,之后可以寫一篇文章來分析分析
private final Integer resultHandlerIndex;
private final Integer rowBoundsIndex;
private final ParamNameResolver paramNameResolver;MapperMethod
這對象里面包含了上面兩個對象,在真正執(zhí)行的時候,會調用execute方法。
Mapper和XML就關聯(lián)起來了,在往下面就要執(zhí)行sql了,下面的步驟肯定有通過全限定類名找到MapperStatement,處理入?yún)?,處理動態(tài)sql。這里會調用OGNL來解析。然后執(zhí)行并且處理結果。后面的流程就之后在說。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
RestTemplate在Spring或非Spring環(huán)境下使用精講
這篇文章主要為大家介紹了RestTemplate在Spring或非Spring環(huán)境下使用精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03
Spring Security之默認的過濾器鏈及自定義Filter操作
這篇文章主要介紹了Spring Security之默認的過濾器鏈及自定義Filter操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
利用Spring Social輕松搞定微信授權登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權登錄的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
springboot如何獲取相對路徑文件夾下靜態(tài)資源的方法
這篇文章主要介紹了springboot如何獲取相對路徑文件夾下靜態(tài)資源的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
SpringBoot集成xxl-job實現(xiàn)超牛的定時任務的步驟詳解
XXL-JOB是一個分布式任務調度平臺,其核心設計目標是開發(fā)迅速、學習簡單、輕量級、易擴展,現(xiàn)已開放源代碼并接入多家公司線上產品線,開箱即用,本文給大家介紹了SpringBoot集成xxl-job實現(xiàn)超牛的定時任務,需要的朋友可以參考下2023-10-10
Springboot啟動報錯Input length = 2的問題解決
最近使用Springboot啟動報錯,報錯內容java.nio.charset.MalformedInputException: Input length = 2,下面就來介紹一下解決方法,感興趣的可以了解一下2024-08-08

