梦想还是要有的,万一忘了咋办?

0%

MyBatis配置

属性(properties)

可以通过三个地方进行属性配置,优先级逐减,向下覆盖。

1、Java代码配置

1
2
3
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
// ... 或者 ...
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment, props);

2、resource、url读取配置

1
2
3
4
5
6
7
<properties resource="org/mybatis/example/config.properties">
<!-- .... -->
</properties>
//... 或者 ...
<properties url="http://xxx.xxx.xxx/config.html">
<!-- .... -->
</properties>

3、property配置

1
2
3
4
<properties >
<property name="username" value="dev_user"/>
<property name="password" value="F2Fa3!33TYyg"/>
</properties>

启用默认值语法

1、开启默认值语法支持

1
2
3
4
5
6
7
<properties resource="org/mybatis/example/config.properties">
<!-- ... -->
<property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true" />
<!-- 启用默认值特性 -->
 <!-- value 可以是boolean值 true ,此时默认语法符号是”:“-->
 <!-- value 也可以是 自定义 语法符号 ”?:“ 防止与三元运算符冲突 -->
</properties>

2、默认值语法使用

1
2
3
4
<dataSource type="POOLED">
<!-- ... -->
<property name="username" value="${username?:ut_user}"/> <!-- 如果属性 'username' 没有被配置,'username' 属性的值将为 'ut_user' -->
</dataSource>

设置(settings)

这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。 这里不做介绍,可以移步官网查看** 设置中各项设置的含义、默认值等**

类型别名(typeAliases)

类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。

两种设置方式

1、xml中配置

1
2
3
4
5
6
7
8
9
<typeAliases>
<typeAlias alias="Author" type="domain.blog.Author"/>
<typeAlias alias="Blog" type="domain.blog.Blog"/>
</typeAliases>
//-----配置某个包路径----
//此时,以类首字母小写作为别名,例如 domain.blog.Author 的别名为 author
<typeAliases>
<package name="domain.blog"/>
</typeAliases>

2、注解

1
2
3
4
@Alias("author")
public class Author {
...
}

3、常见别名

请移步 官网类型别名介绍

类型处理器

MyBatis 在设置预处理语句(PreparedStatement)中的参数或从结果集中取出一个值时, 都会用类型处理器将获取到的值以合适的方式转换成 Java 类型。下表描述了一些默认的类型处理器。
提示 从 3.4.5 开始,MyBatis 默认支持 JSR-310(日期和时间 API)

1、内置处理器

请移步 官网类型处理器介绍

2、自定义类型处理器

实现 org.apache.ibatis.type.TypeHandler 接口,
或继承 org.apache.ibatis.type.BaseTypeHandler,

1
2
3
4
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ExampleTypeHandler extends BaseTypeHandler<String> {

}

添加xml配置

1
2
3
4
<typeHandlers>
 <typeHandler handler="org.mybatis.example.ExampleTypeHandler" jdbcType="VARCHAR" javaType="String"/>
</typeHandlers>

绑定jdbc类型方式

  • 实现类上增加 MappedJdbcTYpes 注解
  • typehandler xml配置中增加 jdbcType属性,优先级高于注解

绑定Java类型方式

  • 实现类上增加 MappedTypes 注解
  • typehandler xml配置中增加 javaType属性,优先级高于注解

3、处理枚举

可以处理Enum类型的有

  • EnumTypeHandler  
    Enum值转化成对应的名字,可以处理任何Enum的子类。默认用的这个
  • EnumOrdinalTypeHandler
    将Enum转换成对应的ordinal进行存储,ordinal的值取决于枚举的顺序,从0开始。改变枚举顺序会影响ordinal的值;
    如果要使用可以通过反射重写ordinal的值。

4、插件

MyBatis 允许你在映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:

  • Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
  • ParameterHandler (getParameterObject, setParameters)
  • ResultSetHandler (handleResultSets, handleOutputParameters)
  • StatementHandler (prepare, parameterize, batch, update, query)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// ExamplePlugin.java
@Intercepts({@Signature(
type= Executor.class,
method = "update",
args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
private Properties properties = new Properties();
public Object intercept(Invocation invocation) throws Throwable {
// implement pre processing if need
Object returnObject = invocation.proceed();
// implement post processing if need
return returnObject;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}

<!-- mybatis-config.xml -->
<plugins>
<plugin interceptor="org.mybatis.example.ExamplePlugin">
<property name="someProperty" value="100"/>
</plugin>
</plugins>

4、环境配置(environments)

包含环境、事务、数据源几部分的配置。
请移步 官方环境配置介绍

5、数据库厂商标识(databaseIdProvider)

请移步 官方数据厂商标识介绍