Mybatis核心杂谈

简介:   Mybatis杂谈   目录 1.1        配置解析 1.2        全局配置简介 1.3        应用浅析            本文主要介绍Mybatis的配置解析对应的Class、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日星期三)

 

 

目录
相关文章
|
4月前
|
SQL Java 数据库连接
MyBatis-Plus 实战教程二 核心功能(一)
MyBatis-Plus 实战教程二 核心功能
55 0
|
4月前
|
SQL
MyBatis-Plus 实战教程二 核心功能(三)
MyBatis-Plus 实战教程二 核心功能
56 0
|
4月前
|
XML Java 数据库连接
干翻Mybatis源码系列之第五篇:Mybatis中核心运行机制超级详细版
干翻Mybatis源码系列之第五篇:Mybatis中核心运行机制超级详细版
|
4月前
|
设计模式 缓存 Java
干翻Mybatis源码系列之第七篇:Mybatis提供的集成缓存方案
干翻Mybatis源码系列之第七篇:Mybatis提供的集成缓存方案
|
4月前
|
SQL
MyBatis-Plus 实战教程二 核心功能(二)
MyBatis-Plus 实战教程二 核心功能
35 0
|
SQL Oracle Java
MyBatis框架:第六章:mybatis的核心配置
MyBatis框架:第六章:mybatis的核心配置
157 0
MyBatis框架:第六章:mybatis的核心配置
|
SQL XML Oracle
Mybatis基础笔记四
Mybatis基础笔记四
|
SQL XML Java
Mybatis基础笔记五
Mybatis基础笔记五
|
SQL Java 数据库连接
mybatis基础笔记三
mybatis基础笔记三
|
SQL Java 关系型数据库
mybatis基础笔记一
mybatis基础笔记一