Mybatis核心杂谈
来源:互联网 发布:centos 6 ssh拒绝访问 编辑:程序博客网 时间:2024/06/03 18:15
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>。具体可以定义哪些配置,分别代表什么含义,请读者参考XMLConfigBuilder的settingsElement()方法,以下是该方法的源码。
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>
<!--日志实现,内置的有SLF4J、COMMONS_LOGGING、LOG4J、LOG4J2、JDK_LOGGING、STDOUT_LOGGING、NO_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" />
<!--自动映射类型,可选值为NONE、PARTIAL和FULL,参考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等这些基本方法上。关于这块有兴趣的读者可以沿着上面的线一直查看Mybatis的源码,最终你会找到MapperProxyFactory和MapperProxy。
(注:本文写于2016年12月28日星期三)
- Mybatis核心杂谈
- Mybatis核心杂谈
- mybatis核心配置文件详解
- mybatis核心配置文件
- mybatis核心组件
- MyBatis核心配置文件
- mybatis核心讲解
- mybatis核心配置文件
- MyBatis的核心组件
- MyBatis核心API测试
- mybatis核心配置文件示例
- 02.Mybatis初级使用-Mybatis核心组件
- Mybatis基础,mybatis核心配置文件properties元素
- mybatis核心配置文件mybatis-config.xml
- MyBatis核心SqlSessionFactory的创建
- MyBatis核心SqlSessionFactory的创建
- mybatis基础与核心类
- MyBatis核心SqlSessionFactory的创建
- C++中sort函数和 stable_sort函数的区别
- 虚幻4 多语言支持
- 将win32程序移植到linux注意事项
- [BZOJ1178][Apio2009]CONVENTION会议中心(单调栈+dp+set)
- Android 关于WebView全方面的使用(项目应用篇)
- Mybatis核心杂谈
- Spring 框架面试题总结
- apache的MultiViews的问题
- js笔记
- 递归建立链表单一head变量
- openCV-基础
- 设计模式-单例模式
- LightOJ1079-Just another Robbery
- 【编程素质】算法-合并排序