diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml b/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml index 9133afc6..df5dcd4f 100644 --- a/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml +++ b/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml @@ -31,10 +31,11 @@ p6spy + - org.slf4j - slf4j-api + me.ahoo.cosid + cosid-spring-boot-starter + true - \ 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 index a646e2ec..390aaef8 100644 --- 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 @@ -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; } 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 index 58aa1145..648c2564 100644 --- 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 @@ -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 时有效) - *

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

- */ - @Bean - @ConditionalOnMissingBean - public IdentifierGenerator identifierGenerator() { - return new DefaultIdentifierGenerator(NetUtil.getLocalhost()); - } - /** * 分页插件配置(PaginationInnerInterceptor) */ diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/idgenerator/MyBatisPlusCosIdIdentifierGenerator.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/idgenerator/MyBatisPlusCosIdIdentifierGenerator.java new file mode 100644 index 00000000..f3ae614f --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/idgenerator/MyBatisPlusCosIdIdentifierGenerator.java @@ -0,0 +1,42 @@ +/* + * 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.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(); + } +} diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/idgenerator/MyBatisPlusIdGeneratorConfiguration.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/idgenerator/MyBatisPlusIdGeneratorConfiguration.java new file mode 100644 index 00000000..c2ce9f62 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/idgenerator/MyBatisPlusIdGeneratorConfiguration.java @@ -0,0 +1,93 @@ +/* + * 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.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); + } + } +} \ 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/idgenerator/MyBatisPlusIdGeneratorProperties.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/idgenerator/MyBatisPlusIdGeneratorProperties.java new file mode 100644 index 00000000..194138b6 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/autoconfigure/idgenerator/MyBatisPlusIdGeneratorProperties.java @@ -0,0 +1,41 @@ +/* + * 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.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; + } +} diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/enums/MyBatisPlusIdGeneratorType.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/enums/MyBatisPlusIdGeneratorType.java new file mode 100644 index 00000000..b740d9a1 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/enums/MyBatisPlusIdGeneratorType.java @@ -0,0 +1,41 @@ +/* + * 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.enums; + +/** + * MyBatis ID 生成器类型枚举 + * + * @author Charles7c + * @since 1.4.0 + */ +public enum MyBatisPlusIdGeneratorType { + + /** + * 默认 + */ + DEFAULT, + + /** + * CosId + */ + COSID, + + /** + * 自定义 + */ + CUSTOM +} diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 760352fb..8f01df21 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -61,6 +61,7 @@ 3.9.1 2.7.5 3.26.0 + 2.6.5 3.1.1 1.3.0 1.6.2 @@ -174,6 +175,18 @@ ${redisson.version} + + + me.ahoo.cosid + cosid-spring-boot-starter + ${cosid.version} + + + me.ahoo.cosid + cosid-spring-redis + ${cosid.version} + + org.dromara.sms4j