From cacba12b94c965665c91a150c7b621e38da95789 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Fri, 24 Nov 2023 21:48:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20MyBatis=20Plus=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE=EF=BC=88=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E6=A8=A1=E5=9D=97=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pom.xml | 37 +++++++ .../MyBatisPlusExtensionProperties.java | 84 ++++++++++++++ .../MybatisPlusAutoConfiguration.java | 103 ++++++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../src/main/resources/spy.properties | 30 +++++ continew-starter-data/pom.xml | 29 +++++ continew-starter-dependencies/pom.xml | 36 +++++- pom.xml | 1 + 8 files changed, 319 insertions(+), 2 deletions(-) create mode 100644 continew-starter-data/continew-starter-data-mybatis-plus/pom.xml create mode 100644 continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MyBatisPlusExtensionProperties.java create mode 100644 continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MybatisPlusAutoConfiguration.java create mode 100644 continew-starter-data/continew-starter-data-mybatis-plus/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 continew-starter-data/continew-starter-data-mybatis-plus/src/main/resources/spy.properties create mode 100644 continew-starter-data/pom.xml diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml b/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml new file mode 100644 index 00000000..d6aaa183 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter-data + ${revision} + + + continew-starter-data-mybatis-plus + jar + + ${project.artifactId} + ContiNew Starter 数据访问模块 - MyBatis Plus + + + + + com.baomidou + mybatis-plus-spring-boot3-starter + + + + + com.baomidou + dynamic-datasource-spring-boot3-starter + + + + + p6spy + p6spy + + + \ No newline at end of file diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MyBatisPlusExtensionProperties.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MyBatisPlusExtensionProperties.java new file mode 100644 index 00000000..ad61fe82 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MyBatisPlusExtensionProperties.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * 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.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 top.charles7c.continew.starter.data.mybatis.plus.autoconfigure; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + + +/** + * MyBatis Plus 扩展配置属性 + * + * @author Charles7c + * @since 1.0.0 + */ +@Data +@ConfigurationProperties(prefix = "mybatis-plus.extension") +public class MyBatisPlusExtensionProperties { + + /** + * 是否启用扩展 + */ + private boolean enabled = false; + + /** + * Mapper 所在包(配置时必须使用:mapper-package 键名) + *

+ * e.g. com.example.**.mapper + *

+ */ + private String mapperPackage; + + /** + * 数据权限处理器实现类 + */ + private Class dataPermissionHandlerImpl; + + /** + * 分页插件配置 + */ + private PaginationProperties pagination; + + /** + * 分页插件配置属性 + */ + @Data + public static class PaginationProperties { + + /** + * 是否启用分页插件 + */ + private boolean enabled = true; + + /** + * 数据库类型 + */ + private DbType dbType; + + /** + * 是否溢出处理 + */ + private boolean overflow = false; + + /** + * 单页分页条数限制(默认:-1 表示无限制) + */ + private Long maxLimit = -1L; + } +} diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MybatisPlusAutoConfiguration.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MybatisPlusAutoConfiguration.java new file mode 100644 index 00000000..1f6df2a2 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/MybatisPlusAutoConfiguration.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * 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.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 top.charles7c.continew.starter.data.mybatis.plus.autoconfigure; + +import cn.hutool.core.net.NetUtil; +import cn.hutool.core.util.ReflectUtil; +import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator; +import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler; +import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import jakarta.annotation.PostConstruct; +import lombok.extern.slf4j.Slf4j; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +/** + * MyBatis Plus 配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@AutoConfiguration +@MapperScan("${mybatis-plus.extension.mapper-package}") +@EnableTransactionManagement(proxyTargetClass = true) +@EnableConfigurationProperties(MyBatisPlusExtensionProperties.class) +@ConditionalOnProperty(prefix = "mybatis-plus.extension", name = "enabled", havingValue = "true") +public class MybatisPlusAutoConfiguration { + + /** + * MyBatis Plus 插件配置 + */ + @Bean + @ConditionalOnMissingBean + public MybatisPlusInterceptor mybatisPlusInterceptor(MyBatisPlusExtensionProperties properties) { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + // 数据权限插件 + Class dataPermissionHandlerImpl = properties.getDataPermissionHandlerImpl(); + if (null != dataPermissionHandlerImpl) { + interceptor.addInnerInterceptor(new DataPermissionInterceptor(ReflectUtil.newInstance(dataPermissionHandlerImpl))); + } + // 分页插件 + MyBatisPlusExtensionProperties.PaginationProperties paginationProperties = properties.getPagination(); + if (properties.isEnabled() && paginationProperties.isEnabled()) { + interceptor.addInnerInterceptor(this.paginationInnerInterceptor(paginationProperties)); + } + // 防全表更新与删除插件 + interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); + return interceptor; + } + + /** + * ID 生成器配置(仅在主键类型(idType)配置为 ASSIGN_ID 或 ASSIGN_UUID 时有效) + *

+ * 使用网卡信息绑定雪花生成器,防止集群雪花 ID 重复 + *

+ */ + @Bean + @ConditionalOnMissingBean + public IdentifierGenerator identifierGenerator() { + return new DefaultIdentifierGenerator(NetUtil.getLocalhost()); + } + + /** + * 分页插件配置(PaginationInnerInterceptor) + */ + private PaginationInnerInterceptor paginationInnerInterceptor(MyBatisPlusExtensionProperties.PaginationProperties paginationProperties) { + // 对于单一数据库类型来说,都建议配置该值,避免每次分页都去抓取数据库类型 + PaginationInnerInterceptor paginationInnerInterceptor = null != paginationProperties.getDbType() + ? new PaginationInnerInterceptor(paginationProperties.getDbType()) + : new PaginationInnerInterceptor(); + paginationInnerInterceptor.setOverflow(paginationProperties.isOverflow()); + paginationInnerInterceptor.setMaxLimit(paginationProperties.getMaxLimit()); + return paginationInnerInterceptor; + } + + @PostConstruct + public void postConstruct() { + log.info("[ContiNew Starter] - Auto Configuration 'MyBatis Plus' completed initialization."); + } +} diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..500cbab7 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +top.charles7c.continew.starter.data.mybatis.plus.autoconfigure.MybatisPlusAutoConfiguration \ No newline at end of file diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/resources/spy.properties b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/resources/spy.properties new file mode 100644 index 00000000..ec1e289b --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/resources/spy.properties @@ -0,0 +1,30 @@ +############################################################################ +# P6Spy 配置(SQL 性能分析组件) # +############################################################################ +modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory +# 自定义日志打印 +logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger +#日志输出到控制台 +appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger +# 使用日志系统记录 SQL +#appender=com.p6spy.engine.spy.appender.Slf4JLogger +# 设置 P6Spy Driver 代理 +deregisterdrivers=true +# 取消 JDBC URL 前缀 +useprefix=true +# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset. +excludecategories=info,debug,result,commit,resultset +# 日期格式 +dateformat=yyyy-MM-dd HH:mm:ss +# SQL语句打印时间格式 +databaseDialectTimestampFormat=yyyy-MM-dd HH:mm:ss +# 实际驱动可多个 +#driverlist=org.h2.Driver +# 是否启用慢 SQL 记录 +outagedetection=true +# 慢 SQL 记录标准 2 秒 +outagedetectioninterval=2 +# 是否过滤 Log +filter=true +# 过滤 Log 时所排除的 SQL 关键字,以逗号分隔 +exclude=SELECT 1 diff --git a/continew-starter-data/pom.xml b/continew-starter-data/pom.xml new file mode 100644 index 00000000..0736a413 --- /dev/null +++ b/continew-starter-data/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter + ${revision} + + + continew-starter-data + pom + + ${project.artifactId} + ContiNew Starter 数据访问模块 + + + continew-starter-data-mybatis-plus + + + + + + top.charles7c.continew + continew-starter-core + + + \ No newline at end of file diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 498f65dd..cbb20f3e 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -55,6 +55,9 @@ 1.0.0-SNAPSHOT + 3.5.4.1 + 4.2.0 + 3.9.1 3.24.3 4.3.0 5.8.23 @@ -62,6 +65,27 @@ + + + com.baomidou + mybatis-plus-spring-boot3-starter + ${mybatis-plus.version} + + + + + com.baomidou + dynamic-datasource-spring-boot3-starter + ${dynamic-datasource.version} + + + + + p6spy + p6spy + ${p6spy.version} + + org.redisson @@ -85,7 +109,15 @@ ${hutool.version} - + + + + top.charles7c.continew + continew-starter-data-mybatis-plus + ${revision} + + + top.charles7c.continew continew-starter-cache-redisson @@ -99,7 +131,7 @@ ${revision} - + top.charles7c.continew continew-starter-json-jackson diff --git a/pom.xml b/pom.xml index 887ddcb4..7f9022c8 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,7 @@ continew-starter-json continew-starter-api-doc continew-starter-cache + continew-starter-data