目录java
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--引入外部配置文件--> <properties resource=""/> <!--设置--> <settings/> <!--定义别名--> <typeAliases> <package name=""/> </typeAliases> <!--类型处理器--> <typeHandlers/> <!--对象工厂--> <objectFactory/> <!--插件--> <plugins/> <!--定义数据库信息,默认使用development数据库构建环境--> <environments default="development"> <environment id="development"> <!--jdbc事物管理--> <transactionManager type="JDBC"/> <!--配置数据库链接信息--> <dataSource type="POOLED"/> </environment> </environments> <!--数据库厂商标识--> <databaseIdProvider/> <mappers/> </configuration>
<properties> <property name="zxhtom" value="jdbc:mysql://localhost:3306/mybatis"/> </properties> <dataSource type="POOLED"> <property name="driver" value="${zxhtom}"/> <dataSource>
<properties resource="mybatis.properties"/>
mysql
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"))); configuration.setDefaultEnumTypeHandler(resolveClass(props.getProperty("defaultEnumTypeHandler"))); 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")); configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
参数 | 功能 | 可选值 | 默认值 |
---|---|---|---|
autoMappingBehavior |
指定Mybatis应如何自动映射列到字段上。 NONE : 表示取消自动映射 PARTIAL:只会自动映射没有定义嵌套结果集映射的结果集 FULL : 自动映射任意复杂的结果集 |
NONE、PARTIAL、FULL | PARTIAL |
autoMappingUnknownColumnBehavior |
指定识别到位置列或属性的时间 NONE : 什么都不作 WARNING:日志会报警(前提是日志设置了显示权限) FAILING : 抛出异常。 |
NONE, WARNING, FAILING | NONE |
cacheEnabled |
该配置影响的全部映射器中配置的缓存的全局开关 | true|false | true |
proxyFactory |
指定Mybatis建立具备延迟加载能力的对象所用到的代理工具未指定时将自动查找 | SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING | not set |
lazyLoadingEnabled |
延时加载全局开关 开启时:级联对象会延时加载;级联标签中能够经过fetchType来定制覆盖此选项 |
true|false | false |
aggressiveLazyLoading |
启用时:对任意延迟属性的调用会使带有延迟加载属性的对象分层性质完整加载,反之按需加载 | true|false | true |
multipleResultSetsEnabled |
是否容许单一语句返回多结果集 | true|false | true |
useColumnLabel |
确切的说当映射找不到参数时会使用列标签(数据库列名)代替别名去映射 | true|false | true |
useGeneratedKeys |
容许 JDBC 支持自动生成主键,须要驱动兼容。 若是设置为 true 则这个设置强制使用自动生成主键,尽管一些驱动不能兼容但仍可正常工做(好比 Derby) | true|false | false |
defaultExecutorType |
配置默认的执行器。SIMPLE 就是普通的执行器;REUSE 执行器会重用预处理语句(prepared statements); BATCH 执行器将重用语句并执行批量更新 | SIMPLE REUSE BATCH | SIMPLE |
defaultStatementTimeout |
设置超时时间,决定驱动等待数据库响应的秒数 | 整数 | null |
defaultFetchSize |
设置数据库resultSet读取数据方式,默认所有加载进内存,设置该属性能够设置一次性读多少条数据进内存 | 整数 | null |
mapUnderscoreToCamelCase |
是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的相似映射。 | true|false | false |
safeRowBoundsEnabled |
-容许在嵌套语句中使用分页 | true|false | false |
localCacheScope |
一级缓存。mybatis默认对同一个sqlsession中数据是共享的。一个sqlsession调用两次相同查询实际只会查询一次。就是由于该属性为SESSION , STATEMENT则针对的是每一条sql | SESSION|STATEMENT | SESSION |
jdbcTypeForNull |
当没有为参数提供特定的jdbc类型时,为空值则指定JDBC类型。在新增时咱们没有设置参数,这个时候就会根据此参数天长。加入设置VARCHAR,那么咱们新增的数据没传参数则为空字符 | NULL|VARCHAR|OTHER | OTHER |
lazyLoadTriggerMethods |
指定具体方法延时加载 | 方法 | equals,clone,hashCode,toString |
safeResultHandlerEnabled |
容许在嵌套语句中使用分页 | true|false | true |
defaultScriptingLanguage |
动态SQL生成的默认语言 | org.apache.ibatis.scripting.xmltags.XMLDynamicLanguageDriver |
|
defaultEnumTypeHandler |
mybatis默认的枚举处理类 | ||
callSettersOnNulls |
指定当结果集中值为null的时候是否调用映射对象的setter(put)方法。 | ||
useActualParamName |
容许使用他们的编译后名称来映射,3.4.2后默认true.在xml中#{0}则报错。设置为false,则#{0}表明第一个参数#{n}第n个 | true|false | true |
returnInstanceForEmptyRow |
当返回行的全部列都是空时,MyBatis默认返回 null。 当开启这个设置时,MyBatis会返回一个空实例。 请注意,它也适用于嵌套的结果集 (如集合或关联)。(新增于 3.4.2) | true|false | false |
logPrefix |
指定 MyBatis 增长到日志名称的前缀。 |
org.apache.ibatis.type.TypeAliasRegistry
这个类中帮咱们内置了别名。能够看下。自定义别名也是经过这个类进行注册的。咱们能够经过settings中typeAliases配置的方式结合@Alias。或者扫描包也能够的。public interface TypeHandler<T> { /** * 设置参数是用到的方法 */ void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; T getResult(ResultSet rs, String columnName) throws SQLException; T getResult(ResultSet rs, int columnIndex) throws SQLException; T getResult(CallableStatement cs, int columnIndex) throws SQLException; }
org.apache.ibatis.type.TypeHandlerRegistry
这个类中mybatis为咱们提供了内置的TypeHandler。基本上是对于基本数据和分装对象的转换。public class BooleanTypeHandler extends BaseTypeHandler<Boolean> { @Override public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException { ps.setBoolean(i, parameter); } @Override public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException { boolean result = rs.getBoolean(columnName); return !result && rs.wasNull() ? null : result; } @Override public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException { boolean result = rs.getBoolean(columnIndex); return !result && rs.wasNull() ? null : result; } @Override public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { boolean result = cs.getBoolean(columnIndex); return !result && cs.wasNull() ? null : result; } }
private Class<? extends TypeHandler> defaultEnumTypeHandler = EnumTypeHandler.class;
。@Override public E getNullableResult(ResultSet rs, String columnName) throws SQLException { String s = rs.getString(columnName); return s == null ? null : Enum.valueOf(type, s); }
在Enum中还有一个属性oridinal。这个表示枚举中的索引。而后咱们经过查看Mybatis提供的处理器发现有个叫EnumOrdinalTypeHandler
。咱们很容易联想到的就是这个处理器是经过枚举的因此做为数据库内容的。在SexEnum中MALE存储到数据库中则为0.注意这个0不是咱们的index.而是MALE的索引。若是将MALE和FEMAEL调换。那么MALE索引则为1.git
由于默认的是EnumTypeHandler。因此想用EnumOrdinalTypeHandler的话咱们要么在resultMap中sex字段指定该处理器。要不就经过配置文件typeHandlers注册进来。(将处理器与Java类进行绑定。mybatis遇到这个Java对象的时候就知道用什么处理器处理)github
<typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.github.zxhtom.enums.SexEnum"/> </typeHandlers>
public class SexTypeHandler extends BaseTypeHandler<SexEnum> { @Override public void setNonNullParameter(PreparedStatement ps, int i, SexEnum parameter, JdbcType jdbcType) throws SQLException { ps.setInt(i,parameter.getIndex()); } @Override public SexEnum getNullableResult(ResultSet rs, String columnName) throws SQLException { int i = rs.getInt(columnName); return SexEnum.getSexEnum(i); } @Override public SexEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException { int i = rs.getInt(columnIndex); return SexEnum.getSexEnum(i); } @Override public SexEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { int i = cs.getInt(columnIndex); return SexEnum.getSexEnum(i); } }
加入战队sql