1 Configuration Structure
- properties
- settings
- typeAliases
- typeHandlers
- objectFactory
- plugins
-
environments
- environment
- transactionManager
- dataSource
- environment
- databaseIdProvider
- mappers
2 Plugins
MyBatis allows you to intercept calls to at certain points within the execution of a mapped statement.
作用:MyBatis 允许你在映射语句执行期间的某一点进行拦截调用。
2.1 可以通过 Plugin 拦截的方法
By default, MyBatis allows plug-ins to intercept method calls of:
默认情况下,MyBatis 允许使用插件来拦截的方法包括:
- Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler (getParameterObject, setParameters)
- ResultSetHandler (handleResultSets, handleOutputParameters)
- StatementHandler (prepare, parameterize, batch, update, query)
The details of these classes methods can be discovered by looking at the full method signature of each, and the source code which is available with each MyBatis release.
这些类的方法的实现细节可以通过查看每个方法的全部方法签名查看,每个 MyBatis 的发行包都可以查看源码。
You should understand the behaviour of the method you’re overriding, assuming you’re doing something more than just monitoring calls. If you attempt to modify or override the behaviour of a given method, you’re likely to break the core of MyBatis. These are low level classes and methods, so use plug-ins with caution.
注意:如果你要做的不仅仅是监控方法执行,你最好理解你所要重写的方法的行为。如果你试图修改或重写既有方法,你很有可能在破坏 MyBatis 的核心模块。这些是底层的类和方法,所以请谨慎使用插件。
2.2 自定义 Plugin 实现
Using plug-ins is pretty simple given the power they provide. Simply implement the Interceptor interface, being sure to specify the signatures you want to intercept.
通过 MyBatis 提供的强大机制,使用插件是相当简单的。仅仅需要实现 Interceptor 接口,并且指定你需要拦截的方法签名即可。
注意,参数必须与对应方法匹配,否则会抛出NoSuchMethodException。
// ExamplePlugin.java
@Intercepts({@Signature(
type= Executor.class,
method = "update",
args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}
<!-- mybatis-config.xml -->
<plugins>
<plugin interceptor="org.mybatis.example.ExamplePlugin">
<property name="someProperty" value="100"/>
</plugin>
</plugins>
The plug-in above will intercept all calls to the "update" method on the Executor instance, which is an internal object responsible for the low level execution of mapped statements.
上面的插件将会拦截对Executor 实例上的所有 "update" 的方法调用,这里的 Executor 是执行底层映射语句的内部对象。
Note:In addition to modifying core MyBatis behaviour with plugins, you can also override the Configuration class entirely. Simply extend it and override any methods inside, and pass it into the call to the SqlSessionFactoryBuilder.build(myConfig) method. Again though, this could have a severe impact on the behaviour of MyBatis, so use caution.
注意:除了使用插件修改 MyBatis 核心模块的行为,你也可以重写整个 Configuration 类。只需要继承它并且重写所有的内部方法,并将它传递给 SqlSessionFactoryBuilder.build(myConfig) 方法。再一次强调,这将对 MyBatis 的行为有严重的影响,所以务必谨慎使用。
最后
说明:MyBatis 官网提供了简体中文的翻译,但个人觉得较为生硬,甚至有些地方逻辑不通,于是自己一个个重新敲着翻译的(都不知道哪里来的自信...),有些地方同官网翻译有出入,有些倔强地保留了自己的,有的实在别扭则保留了官网的,这些都会在实践中一一更正。鉴于个人英文能力有限,文章中保留了官方文档原英文介绍(个别地方加以调整修剪),希望有缘看到这里的朋友们能够有自己的理解,不会被我可能错误或不合理的翻译带跑偏(〃'▽'〃),欢迎指正!
当前版本:mybatis-3.5.0
官网文档:MyBatis
官网翻译:MyBatis 简体中文
项目实践:MyBatis Learn