javascript
MyBatis整合Spring的实现(7)
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
MyBatis整合Spring的實現(xiàn)(6)中分析了方法propertiesElement,下面繼續(xù)往下分析代碼:
1 方法typeAliasesElement
private?void?typeAliasesElement(XNode?parent)?{if?(parent?!=?null)?{for?(XNode?child?:?parent.getChildren())?{if?("package".equals(child.getName()))?{String?typeAliasPackage?=?child.getStringAttribute("name");configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);}?else?{String?alias?=?child.getStringAttribute("alias");String?type?=?child.getStringAttribute("type");try?{Class<?>?clazz?=?Resources.classForName(type);if?(alias?==?null)?{typeAliasRegistry.registerAlias(clazz);}?else?{typeAliasRegistry.registerAlias(alias,?clazz);}}?catch?(ClassNotFoundException?e)?{throw?new?BuilderException("Error?registering?typeAlias?for?'"?+?alias?+?"'.?Cause:?"?+?e,?e);}}}} }這里代碼的主要實現(xiàn)就是MyBatis整合Spring的實現(xiàn)(4)中2、3分析的,如果忘記,可以回顧一下,不再深入討論。下面附上XML配置文件。
1.1 MyBatis全局配置XML文件
<typeAliases><typeAlias?alias="hashMap"?type="java.util.HashMap"/><typeAlias?alias="arraylist"?type="java.util.ArrayList"/><package?name="cn.vansky.bo.user"/><package?name="cn.vansky.bo.menu"/> </typeAliases>
2 方法pluginElement
private?void?pluginElement(XNode?parent)?throws?Exception?{if?(parent?!=?null)?{for?(XNode?child?:?parent.getChildren())?{String?interceptor?=?child.getStringAttribute("interceptor");Properties?properties?=?child.getChildrenAsProperties();Interceptor?interceptorInstance?=?(Interceptor)?resolveClass(interceptor).newInstance();interceptorInstance.setProperties(properties);configuration.addInterceptor(interceptorInstance);}} }前面章節(jié)中就沒有分析攔截器,主要是攔截器的作用是在執(zhí)行相應(yīng)的SQL時,才會發(fā)揮作用,這里只是對象的實例化,沒有過多的進行分析,下面附上分頁攔截器配置。
2.1 MyBatis全局配置XML文件
<!--?-?-?-?-?-?-?分頁攔截器-?-?-?-?-?-?-?-?-?--> <plugins><plugin?interceptor="cn.vansky.framework.core.orm.mybatis.plugin.page.PaginationInterceptor"><property?name="dialectClass"?value="cn.vansky.framework.core.orm.mybatis.plugin.page.dialect.MySQLDialect"/><property?name="sqlPattern"?value=".*findPage*.*"/></plugin> </plugins>3?方法objectFactoryElement
這里作者沒有進行過配置,所以在網(wǎng)上找了個解釋:MyBatis 每次創(chuàng)建結(jié)果對象的新實例時,它都會使用一個對象工廠(ObjectFactory)實例來完成。默認(rèn)的對象工廠需要做的僅僅是實例化目標(biāo)類,要么通過默認(rèn)構(gòu)造方法,要么在參數(shù)映射存在的時候通過參數(shù)構(gòu)造方法來實例化。默認(rèn)情況下,我們不需要配置,mybatis會調(diào)用默認(rèn)實現(xiàn)的objectFactory。 除非我們要自定義ObjectFactory的實現(xiàn), 那么我們才需要去手動配置。
3.1 MyBatis全局配置XML文件
<objectFactory?type="org.mybatis.example.ExampleObjectFactory"><property?name="someProperty"?value="100"/> </objectFactory>
4?方法objectWrapperFactoryElement
private?void?objectWrapperFactoryElement(XNode?context)?throws?Exception?{if?(context?!=?null)?{String?type?=?context.getStringAttribute("type");ObjectWrapperFactory?factory?=?(ObjectWrapperFactory)?resolveClass(type).newInstance();configuration.setObjectWrapperFactory(factory);} }作者沒有使用此屬性,也沒有進行深入研究,所以這里不做討論,有興趣的可以,自己研究,知道的童鞋也可以在評論中回復(fù)作用。
總結(jié):
這里為什么要一下子講4個方法呢?主要是這4個方法的代碼都不難,只是獲取相應(yīng)的對象放入Configuration(全局配置類)中,相信童鞋們自己看看代碼就能懂了。
轉(zhuǎn)載于:https://my.oschina.net/u/1269959/blog/521983
總結(jié)
以上是生活随笔為你收集整理的MyBatis整合Spring的实现(7)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《四》大话 TypeScript 泛型
- 下一篇: Hulu是什么?中国也有了?