From bd497d27b679aea474422dcf8917b3ea3ef01231 Mon Sep 17 00:00:00 2001 From: fjn <1194361820@qq.com> Date: Wed, 28 Jul 2021 17:08:12 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E6=B5=8B=E8=AF=95=E9=A2=9D=E5=A4=96?= =?UTF-8?q?=E7=9A=84=E8=87=AA=E5=AE=9A=E4=B9=89=20interceptor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/test.h2.db | Bin 73728 -> 73728 bytes .../config/MybatisInterceptorConfig.java | 36 +++++++++++ .../mybatis/plugins/Interceptor0.java | 58 ++++++++++++++++++ .../mybatis/plugins/Interceptor1.java | 58 ++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/config/MybatisInterceptorConfig.java create mode 100644 sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/plugins/Interceptor0.java create mode 100644 sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/plugins/Interceptor1.java diff --git a/sqlhelper-examples/sqlhelper-examples-db/src/main/resources/test.h2.db b/sqlhelper-examples/sqlhelper-examples-db/src/main/resources/test.h2.db index 1a3dbabf7e96f87e268f4bfee78f9060b747cc5b..baef74f4d5f8b98b09675b36d100d2c9ab78591f 100644 GIT binary patch delta 58 scmZoTz|wGlWr2XeKHVh|3}BF`z`(#-u~|^yJ-;9WR^iS6^%*|^0K#Gqng9R* delta 58 scmZoTz|wGlWr2Xe(S2U#3}BE@$iTpwxmi%)J-;9WR^iS6^%*|^0NL0ORsaA1 diff --git a/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/config/MybatisInterceptorConfig.java b/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/config/MybatisInterceptorConfig.java new file mode 100644 index 000000000..7557404ee --- /dev/null +++ b/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/config/MybatisInterceptorConfig.java @@ -0,0 +1,36 @@ +/* + * Copyright 2021 the original author or authors. + * + * Licensed under the Apache, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.gnu.org/licenses/lgpl-2.0.html + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.jn.sqlhelper.examples.mybatis.config; + +import com.jn.sqlhelper.examples.mybatis.plugins.Interceptor0; +import com.jn.sqlhelper.examples.mybatis.plugins.Interceptor1; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; + +@Configuration +public class MybatisInterceptorConfig { + @Bean + @Order(Integer.MAX_VALUE) + public Interceptor0 interceptor0(){ + return new Interceptor0(); + } + + @Bean + @Order(Integer.MIN_VALUE) + public Interceptor1 interceptor1(){ + return new Interceptor1(); + } +} diff --git a/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/plugins/Interceptor0.java b/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/plugins/Interceptor0.java new file mode 100644 index 000000000..3037eae84 --- /dev/null +++ b/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/plugins/Interceptor0.java @@ -0,0 +1,58 @@ +/* + * Copyright 2021 the original author or authors. + * + * Licensed under the Apache, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.gnu.org/licenses/lgpl-2.0.html + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.jn.sqlhelper.examples.mybatis.plugins; + +import org.apache.ibatis.cache.CacheKey; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.plugin.*; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Properties; + +@Intercepts({ + @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), + @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), + @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}), + @Signature(type = Executor.class, method = "queryCursor", args = {MappedStatement.class, Object.class, RowBounds.class}), + @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) +}) +public class Interceptor0 implements Interceptor { + private static final Logger logger = LoggerFactory.getLogger(Interceptor0.class); + @Override + public Object intercept(Invocation invocation) throws Throwable { + logger.info("------------------Interceptor0 start---------------"); + Object obj = invocation.proceed(); + logger.info("------------------Interceptor0 end---------------"); + return obj; + } + + @Override + public Object plugin(Object target) { + if (target instanceof Executor) { + return Plugin.wrap(target, this); + } + return target; + } + + @Override + public void setProperties(Properties properties) { + + } +} diff --git a/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/plugins/Interceptor1.java b/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/plugins/Interceptor1.java new file mode 100644 index 000000000..4337d05fa --- /dev/null +++ b/sqlhelper-examples/sqlhelper-examples-service/sqlhelper-examples-service-mybatis/src/main/java/com/jn/sqlhelper/examples/mybatis/plugins/Interceptor1.java @@ -0,0 +1,58 @@ +/* + * Copyright 2021 the original author or authors. + * + * Licensed under the Apache, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.gnu.org/licenses/lgpl-2.0.html + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.jn.sqlhelper.examples.mybatis.plugins; + +import org.apache.ibatis.cache.CacheKey; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.plugin.*; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Properties; + +@Intercepts({ + @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), + @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), + @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}), + @Signature(type = Executor.class, method = "queryCursor", args = {MappedStatement.class, Object.class, RowBounds.class}), + @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) +}) +public class Interceptor1 implements Interceptor { + private static final Logger logger = LoggerFactory.getLogger(Interceptor1.class); + @Override + public Object intercept(Invocation invocation) throws Throwable { + logger.info("------------------Interceptor1 start---------------"); + Object obj = invocation.proceed(); + logger.info("------------------Interceptor1 end---------------"); + return obj; + } + + @Override + public Object plugin(Object target) { + if (target instanceof Executor) { + return Plugin.wrap(target, this); + } + return target; + } + + @Override + public void setProperties(Properties properties) { + + } +}