修改代码模板以及 swagger 配置.
This commit is contained in:
@@ -70,10 +70,6 @@ public enum ErrorCode implements CodeInfo {
|
||||
|
||||
DIABLED_ERROR(715, "数据已被禁用"),
|
||||
|
||||
DATA_PRESENT(716, "数据已存在"),
|
||||
|
||||
DATA_ABESENT(717, "数据不存在"),
|
||||
|
||||
;
|
||||
|
||||
ErrorCode(int code, String message) {
|
||||
|
||||
@@ -13,4 +13,10 @@ public interface ErrorMessage {
|
||||
|
||||
String ID_MISSING = "id 不能为空";
|
||||
|
||||
String USERNAME_PASSWORD_ERROR = "用户名或密码错误";
|
||||
|
||||
String DATA_PRESENT = "数据已存在";
|
||||
|
||||
String DATA_ABSENT = "数据不存在";
|
||||
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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<T> extends BaseMapper<T> {
|
||||
return Db.saveOrUpdateBatch(entities, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 获取 wrapper
|
||||
*/
|
||||
default LambdaQueryWrapper<T> wrapper() {
|
||||
return Conditions.wrapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 DataQuery 对象
|
||||
*
|
||||
@@ -98,6 +107,16 @@ public interface IMapper<T> extends BaseMapper<T> {
|
||||
return DataQuery.of(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 DataQuery 对象
|
||||
*
|
||||
* @param wrapper wrapper
|
||||
* @return DataQuery
|
||||
*/
|
||||
default DataQuery<T> of(LambdaQueryWrapper<T> wrapper) {
|
||||
return DataQuery.of(this, wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 CacheQuery 对象
|
||||
*
|
||||
|
||||
@@ -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<T> {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
private DataQuery(BaseMapper<T> dao, LambdaQueryWrapper<T> wrapper) {
|
||||
this.dao = dao;
|
||||
this.wrapper = wrapper;
|
||||
}
|
||||
|
||||
public static <T> DataQuery<T> of(BaseMapper<T> dao) {
|
||||
Valid.notNull(dao, "dao is null");
|
||||
return new DataQuery<>(dao);
|
||||
}
|
||||
|
||||
public static <T> DataQuery<T> of(BaseMapper<T> dao, LambdaQueryWrapper<T> wrapper) {
|
||||
Valid.notNull(dao, "dao is null");
|
||||
return new DataQuery<>(dao, wrapper);
|
||||
}
|
||||
|
||||
public DataQuery<T> page(IPageRequest page) {
|
||||
this.page = Valid.notNull(page, "page is null");
|
||||
return this;
|
||||
@@ -48,6 +59,11 @@ public class DataQuery<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataQuery<T> only() {
|
||||
this.wrapper.last(Const.LIMIT_1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Optional<T> get() {
|
||||
return Optional.ofNullable(dao.selectOne(wrapper));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String, SecurityScheme> securitySchemas = this.buildSecuritySchemes();
|
||||
OpenAPI api = new OpenAPI()
|
||||
// 接口信息
|
||||
@@ -104,27 +106,26 @@ public class OrionSwaggerAutoConfiguration {
|
||||
Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomizers,
|
||||
Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers,
|
||||
Optional<JavadocProvider> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String, GroupedApiConfig> groupedApi;
|
||||
|
||||
@Data
|
||||
public static class GroupedApiConfig {
|
||||
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String group;
|
||||
|
||||
/**
|
||||
* 路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 配置."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user