diff --git a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ErrorCode.java b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ErrorCode.java index be9fbe3b..55fc9753 100644 --- a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ErrorCode.java +++ b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ErrorCode.java @@ -70,10 +70,6 @@ public enum ErrorCode implements CodeInfo { DIABLED_ERROR(715, "数据已被禁用"), - DATA_PRESENT(716, "数据已存在"), - - DATA_ABESENT(717, "数据不存在"), - ; ErrorCode(int code, String message) { diff --git a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ErrorMessage.java b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ErrorMessage.java index ef3f3999..a92f406f 100644 --- a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ErrorMessage.java +++ b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ErrorMessage.java @@ -13,4 +13,10 @@ public interface ErrorMessage { String ID_MISSING = "id 不能为空"; + String USERNAME_PASSWORD_ERROR = "用户名或密码错误"; + + String DATA_PRESENT = "数据已存在"; + + String DATA_ABSENT = "数据不存在"; + } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/generator/CodeGenerator.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/generator/CodeGenerator.java index 3c83c87d..0f144795 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/generator/CodeGenerator.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/generator/CodeGenerator.java @@ -40,10 +40,10 @@ public class CodeGenerator { @NotNull String author = Const.ORION_AUTHOR; @NotEmpty - String[] tables = {"test_table", "table_copy"}; + String[] tables = {"system_user"}; // 表业务注释 需要和表一一对应 null则为表注释 @NotEmpty - String[] comment = {"用户", "复制"}; + String[] comment = {"用户"}; // 模块 @NotNull String module = "infra"; diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/mapper/IMapper.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/mapper/IMapper.java index 84d301f7..996eb119 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/mapper/IMapper.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/mapper/IMapper.java @@ -1,8 +1,10 @@ package com.orion.ops.framework.mybatis.core.mapper; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.toolkit.Db; import com.orion.ops.framework.mybatis.core.query.CacheQuery; +import com.orion.ops.framework.mybatis.core.query.Conditions; import com.orion.ops.framework.mybatis.core.query.DataQuery; import java.util.Collection; @@ -89,6 +91,13 @@ public interface IMapper extends BaseMapper { return Db.saveOrUpdateBatch(entities, size); } + /** + * @return 获取 wrapper + */ + default LambdaQueryWrapper wrapper() { + return Conditions.wrapper(); + } + /** * 获取 DataQuery 对象 * @@ -98,6 +107,16 @@ public interface IMapper extends BaseMapper { return DataQuery.of(this); } + /** + * 获取 DataQuery 对象 + * + * @param wrapper wrapper + * @return DataQuery + */ + default DataQuery of(LambdaQueryWrapper wrapper) { + return DataQuery.of(this, wrapper); + } + /** * 获取 CacheQuery 对象 * diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/query/DataQuery.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/query/DataQuery.java index b24c9e5d..a3513b3a 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/query/DataQuery.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/core/query/DataQuery.java @@ -7,6 +7,7 @@ import com.orion.lang.define.wrapper.IPageRequest; import com.orion.lang.define.wrapper.Pager; import com.orion.lang.utils.Valid; import com.orion.lang.utils.collect.Lists; +import com.orion.ops.framework.common.constant.Const; import java.util.List; import java.util.Optional; @@ -33,11 +34,21 @@ public class DataQuery { this.dao = dao; } + private DataQuery(BaseMapper dao, LambdaQueryWrapper wrapper) { + this.dao = dao; + this.wrapper = wrapper; + } + public static DataQuery of(BaseMapper dao) { Valid.notNull(dao, "dao is null"); return new DataQuery<>(dao); } + public static DataQuery of(BaseMapper dao, LambdaQueryWrapper wrapper) { + Valid.notNull(dao, "dao is null"); + return new DataQuery<>(dao, wrapper); + } + public DataQuery page(IPageRequest page) { this.page = Valid.notNull(page, "page is null"); return this; @@ -48,6 +59,11 @@ public class DataQuery { return this; } + public DataQuery only() { + this.wrapper.last(Const.LIMIT_1); + return this; + } + public Optional get() { return Optional.ofNullable(dao.selectOne(wrapper)); } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-entity-request-create.java.vm b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-entity-request-create.java.vm index 13b5412c..b1acdefb 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-entity-request-create.java.vm +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-entity-request-create.java.vm @@ -9,6 +9,9 @@ import lombok.NoArgsConstructor; import java.util.*; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + /** * $!{table.comment} 创建请求对象 * @@ -25,6 +28,11 @@ public class ${type}CreateRequest implements Serializable { #foreach($field in ${table.fields}) #if("$!field.propertyName" != "id") + #if("$field.propertyType" == "String") + @NotBlank + #else + @NotNull + #end #if("$!field.comment" != "") @Schema(description = "${field.comment}") #end diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-entity-request-update.java.vm b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-entity-request-update.java.vm index daa79485..b7ee1a45 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-entity-request-update.java.vm +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-entity-request-update.java.vm @@ -9,6 +9,9 @@ import lombok.NoArgsConstructor; import java.util.*; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + /** * $!{table.comment} 更新请求对象 * @@ -24,6 +27,11 @@ import java.util.*; public class ${type}UpdateRequest implements Serializable { #foreach($field in ${table.fields}) + #if("$field.propertyType" == "String") + @NotBlank + #else + @NotNull + #end #if("$!field.comment" != "") @Schema(description = "${field.comment}") #end diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-service-impl.java.vm b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-service-impl.java.vm index 28e131bd..cfa1fbf2 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-service-impl.java.vm +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/templates/orion-service-impl.java.vm @@ -5,9 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.orion.lang.define.wrapper.DataGrid; import com.orion.lang.utils.Valid; import com.orion.lang.utils.collect.Lists; -import com.orion.ops.framework.common.constant.ErrorCode; import com.orion.ops.framework.common.constant.ErrorMessage; -import com.orion.ops.framework.mybatis.core.query.Conditions; #foreach($pkg in ${customFilePackages}) import ${pkg}.*; #end @@ -43,7 +41,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} { this.check${type}Present(record); // 插入 int effect = ${typeLower}DAO.insert(record); - log.info("${type}Service-add${type} effect: {}, domain: {}", effect, JSON.toJSONString(record)); + log.info("${type}Service-create${type} effect: {}, domain: {}", effect, JSON.toJSONString(record)); return record.getId(); } @@ -64,9 +62,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} { public ${type}VO get${type}(Long id) { // 查询 ${type}DO record = ${typeLower}DAO.selectById(id); - if (record == null) { - throw ErrorCode.DATA_ABESENT.exception(); - } + Valid.notNull(record, ErrorMessage.DATA_ABSENT); // 转换 return ${type}Convert.MAPPER.to(record); } @@ -85,7 +81,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} { @Override public DataGrid<${type}VO> get${type}Page(${type}QueryRequest request) { // 构造条件 - LambdaQueryWrapper<${type}DO> wrapper = Conditions.wrapper(${type}DO.class) + LambdaQueryWrapper<${type}DO> wrapper = ${typeLower}DAO.wrapper() #foreach($field in ${table.fields}) .eq(${type}DO::get${field.capitalName}, request.get${field.capitalName}())#if(!$foreach.hasNext);#end #end @@ -117,7 +113,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} { */ private void check${type}Present(${type}DO domain) { // 构造条件 - LambdaQueryWrapper<${type}DO> wrapper = Conditions.wrapper(${type}DO.class) + LambdaQueryWrapper<${type}DO> wrapper = ${typeLower}DAO.wrapper() // 更新时忽略当前记录 .ne(${type}DO::getId, domain.getId()) // 用其他字段做重复校验 @@ -128,9 +124,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} { #end // 检查是否存在 boolean present = ${typeLower}DAO.of().wrapper(wrapper).present(); - if (present) { - throw ErrorCode.DATA_PRESENT.exception(); - } + Valid.isFalse(present, ErrorMessage.DATA_PRESENT); } } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/java/com/orion/ops/framework/swagger/config/OrionSwaggerAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/java/com/orion/ops/framework/swagger/config/OrionSwaggerAutoConfiguration.java index bc3d7609..877a44e9 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/java/com/orion/ops/framework/swagger/config/OrionSwaggerAutoConfiguration.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/java/com/orion/ops/framework/swagger/config/OrionSwaggerAutoConfiguration.java @@ -15,6 +15,7 @@ import org.springdoc.core.customizers.OpenApiBuilderCustomizer; import org.springdoc.core.customizers.ServerBaseUrlCustomizer; import org.springdoc.core.providers.JavadocProvider; import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -44,14 +45,15 @@ import java.util.Optional; @AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_SWAGGER) public class OrionSwaggerAutoConfiguration { - private static String orionApiPrefix; + @Value("${orion.api.prefix}") + private String orionApiPrefix; /** * @param properties 配置 - * @return API + * @return OpenAPI */ @Bean - public OpenAPI createApi(SwaggerProperties properties) { + public OpenAPI openApi(SwaggerProperties properties) { Map securitySchemas = this.buildSecuritySchemes(); OpenAPI api = new OpenAPI() // 接口信息 @@ -104,27 +106,26 @@ public class OrionSwaggerAutoConfiguration { Optional> openApiBuilderCustomizers, Optional> serverBaseUrlCustomizers, Optional javadocProvider) { - return new OpenAPIService(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider); } /** + * @param properties properties + * @param beanFactory beanFactory * @return 所有模块的 api 分组 */ @Bean - public GroupedOpenApi allGroupedOpenApi() { - return buildGroupedOpenApi("全部", "*"); - } - - /** - * 构建 api 分组 - * - * @param group group - * @return group - */ - public static GroupedOpenApi buildGroupedOpenApi(String group) { - return buildGroupedOpenApi(group, group); + public GroupedOpenApi allGroupedOpenApi(ConfigurableListableBeanFactory beanFactory, + SwaggerProperties properties) { + // 全部 + GroupedOpenApi all = buildGroupedOpenApi("全部", "*"); + // 注册模块分组 api + properties.getGroupedApi().forEach((t, v) -> { + GroupedOpenApi api = buildGroupedOpenApi(v.getGroup(), v.getPath()); + beanFactory.registerSingleton(t + "GroupedOpenApi", api); + }); + return all; } /** @@ -134,7 +135,7 @@ public class OrionSwaggerAutoConfiguration { * @param path path * @return group */ - public static GroupedOpenApi buildGroupedOpenApi(String group, String path) { + private GroupedOpenApi buildGroupedOpenApi(String group, String path) { return GroupedOpenApi.builder() .group(group) .pathsToMatch(orionApiPrefix + "/" + path + "/**") @@ -146,7 +147,7 @@ public class OrionSwaggerAutoConfiguration { /** * @return Authorization 认证请求头参数 */ - private static Parameter buildSecurityHeaderParameter() { + private Parameter buildSecurityHeaderParameter() { return new Parameter() .name(HttpHeaders.AUTHORIZATION) .description("认证 Token") @@ -157,9 +158,4 @@ public class OrionSwaggerAutoConfiguration { .description("认证 Token")); } - @Value("${orion.api.prefix}") - public void setOrionApiPrefix(String orionApiPrefix) { - OrionSwaggerAutoConfiguration.orionApiPrefix = orionApiPrefix; - } - } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/java/com/orion/ops/framework/swagger/config/SwaggerProperties.java b/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/java/com/orion/ops/framework/swagger/config/SwaggerProperties.java index 40f4bf74..def6a8d0 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/java/com/orion/ops/framework/swagger/config/SwaggerProperties.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/java/com/orion/ops/framework/swagger/config/SwaggerProperties.java @@ -3,6 +3,8 @@ package com.orion.ops.framework.swagger.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +import java.util.Map; + /** * @author Jiahang Li * @version 1.0.0 @@ -52,4 +54,24 @@ public class SwaggerProperties { */ private String licenseUrl; + /** + * api 分组 + */ + private Map groupedApi; + + @Data + public static class GroupedApiConfig { + + /** + * 名称 + */ + private String group; + + /** + * 路径 + */ + private String path; + + } + } diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/resources/META-INF/spring-configuration-metadata.json b/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/resources/META-INF/spring-configuration-metadata.json index d08ae3df..2ef1d928 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/orion-ops-framework/orion-ops-spring-boot-starter-swagger/src/main/resources/META-INF/spring-configuration-metadata.json @@ -41,6 +41,11 @@ "name": "orion.swagger.license-url", "type": "java.lang.String", "description": "swagger 项目license-url." + }, + { + "name": "orion.swagger.grouped-api", + "type": "java.util.Map", + "description": "grouped api 配置." } ] } \ No newline at end of file diff --git a/orion-ops-launch/src/main/resources/application.yaml b/orion-ops-launch/src/main/resources/application.yaml index b8aae4b3..44b365f9 100644 --- a/orion-ops-launch/src/main/resources/application.yaml +++ b/orion-ops-launch/src/main/resources/application.yaml @@ -142,6 +142,11 @@ orion: email: ljh1553488six@139.com license: Apache-2.0 license-url: https://github.com/lijiahangmax/orion-ops-pro/blob/main/LICENSE + grouped-api: + infra: + group: "infra - 基建模块" + path: "infra" + logging: # 全局日志打印 printer: