本文只论mybatis本身,不涉及与spring整合,文中探讨了mybatis最新版本提供的全部配置项的作用。
首先要了解都有哪些配置项,mybatis的SqlSession来自SqlSessionFactory,SqlSessionFactory来自SqlSessionFactoryBuilder,从SqlSessionFactoryBuilder切入分析
... public SqlSessionFactory build(Reader reader, String environment, Properties properties) { try { XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); return build(parser.parse()); } catch (Exception e) { throw ExceptionFactory.wrapException("Error building SqlSession.", e); } finally { ErrorContext.instance().reset(); try { reader.close(); } catch (IOException e) { // Intentionally ignore. Prefer previous error. } } } ...
构造SqlSessionFactoryBuilder用到了XMLConfigBuilder,然后看XMLConfigBuilder
public Configuration parse() { if (parsed) { throw new BuilderException("Each XMLConfigBuilder can only be used once."); } parsed = true; parseConfiguration(parser.evalNode("/configuration")); return configuration; } private void parseConfiguration(XNode root) { try { //issue #117 read properties first propertiesElement(root.evalNode("properties")); Properties settings = settingsAsProperties(root.evalNode("settings")); loadCustomVfs(settings); typeAliasesElement(root.evalNode("typeAliases")); pluginElement(root.evalNode("plugins")); objectFactoryElement(root.evalNode("objectFactory")); objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); reflectorFactoryElement(root.evalNode("reflectorFactory")); settingsElement(settings); // read it after objectFactory and objectWrapperFactory issue #631 environmentsElement(root.evalNode("environments")); databaseIdProviderElement(root.evalNode("databaseIdProvider")); typeHandlerElement(root.evalNode("typeHandlers")); mapperElement(root.evalNode("mappers")); } catch (Exception e) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); } } private void settingsElement(Properties props) throws Exception { configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL"))); configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE"))); 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"), false)); 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"))); @SuppressWarnings("unchecked") Class<? extends TypeHandler> typeHandler = (Class<? extends TypeHandler>)resolveClass(props.getProperty("defaultEnumTypeHandler")); configuration.setDefaultEnumTypeHandler(typeHandler); configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false)); configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true)); configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false)); configuration.setLogPrefix(props.getProperty("logPrefix")); @SuppressWarnings("unchecked") Class<? extends Log> logImpl = (Class<? extends Log>)resolveClass(props.getProperty("logImpl")); configuration.setLogImpl(logImpl); configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory"))); }
configuration节点为根节点。
可以配置10个子节点:properties、settings、typeAliases、plugins、objectFactory、objectWrapperFactory、environments、databaseIdProvider、typeHandlers、mappers。
properties
这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递。例如:
<!-- mybatis-config.xml --> <properties resource="jdbc.properties"></properties> <!-- mybatis-config.xml --> <properties> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/a"/> <property name="username" value="root"/> <property name="password" value="root"/> </properties>settings
settings
这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。
<!-- mybatis-config.xml --> <settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="defaultFetchSize" value="100"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings>
懒加载
熟悉配置前需要知道什么是懒加载
public class Order { public Long id; public Long addressId; public Address address; }
public class Address { public Long id; public String name; }
<!-- addressMapper.xml --> <mapper namespace="addressMapperSpace"> <select id="getAddressById" parameterType="Long" resultType="Address"> select id,name from t_address where id = #{id} </select> </mapper>
<!-- orderMapper.xml --> <mapper namespace="..."> <resultMap id="orderMap" type="Order"> <id property="id" column="id" /> <association property="address" column="address_id" select="addressMapperSpace.getAddressById" /> </resultMap> <select id="getOrderById" resultMap="orderMap" parameterType="Long"> select id,address_id from t_order where id = #{id} <select> </mapper>
如果是懒加载,那么访问order的address属性时才会去查询address。
参数介绍
参数 |
官方中文描述 |
理解 |
可选值 |
默认值 |
---|---|---|---|---|
cacheEnabled |
全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存。 |
mybatis缓存,不支持集群环境,必须设置成false。 |
true | false |
true |
lazyLoadingEnabled |
延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置fetchType属性来覆盖该项的开关状态 |
可以不设置 |
true | false |
false |
aggressiveLazyLoading |
当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载(参考lazyLoadTriggerMethods). |
当设置为true时,懒加载的对象可能被任何懒属性全部加载;否则,每个属性按需加载。一般不用。 可以不设置 |
true | false |
false (true in ≤3.4.1) |
lazyLoadTriggerMethods |
指定对象的哪个方法触发一次延迟加载。 |
在lazyLoadingEnabled=true时有效,调用本方法会使得所有延迟加载属性被加载,如果有多个懒加载属性,可以使用这个方法把所有懒加载属性一起加载了。 可以不设置 |
用逗号分隔的方法列表。 |
equals,clone,hashCode,toString |
proxyFactory |
指定 Mybatis 创建具有延迟加载能力的对象所用到的代理工具。 |
mybatis延迟加载用的工具,旧版本使用的是CGLIB动态代理技术,新版本支持使用JAVASSIST(Javassist是一个运行时编译库,他能动态的生成或修改类的字节码)来完成。 可以不设置 |
CGLIB | JAVASSIST |
JAVASSIST (MyBatis 3.3 or above) |
multipleResultSetsEnabled |
是否允许单一语句返回多结果集(需要兼容驱动)。 |
sql与ResultSet一对多的用法, 没找到用法。 可以不设置 |
true | false |
true |
useColumnLabel |
使用列标签代替列名。不同的驱动在这方面会有不同的表现, 具体可参考相关驱动文档或通过测试这两种不同的模式来观察所用驱动的结果。 |
在Select字段的时候使用AS,用得上,由于默认true。 可以不设置 |
true | false |
true |
useGeneratedKeys |
允许 JDBC 支持自动生成主键,需要驱动兼容。 如果设置为 true 则这个设置强制使用自动生成主键,尽管一些驱动不能兼容但仍可正常工作(比如 Derby)。 |
我们使用mysql数据库自增主键,在xml的insert块中如果使用useGeneratedKeys来获得生成的主键,那这个属性必须设置成true。如果使用以下方法,那也可以不设置。 <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long"> SELECT LAST_INSERT_ID() </selectKey> |
true | false |