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 @@
- * 使用网卡信息绑定雪花生成器,防止集群雪花 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 @@