`
234390216
  • 浏览: 10197254 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A5ee55b9-a463-3d09-9c78-0c0cf33198cd
Oracle基础
浏览量:461019
Ad26f909-6440-35a9-b4e9-9aea825bd38e
springMVC介绍
浏览量:1772134
Ce363057-ae4d-3ee1-bb46-e7b51a722a4b
Mybatis简介
浏览量:1395785
Bdeb91ad-cf8a-3fe9-942a-3710073b4000
Spring整合JMS
浏览量:394006
5cbbde67-7cd5-313c-95c2-4185389601e7
Ehcache简介
浏览量:678363
Cc1c0708-ccc2-3d20-ba47-d40e04440682
Cas简介
浏览量:529417
51592fc3-854c-34f4-9eff-cb82d993ab3a
Spring Securi...
浏览量:1178996
23e1c30e-ef8c-3702-aa3c-e83277ffca91
Spring基础知识
浏览量:462567
4af1c81c-eb9d-365f-b759-07685a32156e
Spring Aop介绍
浏览量:150282
2f926891-9e7a-3ce2-a074-3acb2aaf2584
JAXB简介
浏览量:66976
社区版块
存档分类
最新评论

Mybatis核心杂谈

阅读更多

 


Mybatis杂谈

 

目录

1.1        配置解析

1.2        全局配置简介

1.3        应用浅析

 

         本文主要介绍Mybatis的配置解析对应的ClassMybatis的全局配置等,每个点都不会讲太多细节的东西,只是提一下对应的点,更多细节的东西请有兴趣的读者自己去挖掘。

1.1     配置解析

       Mybatis有一堆的配置,有Mybatis的全局配置文件,有Mapper.xml文件,Mapper.xml文件里面又有selectinsertMappedStatementMappedStatement里面又包含SQL语句,SQL语句里面可能还包含<if><where>等动态标签。在Mybatis中这些都是通过BaseBuilder的子类来解析的,具体如下。

类型

解析器

Mybatis全局配置文件

XMLConfigBuilder

Mapper.xml配置文件

XMLMapperBuilder

Mapper.xml配置文件里面的<select><insert>等操作语句

XMLStatementBuilder

SQL语句中的<if><where>等动态标签

XMLScriptBuilder

SQL语句及其预编译变量

SqlSourceBuilder

 

1.2     全局配置简介

       Mybatis的全局配置是通过XMLConfigBuilder解析的,Mybatis的全局配置文件里面可以有很多配置,如数据源、Mapper映射文件等,而本小节主要是介绍其中可定义的在<settings>下面的全局配置<setting>。具体可以定义哪些配置,分别代表什么含义,请读者参考XMLConfigBuildersettingsElement()方法,以下是该方法的源码。

 

  private void settingsElement(Properties props) throws Exception {

    configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));

    configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));

    configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));

    configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));

    configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), true));

    configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));

    configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));

    configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));

    configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));

    configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));

    configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));

    configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));

    configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));

    configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));

    configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));

    configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));

    configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));

    configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));

    configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));

    configuration.setLogPrefix(props.getProperty("logPrefix"));

    configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));

    configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));

  }

 

       以下是一个全局配置的示例。

   <settings>

      <!--日志实现,内置的有SLF4JCOMMONS_LOGGINGLOG4JLOG4J2JDK_LOGGINGSTDOUT_LOGGINGNO_LOGGING

        不指定时会按照一定的顺序一个个试,直到获取到为止,可以参考LogFactory -->

      <setting name="logImpl" value="LOG4J"/>

      <setting name="cacheEnabled" value="true" />

      <setting name="proxyFactory" value="CGLIB"/>

      <setting name="lazyLoadingEnabled" value="true"/>

      <!--当访问代理对象方法时是否一次加载懒加载对象的所有属性,默认是true -->

      <setting name="aggressiveLazyLoading" value="fasle"/>

      <!--当为懒加载时触发加载对象所有属性的方法,默认是下面四个方法 -->

      <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />

      <!--自动映射类型,可选值为NONEPARTIALFULL,参考AutoMappingBehavior枚举 -->

      <setting name="autoMappingBehavior" value="PARTIAL"/>

      <!--默认的Executor类型,取值参考ExecutorType枚举 -->

      <setting name="defaultExecutorType" value="SIMPLE"/>

      <!--默认的语句超时时间,单位是秒。默认是null超时-->

      <setting name="defaultStatementTimeout" value="10"/>

      <setting name="localCacheScope" value="SESSION"/>

   </settings>

 

1.3     应用浅析

       在我们使用Mybatis的时候都是通过SqlSessionFactoryBuilder对象的build方法来构建一个SqlSessionFactory,然后通过SqlSessionFactory创建一个SqlSession对象,然后所有的操作都是基于sqlSessionselectOneselectListinsertupdatedelete等方法进行的。但是近来我们可能都习惯基于MybatisMapper接口编程,通过SqlSessiongetMapper()方法获取对应的Mapper接口实现,然后就可以基于接口进行操作了。其实SqlSessiongetMapper()调用获取的是基于当前Mapper的一个代理对象实现,其底层在调用对应的接口方法时,最终还是会反应到调用前面提到的selectOneselectListinsert等这些基本方法上。关于这块有兴趣的读者可以沿着上面的线一直查看Mybatis的源码,最终你会找到MapperProxyFactoryMapperProxy

 

(注:本文写于20161228日星期三)

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics