effect
Official description:
MyBatis allows you to intercept calls at a certain point during the execution of a mapped statement.
What does it mean? That is, you can intercept some methods before executing them and do some of your own operations, such as:
1. Record all executed SQL (by intercepting the prepare method in MyBatis org.apache.ibatis.executor.statement.StatementHandler)
2. Modify SQL (org.apache.ibatis.executor.Executor for intercepting) and so on.
However, there are restrictions on intercepting method calls. MyBatis allows plugins to intercept method calls include:
accomplish
Using plug-ins is very simple. Just implement the Interceptor interface and specify the method signature you want to intercept.
// ExamplePlugin.java@Intercepts({@Signature( type= Executor.class, method = "update", args = {MappedStatement.class,Object.class}, @Signature( type = Executor.class, //The method supported above is the method = "query", //The supported methods in the class can be viewed from the source code which methods are supported args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}) //The corresponding parameter Class can also be viewed from the source code public class ExamplePlugin implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { Object[] queryArgs = invocation.getArgs(); MappedStatement mappedStatement = (MappedStatement) queryArgs[0]; Object parameter = queryArgs[1]; BoundSql boundSql = mappedStatement.getBoundSql(parameter); String sql = boundSql.getSql();//Get SQL , you can adjust String name = invocation.getMethod().getName(); queryArgs[1] = 2; //You can modify the parameter content System.err.println("The method name of the intercept is: " + name); return invocation.proceed(); } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties properties) { }}Register plugin in configuration file
<!-- mybatis-config.xml --><plugins> <plugin interceptor="org.mybatis.example.ExamplePlugin"> <property name="someProperty" value="100"/> </plugin></plugins>
When we call the query method, we match the intercept method, so we will execute the intercept method under the intercept and do our own processing.
References, official website
http://www.mybatis.org/mybatis-3/zh/configuration.html#plugins
Summarize
The above is the MyBatis custom Plugin plugin introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!