mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 22:57:19 +08:00 
			
		
		
		
	feat: 新增 MyBatis Plus 自动配置(数据访问模块)
This commit is contained in:
		| @@ -0,0 +1,37 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
|     <parent> | ||||
|         <groupId>top.charles7c.continew</groupId> | ||||
|         <artifactId>continew-starter-data</artifactId> | ||||
|         <version>${revision}</version> | ||||
|     </parent> | ||||
|  | ||||
|     <artifactId>continew-starter-data-mybatis-plus</artifactId> | ||||
|     <packaging>jar</packaging> | ||||
|  | ||||
|     <name>${project.artifactId}</name> | ||||
|     <description>ContiNew Starter 数据访问模块 - MyBatis Plus</description> | ||||
|  | ||||
|     <dependencies> | ||||
|         <!-- MyBatis Plus(MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) --> | ||||
|         <dependency> | ||||
|             <groupId>com.baomidou</groupId> | ||||
|             <artifactId>mybatis-plus-spring-boot3-starter</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- Dynamic Datasource(基于 Spring Boot 的快速集成多数据源的启动器) --> | ||||
|         <dependency> | ||||
|             <groupId>com.baomidou</groupId> | ||||
|             <artifactId>dynamic-datasource-spring-boot3-starter</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- P6Spy(SQL 性能分析组件) --> | ||||
|         <dependency> | ||||
|             <groupId>p6spy</groupId> | ||||
|             <artifactId>p6spy</artifactId> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| </project> | ||||
| @@ -0,0 +1,84 @@ | ||||
| /* | ||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||
|  * <p> | ||||
|  * 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 | ||||
|  * <p> | ||||
|  * http://www.gnu.org/licenses/lgpl.html | ||||
|  * <p> | ||||
|  * 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 键名) | ||||
|      * <p> | ||||
|      *  e.g. com.example.**.mapper | ||||
|      * </p> | ||||
|      */ | ||||
|     private String mapperPackage; | ||||
|  | ||||
|     /** | ||||
|      * 数据权限处理器实现类 | ||||
|      */ | ||||
|     private Class<? extends DataPermissionHandler> 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; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,103 @@ | ||||
| /* | ||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||
|  * <p> | ||||
|  * 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 | ||||
|  * <p> | ||||
|  * http://www.gnu.org/licenses/lgpl.html | ||||
|  * <p> | ||||
|  * 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<? extends DataPermissionHandler> 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 时有效) | ||||
|      * <p> | ||||
|      * 使用网卡信息绑定雪花生成器,防止集群雪花 ID 重复 | ||||
|      * </p> | ||||
|      */ | ||||
|     @Bean | ||||
|     @ConditionalOnMissingBean | ||||
|     public IdentifierGenerator identifierGenerator() { | ||||
|         return new DefaultIdentifierGenerator(NetUtil.getLocalhost()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 分页插件配置(<a href="https://baomidou.com/pages/97710a/#paginationinnerinterceptor">PaginationInnerInterceptor</a>) | ||||
|      */ | ||||
|     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."); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1 @@ | ||||
| top.charles7c.continew.starter.data.mybatis.plus.autoconfigure.MybatisPlusAutoConfiguration | ||||
| @@ -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 | ||||
							
								
								
									
										29
									
								
								continew-starter-data/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								continew-starter-data/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
|     <parent> | ||||
|         <groupId>top.charles7c.continew</groupId> | ||||
|         <artifactId>continew-starter</artifactId> | ||||
|         <version>${revision}</version> | ||||
|     </parent> | ||||
|  | ||||
|     <artifactId>continew-starter-data</artifactId> | ||||
|     <packaging>pom</packaging> | ||||
|  | ||||
|     <name>${project.artifactId}</name> | ||||
|     <description>ContiNew Starter 数据访问模块</description> | ||||
|  | ||||
|     <modules> | ||||
|         <module>continew-starter-data-mybatis-plus</module> | ||||
|     </modules> | ||||
|  | ||||
|     <dependencies> | ||||
|         <!-- 核心模块 --> | ||||
|         <dependency> | ||||
|             <groupId>top.charles7c.continew</groupId> | ||||
|             <artifactId>continew-starter-core</artifactId> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| </project> | ||||
| @@ -55,6 +55,9 @@ | ||||
|  | ||||
|     <properties> | ||||
|         <revision>1.0.0-SNAPSHOT</revision> | ||||
|         <mybatis-plus.version>3.5.4.1</mybatis-plus.version> | ||||
|         <dynamic-datasource.version>4.2.0</dynamic-datasource.version> | ||||
|         <p6spy.version>3.9.1</p6spy.version> | ||||
|         <redisson.version>3.24.3</redisson.version> | ||||
|         <knife4j.version>4.3.0</knife4j.version> | ||||
|         <hutool.version>5.8.23</hutool.version> | ||||
| @@ -62,6 +65,27 @@ | ||||
|  | ||||
|     <dependencyManagement> | ||||
|         <dependencies> | ||||
|             <!-- MyBatis Plus(MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) --> | ||||
|             <dependency> | ||||
|                 <groupId>com.baomidou</groupId> | ||||
|                 <artifactId>mybatis-plus-spring-boot3-starter</artifactId> | ||||
|                 <version>${mybatis-plus.version}</version> | ||||
|             </dependency> | ||||
|  | ||||
|             <!-- Dynamic Datasource(基于 Spring Boot 的快速集成多数据源的启动器) --> | ||||
|             <dependency> | ||||
|                 <groupId>com.baomidou</groupId> | ||||
|                 <artifactId>dynamic-datasource-spring-boot3-starter</artifactId> | ||||
|                 <version>${dynamic-datasource.version}</version> | ||||
|             </dependency> | ||||
|  | ||||
|             <!-- P6Spy(SQL 性能分析组件) --> | ||||
|             <dependency> | ||||
|                 <groupId>p6spy</groupId> | ||||
|                 <artifactId>p6spy</artifactId> | ||||
|                 <version>${p6spy.version}</version> | ||||
|             </dependency> | ||||
|  | ||||
|             <!-- Redisson(不仅仅是一个 Redis Java 客户端) --> | ||||
|             <dependency> | ||||
|                 <groupId>org.redisson</groupId> | ||||
| @@ -85,7 +109,15 @@ | ||||
|                 <version>${hutool.version}</version> | ||||
|             </dependency> | ||||
|  | ||||
|             <!-- 缓存 - Redisson 模块 --> | ||||
|             <!-- ContiNew Starter 依赖 --> | ||||
|             <!-- 数据访问模块 - MyBatis Plus --> | ||||
|             <dependency> | ||||
|                 <groupId>top.charles7c.continew</groupId> | ||||
|                 <artifactId>continew-starter-data-mybatis-plus</artifactId> | ||||
|                 <version>${revision}</version> | ||||
|             </dependency> | ||||
|  | ||||
|             <!-- 缓存模块 - Redisson --> | ||||
|             <dependency> | ||||
|                 <groupId>top.charles7c.continew</groupId> | ||||
|                 <artifactId>continew-starter-cache-redisson</artifactId> | ||||
| @@ -99,7 +131,7 @@ | ||||
|                 <version>${revision}</version> | ||||
|             </dependency> | ||||
|  | ||||
|             <!-- Jackson 模块 --> | ||||
|             <!-- JSON 模块 - Jackson --> | ||||
|             <dependency> | ||||
|                 <groupId>top.charles7c.continew</groupId> | ||||
|                 <artifactId>continew-starter-json-jackson</artifactId> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user