一、概述
MyBatis配置項的順序不能顛倒,否則可能發生異常。
二、各個配置項說明
1、properties
該屬性可以給系統配置一些運行參數,可以直接在基本配置文件中即SqlSessionFactory的配置文件中直接配置:
<properties>
? ? ? <property name="database.driver" value="com.mysql.cj.jdbc.Driver"/>
? ? ? <property name="database.url" value="jdbc:mysql://localhost:3306/wz_db" />
? ? ? <property name="database.username" value="root" />
? ? ? <property name="database.password" value="123456" />
? ? </properties>
使用:
<dataSource type="POOLED">
? ? ? ? ? <property name="driver" value="${database.driver}"/>
? ? ? ? ? <property name="url" value="${database.url}"/>
? ? ? ? ? <property name="username" value="${database.username}" />
? ? ? ? ? <property name="password" value="${database.password}" />
? ? ? ? </dataSource>
或者通過額外的properties配置文件配置:
<properties resource="jdbc.properties" />
在某些情況下,properties文件中的數據庫用戶名和密碼是加密的,這時就需要通過代碼傳遞property參數了。代碼如下:
String resource = "mybatis-config.xml"http://基礎配置文件
InputStream inputStream;
InputSream is = Resource.getResourceAsStream("jdbc.properties");
Properties props = new Properties();
props.load(is);
String userName = props.getProperty("database.username");
String password = props.getProperty("database.password");
//解密用戶名和密碼
props.put("database.username", CodeUtils.decode(userName));
props.put("database.password", CodeUtils.decode(password));
inputStream = Resource.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream, props);
總結:三種配置方式優先級:代碼方式>properties文件方式>property子元素方式
2、settings
settings是MyBatis中最復雜的配置,它能深刻影響MyBatis底層的運行,常用的配置項有:
關于緩存的cacheEnabled、關于級聯的lazyLoadingEnabled和aggresiveLazyLoading、關于自動映射的autoMappingBehavior和mapUnderscoreToCamelCase、關于執行器類型的defaultExecutorType等。
3、typeAliases 別名
別名不區分大小寫
3.1、mybatis已經定義的一些別名:
如果要使用對應類型的數組類型,查表看mybatis是否支持,然后別名加[]即可,如
_int[]。
3.2、自定義別名
3.2.1 通過代碼注冊別名
通過Configuration獲取TypeAliasRegistry類對象:
TypeAliasRegistry aliasRegistry = configuration.getTypeAliasRegistry();
使用registerAlias()方法注冊別名:
aliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
3.2.2 在配置文件中配置別名
在MyBatis基礎配置文件即SqlSessionFactory配置文件中:
<typeAliases>
? ? ?<typeAlias alias="role" type="com.mybatis.learn.Role" />
</typeAliases>
3.2.3、通過包名
<typeAliases>
<package name="com.mybatis.learn.chapter03.pojo" />
</typeAliases>
MyBatis將掃描這個包里所有的類,類名的第一個字母變為小寫作為其別名,但同時掃描幾個包時,可能出現重名的情況如com.chapter03.pojo和com.chapter04.pojo包中都存在類名為User的類,這時候可以通過注解@Alias("user3")進行區分。
4、typeHandler類型轉換器
typeHandler的作用是將jdbcType和javaType互相轉換。MyBatis提供了大部分數據類型轉換,但是對于枚舉類型,我們往往需要通過自定義TypeHandler去實現轉換。
4.1 自定義TypeHandler
自定義TypeHandler需要實現TypeHandler接口
public class SexEnumTypeHandler implements TypeHandler<sexEnum> {
@Override
public sexEnum getResult(ResultSet rs, String columnName) throws SQLException {
return sexEnum.getSexById(rs.getInt(columnName));
}
@Override
public sexEnum getResult(ResultSet rs, int i) throws SQLException {
return sexEnum.getSexById(rs.getInt(i));
}
@Override
public sexEnum getResult(CallableStatement cs, int i) throws SQLException {
return sexEnum.getSexById(cs.getInt(i));
}
@Override
public void setParameter(PreparedStatement ps, int i, sexEnum sex, JdbcType jdbcType) throws SQLException {
ps.setInt(i, sex.getId());
}
}
枚舉類型 sexEnum:
public enum sexEnum{
MALE(0, "男"), FEMALE(1, "女");
? ? private int id;
? ? private String sex;
private sexEnum(int id, String sex) {
this.id = id;
this.sex = sex;
}
public static sexEnum getSexById(int id){
for(sexEnum s : sexEnum.values()) {
if(s.getId() == s.id) {
return s;
}
return null;
}
/**getter and setter**/
}
啟用自定義TypeHandler:
采用掃包+注解方式啟用: