Mybatis核心杂谈

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

1.1     配置解析

       Mybatis有一堆的配置,有Mybatis的全局配置文件,有Mapper.xml文件,Mapper.xml文件里面又有select、insert等MappedStatement,MappedStatement里面又包含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>。以下是该方法的源码下载

 

  private void settingsElement(Properties propsthrows 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对象,然后所有的操作都是基于sqlSession的selectOne、selectList、insert、update和delete等方法进行的。但是近来我们可能都习惯基于Mybatis的Mapper接口编程,通过SqlSession的getMapper()方法获取对应的Mapper接口实现,然后就可以基于接口进行操作了。其实SqlSession的getMapper()调用获取的是基于当前Mapper的一个代理对象实现,其底层在调用对应的接口方法时,最终还是会反应到调用前面提到的selectOne、selectList、insert等这些基本方法上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值