refactor(data/mybatis-plus): 重构 ID 生成器配置,支持默认、CosId、自定义

This commit is contained in:
2024-02-12 23:19:23 +08:00
parent 9ebcd14878
commit c9311df093
8 changed files with 262 additions and 18 deletions

View File

@@ -31,10 +31,11 @@
<artifactId>p6spy</artifactId>
</dependency>
<!-- CosId通用、灵活、高性能的分布式 ID 生成器) -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<groupId>me.ahoo.cosid</groupId>
<artifactId>cosid-spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -18,6 +18,8 @@ package top.charles7c.continew.starter.data.mybatis.plus.autoconfigure;
import com.baomidou.mybatisplus.annotation.DbType;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import top.charles7c.continew.starter.data.mybatis.plus.autoconfigure.idgenerator.MyBatisPlusIdGeneratorProperties;
/**
* MyBatis Plus 扩展配置属性
@@ -41,6 +43,12 @@ public class MyBatisPlusExtensionProperties {
*/
private String mapperPackage;
/**
* ID 生成器
*/
@NestedConfigurationProperty
private MyBatisPlusIdGeneratorProperties idGenerator;
/**
* 数据权限插件配置
*/
@@ -144,6 +152,14 @@ public class MyBatisPlusExtensionProperties {
this.mapperPackage = mapperPackage;
}
public MyBatisPlusIdGeneratorProperties getIdGenerator() {
return idGenerator;
}
public void setIdGenerator(MyBatisPlusIdGeneratorProperties idGenerator) {
this.idGenerator = idGenerator;
}
public DataPermissionProperties getDataPermission() {
return dataPermission;
}

View File

@@ -16,10 +16,7 @@
package top.charles7c.continew.starter.data.mybatis.plus.autoconfigure;
import cn.hutool.core.net.NetUtil;
import cn.hutool.extra.spring.SpringUtil;
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;
@@ -34,10 +31,13 @@ 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.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import top.charles7c.continew.starter.core.util.GeneralPropertySourceFactory;
import top.charles7c.continew.starter.data.mybatis.plus.autoconfigure.idgenerator.MyBatisPlusIdGeneratorConfiguration;
import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermissionFilter;
import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermissionHandlerImpl;
@@ -81,6 +81,15 @@ public class MybatisPlusAutoConfiguration {
return interceptor;
}
/**
* ID 生成器配置
*/
@Configuration
@Import({MyBatisPlusIdGeneratorConfiguration.Default.class, MyBatisPlusIdGeneratorConfiguration.CosId.class,
MyBatisPlusIdGeneratorConfiguration.Custom.class})
protected static class MyBatisPlusIdGeneratorAutoConfiguration {
}
/**
* 数据权限处理器
*/
@@ -91,18 +100,6 @@ public class MybatisPlusAutoConfiguration {
return new DataPermissionHandlerImpl(dataPermissionFilter);
}
/**
* 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>
*/

View File

@@ -0,0 +1,42 @@
/*
* 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.idgenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import me.ahoo.cosid.snowflake.SnowflakeId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
/**
* MyBatis Plus ID 生成器 - CosId
*
* @author Charles7c
* @since 1.4.0
*/
public class MyBatisPlusCosIdIdentifierGenerator implements IdentifierGenerator {
@Qualifier("__share__SnowflakeId")
@Lazy
@Autowired
private SnowflakeId snowflakeId;
@Override
public Number nextId(Object entity) {
return snowflakeId.generate();
}
}

View File

@@ -0,0 +1,93 @@
/*
* 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.idgenerator;
import cn.hutool.core.net.NetUtil;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import me.ahoo.cosid.IdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ResolvableType;
/**
* MyBatis ID 生成器配置
*
* @author Charles7c
* @since 1.4.0
*/
public class MyBatisPlusIdGeneratorConfiguration {
private static final Logger log = LoggerFactory.getLogger(MyBatisPlusIdGeneratorConfiguration.class);
private MyBatisPlusIdGeneratorConfiguration() {
}
/**
* 自定义 ID 生成器-默认(雪花算法,使用网卡信息绑定雪花生成器,防止集群雪花 ID 重复)
*/
@ConditionalOnMissingBean(IdentifierGenerator.class)
@ConditionalOnProperty(name = "mybatis-plus.extension.id-generator.type", havingValue = "default", matchIfMissing = true)
public static class Default {
static {
log.debug("[ContiNew Starter] - Auto Configuration 'MyBatis Plus-IdGenerator-Default' completed initialization.");
}
@Bean
public IdentifierGenerator identifierGenerator() {
return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
}
}
/**
* 自定义 ID 生成器-CosId
*/
@ConditionalOnMissingBean(IdentifierGenerator.class)
@ConditionalOnClass(IdGenerator.class)
@ConditionalOnProperty(name = "mybatis-plus.extension.id-generator.type", havingValue = "cosid")
public static class CosId {
static {
log.debug("[ContiNew Starter] - Auto Configuration 'MyBatis Plus-IdGenerator-CosId' completed initialization.");
}
@Bean
public IdentifierGenerator identifierGenerator() {
return new MyBatisPlusCosIdIdentifierGenerator();
}
}
/**
* 自定义 ID 生成器
*/
@ConditionalOnProperty(name = "mybatis-plus.extension.id-generator.type", havingValue = "custom")
public static class Custom {
@Bean
@ConditionalOnMissingBean
public IdentifierGenerator identifierGenerator() {
if (log.isErrorEnabled()) {
log.error("Consider defining a bean of type '{}' in your configuration.", ResolvableType
.forClass(IdentifierGenerator.class));
}
throw new NoSuchBeanDefinitionException(IdentifierGenerator.class);
}
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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.idgenerator;
import top.charles7c.continew.starter.data.mybatis.plus.enums.MyBatisPlusIdGeneratorType;
/**
* MyBatis ID 生成器配置属性
*
* @author Charles7c
* @since 1.4.0
*/
public class MyBatisPlusIdGeneratorProperties {
/**
* ID 生成器类型
*/
private MyBatisPlusIdGeneratorType type = MyBatisPlusIdGeneratorType.DEFAULT;
public MyBatisPlusIdGeneratorType getType() {
return type;
}
public void setType(MyBatisPlusIdGeneratorType type) {
this.type = type;
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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.enums;
/**
* MyBatis ID 生成器类型枚举
*
* @author Charles7c
* @since 1.4.0
*/
public enum MyBatisPlusIdGeneratorType {
/**
* 默认
*/
DEFAULT,
/**
* CosId
*/
COSID,
/**
* 自定义
*/
CUSTOM
}