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 1a3dbabf7..baef74f4d 100644 Binary files a/sqlhelper-examples/sqlhelper-examples-db/src/main/resources/test.h2.db and b/sqlhelper-examples/sqlhelper-examples-db/src/main/resources/test.h2.db differ 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) { + + } +}