添加 api 代码生成模板.

This commit is contained in:
lijiahang
2023-08-22 18:47:39 +08:00
parent 2b7ebd4e26
commit 21f7cbc546
16 changed files with 539 additions and 145 deletions

View File

@@ -12,8 +12,6 @@ import com.orion.lang.constant.Const;
import com.orion.lang.utils.ext.yml.YmlExt;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
import com.orion.ops.framework.mybatis.core.mapper.IMapper;
import com.orion.ops.module.infra.enums.RoleStatusEnum;
import com.orion.ops.module.infra.enums.UserStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import java.io.File;
@@ -40,13 +38,10 @@ public class CodeGenerator {
String module = "infra";
// 生成的表
GenTable[] tables = {
new GenTable("system_user", "用户", "user")
.vue("user", "user")
.enums(UserStatusEnum.class),
new GenTable("system_role", "角色", "role")
.vue("user", "role")
.enums(RoleStatusEnum.class),
// new GenTable("system_menu", "菜单", "menu"),
// new GenTable("system_user", "用户", "user")
// .vue("user", "user")
// .enums(UserStatusEnum.class),
new GenTable("favorite", "收藏", "favorite", true),
};
// jdbc 配置 - 使用配置文件
File yamlFile = new File("orion-ops-launch/src/main/resources/application-dev.yaml");
@@ -127,8 +122,7 @@ public class CodeGenerator {
* @return config
*/
private static GlobalConfig getGlobalConfig(String outputDir, String author) {
// 全局配置
GlobalConfig gbConfig = new GlobalConfig.Builder()
return new GlobalConfig.Builder()
// 设置作者
.author(author)
// 生成路径
@@ -141,7 +135,6 @@ public class CodeGenerator {
.commentDate("yyyy-M-d HH:mm")
// 构建
.build();
return gbConfig;
}
/**
@@ -153,7 +146,7 @@ public class CodeGenerator {
* @return 数据源配置
*/
private static DataSourceConfig getDataSourceConfig(String url, String username, String password) {
DataSourceConfig dsConfig = new DataSourceConfig.Builder(url, username, password)
return new DataSourceConfig.Builder(url, username, password)
// 转换器
.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
switch (metaInfo.getJdbcType().TYPE_CODE) {
@@ -168,7 +161,6 @@ public class CodeGenerator {
.dbQuery(new MySqlQuery())
// 构建
.build();
return dsConfig;
}
/**
@@ -181,7 +173,8 @@ public class CodeGenerator {
String[] tableNames = Arrays.stream(tables)
.map(GenTable::getTableName)
.toArray(String[]::new);
StrategyConfig stConfig = new StrategyConfig.Builder()
// 策略配置
return new StrategyConfig.Builder()
// 生成的表
.addInclude(tableNames)
// 全局大写命名
@@ -240,7 +233,6 @@ public class CodeGenerator {
.formatServiceImplFileName("%sServiceImpl")
// 构建
.build();
return stConfig;
}
/**
@@ -250,7 +242,7 @@ public class CodeGenerator {
* @return 包名配置
*/
private static PackageConfig getPackageConfig(String module) {
PackageConfig pkConfig = new PackageConfig.Builder()
return new PackageConfig.Builder()
// 声明父包
.parent("com.orion.ops.module")
// 模块名称
@@ -269,7 +261,6 @@ public class CodeGenerator {
.controller("controller")
// 构建
.build();
return pkConfig;
}
/**
@@ -278,7 +269,7 @@ public class CodeGenerator {
* @return 模板配置
*/
private static TemplateConfig getTemplateConfig() {
TemplateConfig tplConfig = new TemplateConfig.Builder()
return new TemplateConfig.Builder()
.controller("/templates/orion-server-controller.java.vm")
.entity("/templates/orion-server-entity-do.java.vm")
.service("/templates/orion-server-service.java.vm")
@@ -286,7 +277,6 @@ public class CodeGenerator {
.mapper("/templates/orion-server-mapper.java.vm")
.xml("/templates/orion-server-mapper.xml.vm")
.build();
return tplConfig;
}
/**
@@ -296,12 +286,11 @@ public class CodeGenerator {
*/
private static InjectionConfig getInjectionConfig() {
String[][] customFileDefineArr = new String[][]{
// -------------------- 后端 - service --------------------
// http 文件
new String[]{"/templates/orion-server-controller.http.vm", "%sController.http", "controller"},
// vo 文件
new String[]{"/templates/orion-server-entity-vo.java.vm", "%sVO.java", "entity.vo"},
// dto 文件
new String[]{"/templates/orion-server-entity-dto.java.vm", "%sDTO.java", "entity.dto"},
// create request 文件
new String[]{"/templates/orion-server-entity-request-create.java.vm", "%sCreateRequest.java", "entity.request.%s"},
// update request 文件
@@ -310,8 +299,22 @@ public class CodeGenerator {
new String[]{"/templates/orion-server-entity-request-query.java.vm", "%sQueryRequest.java", "entity.request.%s"},
// convert 文件
new String[]{"/templates/orion-server-convert.java.vm", "%sConvert.java", "convert"},
// -------------------- 后端 - api --------------------
// api 文件
new String[]{"/templates/orion-server-api-interface.java.vm", "%sApi.java", "api"},
// api impl 文件
new String[]{"/templates/orion-server-api-impl.java.vm", "%sApiImpl.java", "api.impl"},
// dto 文件
new String[]{"/templates/orion-server-api-entity-dto.java.vm", "%sDTO.java", "entity.dto.%s"},
// create dto 文件
new String[]{"/templates/orion-server-api-entity-dto-create.java.vm", "%sCreateDTO.java", "entity.dto.%s"},
// update dto 文件
new String[]{"/templates/orion-server-api-entity-dto-update.java.vm", "%sUpdateDTO.java", "entity.dto.%s"},
// query dto 文件
new String[]{"/templates/orion-server-api-entity-dto-query.java.vm", "%sQueryDTO.java", "entity.dto.%s"},
// convert provider 文件
new String[]{"/templates/orion-server-convert-provider.java.vm", "%sProviderConvert.java", "convert"},
new String[]{"/templates/orion-server-api-convert.java.vm", "%sApiConvert.java", "convert"},
// -------------------- 前端 --------------------
// vue api 文件
new String[]{"/templates/orion-vue-api.ts.vm", "${feature}.ts", "vue/api/${module}"},
// vue router 文件

View File

@@ -42,6 +42,7 @@ import java.io.OutputStreamWriter;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
@@ -53,7 +54,7 @@ import java.util.stream.Collectors;
*/
public class VelocityTemplateEngine extends AbstractTemplateEngine {
private static final String REQUEST_PACKAGE_REPLACER = "request.%s";
private static final String[] PACKAGE_REPLACER = new String[]{"request.%s", "dto.%s"};
private final Map<String, GenTable> tables;
@@ -82,7 +83,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
p.setProperty(Velocity.ENCODING_DEFAULT, ConstVal.UTF8);
p.setProperty(Velocity.INPUT_ENCODING, ConstVal.UTF8);
p.setProperty("file.resource.loader.unicode", StringPool.TRUE);
velocityEngine = new VelocityEngine(p);
this.velocityEngine = new VelocityEngine(p);
}
return this;
}
@@ -95,7 +96,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
BufferedWriter writer = new BufferedWriter(ow)) {
template.merge(new VelocityContext(objectMap), writer);
}
LOGGER.debug("模板:" + templatePath + "; 文件:" + outputFile);
LOGGER.debug("模板: " + templatePath + "; 文件: " + outputFile);
}
@Override
@@ -109,20 +110,32 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
* 创建自定义文件副本对象
*
* @param originCustomerFile originCustomerFile
* @param tableInfo tableInfo
* @return backup
*/
private List<CustomFile> createCustomFilesBackup(@NotNull List<CustomFile> originCustomerFile) {
private List<CustomFile> createCustomFilesBackup(@NotNull List<CustomFile> originCustomerFile,
@NotNull TableInfo tableInfo) {
// 生成文件副本
List<CustomFile> customFiles = originCustomerFile.stream().map(s -> {
return new CustomFile.Builder()
.enableFileOverride()
.templatePath(s.getTemplatePath())
.filePath(s.getFilePath())
.fileName(s.getFileName())
.packageName(s.getPackageName())
.build();
}).collect(Collectors.toList());
return customFiles;
List<CustomFile> files = originCustomerFile.stream().map(s ->
new CustomFile.Builder()
.enableFileOverride()
.templatePath(s.getTemplatePath())
.filePath(s.getFilePath())
.fileName(s.getFileName())
.packageName(s.getPackageName())
.build())
.collect(Collectors.toList());
// 获取 table
GenTable table = tables.get(tableInfo.getName());
// 不生成 api 文件
if (!table.isGenApi()) {
files.removeIf(file -> this.isServerApiFile(file.getTemplatePath()));
}
// 不生成 vue 文件
if (!table.isGenVue()) {
files.removeIf(file -> this.isVueFile(file.getTemplatePath()));
}
return files;
}
/**
@@ -138,22 +151,37 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
// 反射调用 setter 方法
BiConsumer<String, Object> callSetter = (field, value) -> Fields.setFieldValue(s, field, value);
String packageName = s.getPackageName();
// 替换 Request.java 文件包名
if (packageName.contains(REQUEST_PACKAGE_REPLACER)) {
String replacePackage = String.format(packageName, tables.get(tableInfo.getName()).getRequestPackage());
// 替换文件业务包名
boolean replace = Arrays.stream(PACKAGE_REPLACER).anyMatch(packageName::contains);
if (replace) {
String replacePackage = String.format(packageName, tables.get(tableInfo.getName()).getBizPackage());
callSetter.accept("packageName", replacePackage);
}
});
// 包转换器
Function<Predicate<String>, List<String>> packageConverter = conv ->
customFiles.stream()
.filter(s -> conv.test(s.getTemplatePath()))
.map(CustomFile::getPackageName)
.map(s -> getConfigBuilder().getPackageConfig().getParent() + "." + s)
.distinct()
.collect(Collectors.toList());
// 自定义文件的包 (导入用)
List<String> customFilePackages = customFiles.stream()
.filter(s -> s.getTemplatePath().contains(".java.vm"))
.map(CustomFile::getPackageName)
.map(s -> getConfigBuilder().getPackageConfig().getParent() + "." + s)
.distinct()
.collect(Collectors.toList());
List<String> customFilePackages = packageConverter.apply(s -> s.contains(".java.vm") && !s.contains("orion-server-api"));
objectMap.put("customFilePackages", customFilePackages);
// 自定义 api entity 文件的包 (导入用)
List<String> customApiEntityFilePackages = packageConverter.apply(s -> s.contains(".java.vm") && s.contains("orion-server-api-entity"));
objectMap.put("customApiEntityFilePackages", customApiEntityFilePackages);
// 自定义 api interface 文件的包 (导入用)
List<String> customApiInterfaceFilePackages = packageConverter.apply(s -> s.contains("orion-server-api-interface.java.vm"));
objectMap.put("customApiInterfaceFilePackage", customApiInterfaceFilePackages.get(0));
}
/**
* 插入表元数据
*
@@ -187,18 +215,19 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
objectMap.put("apiComment", map);
String comment = tableInfo.getComment();
map.put("create", "创建" + comment);
map.put("update", "通过 id 更新" + comment);
map.put("get", "通过 id 查询" + comment);
map.put("list", "通过 id 批量查询" + comment);
map.put("query", "分页查询" + comment);
map.put("delete", "通过 id 删除" + comment);
map.put("batchDelete", "通过 id 批量删除" + comment);
map.put("updateById", "通过 id 更新" + comment);
map.put("getById", "通过 id 查询" + comment);
map.put("listById", "通过 id 批量查询" + comment);
map.put("listAll", "查询全部" + comment);
map.put("queryPage", "分页查询" + comment);
map.put("deleteById", "通过 id 删除" + comment);
map.put("batchDeleteById", "通过 id 批量删除" + comment);
}
@Override
protected void outputCustomFile(@NotNull List<CustomFile> customFiles, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
// 创建自定义文件副本文件
customFiles = this.createCustomFilesBackup(customFiles);
customFiles = this.createCustomFilesBackup(customFiles, tableInfo);
// 替换自定义包名
this.replacePackageName(customFiles, tableInfo, objectMap);
// 添加表元数据
@@ -265,7 +294,6 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
beanMap.put("enums", this.getEnumMap(table));
objectMap.put("vue", beanMap);
// 生成文件
customFiles.forEach(file -> {
// 文件路径
@@ -284,7 +312,17 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
* @return 是否为后端文件
*/
private boolean isServerFile(String templatePath) {
return templatePath.startsWith("orion-server");
return templatePath.contains("orion-server");
}
/**
* 是否为后端 api 文件
*
* @param templatePath templatePath
* @return 是否为 sql 文件
*/
private boolean isServerApiFile(String templatePath) {
return templatePath.contains("orion-server-api");
}
/**
@@ -298,16 +336,6 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
templatePath.contains("orion-sql-menu.sql");
}
/**
* 是否为 sql 文件
*
* @param templatePath templatePath
* @return 是否为 sql 文件
*/
private boolean isSqlFile(String templatePath) {
return templatePath.contains("orion-sql-");
}
/**
* 获取枚举 map
*

View File

@@ -0,0 +1,39 @@
package ${currentPackage};
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import ${package.Entity}.*;
#foreach($pkg in ${customFilePackages})
import ${pkg}.*;
#end
#foreach($pkg in ${customApiEntityFilePackages})
import ${pkg}.*;
#end
import java.util.List;
/**
* $!{table.comment} 对外服务对象转换器
*
* @author ${author}
* @version ${since}
* @since ${date}
*/
@Mapper
public interface ${type}ApiConvert {
${type}ApiConvert MAPPER = Mappers.getMapper(${type}ApiConvert.class);
${type}DO to(${type}DTO dto);
${type}DTO to(${type}DO domain);
${type}DO to(${type}QueryDTO domain);
${type}CreateRequest to(${type}CreateDTO domain);
${type}UpdateRequest to(${type}UpdateDTO domain);
List<${type}DTO> toDTO(List<${type}DO> list);
List<${type}DO> toDomain(List<${type}DTO> list);
}

View File

@@ -0,0 +1,44 @@
package ${currentPackage};
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.*;
/**
* $!{table.comment} 创建请求业务对象
*
* @author ${author}
* @version ${since}
* @since ${date}
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "${type}CreateDTO", description = "$!{table.comment} 创建请求业务对象")
public class ${type}CreateDTO implements Serializable {
#foreach($field in ${table.fields})
#if("$!field.propertyName" != "id")
#if("$field.propertyType" == "String")
@NotBlank
@Size(max = $field.metaInfo.length)
#else
@NotNull
#end
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#end
private ${field.propertyType} ${field.propertyName};
#end
#end
}

View File

@@ -0,0 +1,37 @@
package ${currentPackage};
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.*;
/**
* $!{table.comment} 查询请求业务对象
*
* @author ${author}
* @version ${since}
* @since ${date}
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "${type}QueryDTO", description = "$!{table.comment} 查询请求业务对象")
public class ${type}QueryDTO implements Serializable {
#foreach($field in ${table.fields})
#if("$field.propertyType" == "String")
@Size(max = $field.metaInfo.length)
#end
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#end
private ${field.propertyType} ${field.propertyName};
#end
}

View File

@@ -0,0 +1,42 @@
package ${currentPackage};
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.*;
/**
* $!{table.comment} 更新请求业务对象
*
* @author ${author}
* @version ${since}
* @since ${date}
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "${type}UpdateDTO", description = "$!{table.comment} 更新请求业务对象")
public class ${type}UpdateDTO implements Serializable {
#foreach($field in ${table.fields})
#if("$field.propertyType" == "String")
@NotBlank
@Size(max = $field.metaInfo.length)
#else
@NotNull
#end
#if("$!field.comment" != "")
@Schema(description = "${field.comment}")
#end
private ${field.propertyType} ${field.propertyName};
#end
}

View File

@@ -0,0 +1,112 @@
package ${package.ServiceImpl};
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.utils.collect.Collections;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid;
#foreach($pkg in ${customFilePackages})
import ${pkg}.*;
#end
import ${customApiInterfaceFilePackage}.${type}Api;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* $!{table.comment} 对外服务实现类
*
* @author ${author}
* @version ${since}
* @since ${date}
*/
@Slf4j
@Service
public class ${type}ApiImpl implements ${type}Api {
@Resource
private ${type}Service ${type}Service;
@Resource
private ${type}DAO ${typeLower}DAO;
@Override
public Long create${type}(${type}CreateDTO dto) {
log.info("${type}Api.create${type} request: {}", JSON.toJSONString(dto));
Valid.valid(dto);
// 转换
${type}CreateRequest request = ${type}ApiConvert.MAPPER.to(dto);
// 创建
return ${type}Service.create${type}(request);
}
@Override
public Integer update${type}ById(${type}UpdateDTO dto) {
log.info("${type}Api.update${type} request: {}", JSON.toJSONString(dto));
Valid.valid(dto);
// 转换
${type}UpdateRequest request = ${type}ApiConvert.MAPPER.to(dto);
// 修改
return ${type}Service.update${type}(request);
}
@Override
public ${type}DTO get${type}ById(Long id) {
log.info("${type}Api.get${type}ById id: {}", id);
Valid.notNull(id, ErrorMessage.ID_MISSING);
// 修改
${type}DO record = ${type}DAO.selectById(id);
if (record == null) {
return null;
}
// 转换
return ${type}ApiConvert.MAPPER.to(record);
}
@Override
public List<${type}DTO> get${type}ListById(Collection<Long> idList) {
log.info("${type}Api.get${type}ListById idList: {}", idList);
if (Collections.isEmpty(idList)) {
return new ArrayList<>();
}
// 查询
List<${type}DO> rows = ${type}DAO.selectBatchIds(idList);
// 转换
return ${type}ApiConvert.MAPPER.toDTO(rows);
}
@Override
public List<${type}DTO> get${type}List(${type}QueryDTO dto) {
log.info("${type}Api.get${type}List dto: {}", JSON.toJSONString(dto));
Valid.valid(dto);
// 转换条件
${type}DO domain = ${type}ApiConvert.MAPPER.to(dto);
// 查询
LambdaQueryWrapper<${type}DO> wrapper = ${type}DAO.queryCondition(domain);
List<${type}DO> rows = ${type}DAO.selectList(wrapper);
// 转换结果
return ${type}ApiConvert.MAPPER.toDTO(rows);
}
@Override
public Integer delete${type}ById(Long id) {
log.info("${type}Api.delete${type}ById id: {}", id);
Valid.notNull(id, ErrorMessage.ID_MISSING);
// 删除
return ${type}DAO.deleteById(id);
}
@Override
public Integer batchDelete${type}ById(Collection<Long> idList) {
log.info("${type}Api.batchDelete${type}ById idList: {}", idList);
Valid.notEmpty(idList, ErrorMessage.ID_MISSING);
// 删除
return ${type}DAO.deleteBatchIds(idList);
}
}

View File

@@ -0,0 +1,75 @@
package ${currentPackage};
#foreach($pkg in ${customApiEntityFilePackages})
import ${pkg}.*;
#end
import java.util.Collection;
import java.util.List;
/**
* $!{table.comment} 对外服务类
*
* @author ${author}
* @version ${since}
* @since ${date}
*/
public interface ${type}Api {
/**
* ${apiComment.create}
*
* @param dto dto
* @return id
*/
Long create${type}(${type}CreateDTO dto);
/**
* ${apiComment.updateById}
*
* @param dto dto
* @return effect
*/
Integer update${type}ById(${type}UpdateDTO dto);
/**
* ${apiComment.getById}
*
* @param id id
* @return row
*/
${type}DTO get${type}ById(Long id);
/**
* ${apiComment.listById}
*
* @param idList idList
* @return rows
*/
List<${type}DTO> get${type}ByIdList(Collection<Long> idList);
/**
* ${apiComment.listAll}
*
* @param dto dto
* @return rows
*/
List<${type}DTO> get${type}List(${type}QueryDTO dto);
/**
* ${apiComment.deleteById}
*
* @param id id
* @return effect
*/
Integer delete${type}ById(Long id);
/**
* ${apiComment.batchDeleteById}
*
* @param idList idList
* @return effect
*/
Integer batchDelete${type}ByIdList(Collection<Long> idList);
}

View File

@@ -12,7 +12,7 @@ Authorization: {{token}}
}
${httpComment} ${apiComment.update}
${httpComment} ${apiComment.updateById}
PUT {{baseUrl}}/${package.ModuleName}/${typeHyphen}/update
Content-Type: application/json
Authorization: {{token}}
@@ -24,17 +24,29 @@ Authorization: {{token}}
}
${httpComment} ${apiComment.get}
${httpComment} ${apiComment.getById}
GET {{baseUrl}}/${package.ModuleName}/${typeHyphen}/get?id=1
Authorization: {{token}}
${httpComment} ${apiComment.list}
${httpComment} ${apiComment.listById}
GET {{baseUrl}}/${package.ModuleName}/${typeHyphen}/list?idList=1,2,3
Authorization: {{token}}
${httpComment} ${apiComment.query}
${httpComment} ${apiComment.listAll}
POST {{baseUrl}}/${package.ModuleName}/${typeHyphen}/list-all
Content-Type: application/json
Authorization: {{token}}
{
#foreach($field in ${table.fields})
"${field.propertyName}": ""#if($foreach.hasNext),#end
#end
}
${httpComment} ${apiComment.queryPage}
POST {{baseUrl}}/${package.ModuleName}/${typeHyphen}/query
Content-Type: application/json
Authorization: {{token}}
@@ -46,12 +58,12 @@ Authorization: {{token}}
}
${httpComment} ${apiComment.delete}
${httpComment} ${apiComment.deleteById}
DELETE {{baseUrl}}/${package.ModuleName}/${typeHyphen}/delete?id=1
Authorization: {{token}}
${httpComment} ${apiComment.batchDelete}
${httpComment} ${apiComment.batchDeleteById}
DELETE {{baseUrl}}/${package.ModuleName}/${typeHyphen}/delete-batch?idList=1,2,3
Authorization: {{token}}

View File

@@ -53,52 +53,60 @@ public class ${table.controllerName} {
}
@PutMapping("/update")
@Operation(summary = "${apiComment.update}")
@Operation(summary = "${apiComment.updateById}")
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:update')")
public Integer update${type}(@Validated @RequestBody ${type}UpdateRequest request) {
return ${typeLower}Service.update${type}(request);
return ${typeLower}Service.update${type}ById(request);
}
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/get")
@Operation(summary = "${apiComment.get}")
@Operation(summary = "${apiComment.getById}")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:query')")
public ${type}VO get${type}(@RequestParam("id") Long id) {
return ${typeLower}Service.get${type}(id);
return ${typeLower}Service.get${type}ById(id);
}
@IgnoreLog(IgnoreLogMode.RET)
@GetMapping("/list")
@Operation(summary = "${apiComment.list}")
@Operation(summary = "${apiComment.listById}")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:query')")
public List<${type}VO> get${type}List(@RequestParam("idList") List<Long> idList) {
return ${typeLower}Service.get${type}List(idList);
return ${typeLower}Service.get${type}ByIdList(idList);
}
@IgnoreLog(IgnoreLogMode.RET)
@PostMapping("/list-all")
@Operation(summary = "${apiComment.listAll}")
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:query')")
public List<${type}VO> get${type}ListAll(@Validated @RequestBody ${type}QueryRequest request) {
return ${typeLower}Service.get${type}List(request);
}
@IgnoreLog(IgnoreLogMode.RET)
@PostMapping("/query")
@Operation(summary = "${apiComment.query}")
@Operation(summary = "${apiComment.queryPage}")
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:query')")
public DataGrid<${type}VO> get${type}Page(@Validated @RequestBody ${type}QueryRequest request) {
return ${typeLower}Service.get${type}Page(request);
}
@DeleteMapping("/delete")
@Operation(summary = "${apiComment.delete}")
@Operation(summary = "${apiComment.deleteById}")
@Parameter(name = "id", description = "id", required = true)
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:delete')")
public Integer delete${type}(@RequestParam("id") Long id) {
return ${typeLower}Service.delete${type}(id);
return ${typeLower}Service.delete${type}ById(id);
}
@DeleteMapping("/delete-batch")
@Operation(summary = "${apiComment.batchDelete}")
@Operation(summary = "${apiComment.batchDeleteById}")
@Parameter(name = "idList", description = "idList", required = true)
@PreAuthorize("@ss.hasPermission('${package.ModuleName}:${typeHyphen}:delete')")
public Integer batchDelete${type}(@RequestParam("idList") List<Long> idList) {
return ${typeLower}Service.batchDelete${type}(idList);
return ${typeLower}Service.batchDelete${type}ByIdList(idList);
}
}

View File

@@ -1,31 +0,0 @@
package ${currentPackage};
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import ${package.Entity}.*;
#foreach($pkg in ${customFilePackages})
import ${pkg}.*;
#end
import java.util.List;
/**
* $!{table.comment} 暴露服务转换器
*
* @author ${author}
* @version ${since}
* @since ${date}
*/
@Mapper
public interface ${type}ProviderConvert {
${type}ProviderConvert MAPPER = Mappers.getMapper(${type}ProviderConvert.class);
${type}DO to(${type}DTO dto);
${type}DTO to(${type}DO domain);
List<${type}DO> toDO(List<${type}DTO> list);
List<${type}DTO> toDTO(List<${type}DO> list);
}

View File

@@ -20,12 +20,12 @@ import ${mapperAnnotationClass.name};
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {
/**
* 获取全部条件
* 获取查询条件
*
* @param entity entity
* @return 全部条件
* @return 查询条件
*/
default LambdaQueryWrapper<${entity}> condition(${entity} entity) {
default LambdaQueryWrapper<${entity}> queryCondition(${entity} entity) {
return this.wrapper()
#foreach($field in ${table.fields})
.eq(${type}DO::get${field.capitalName}, entity.get${field.capitalName}())#if(!$foreach.hasNext);#end

View File

@@ -46,7 +46,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
}
@Override
public Integer update${type}(${type}UpdateRequest request) {
public Integer update${type}ById(${type}UpdateRequest request) {
// 查询
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
${type}DO record = ${typeLower}DAO.selectById(id);
@@ -62,7 +62,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
}
@Override
public ${type}VO get${type}(Long id) {
public ${type}VO get${type}ById(Long id) {
// 查询
${type}DO record = ${typeLower}DAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
@@ -71,7 +71,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
}
@Override
public List<${type}VO> get${type}List(List<Long> idList) {
public List<${type}VO> get${type}ByIdList(List<Long> idList) {
// 查询
List<${type}DO> records = ${typeLower}DAO.selectBatchIds(idList);
if (records.isEmpty()) {
@@ -82,13 +82,23 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
}
@Override
public DataGrid<${type}VO> get${type}Page(${type}QueryRequest request) {
public List<${type}VO> get${type}List(${type}QueryRequest request) {
// 转换
${type}DO record = ${type}Convert.MAPPER.to(request);
// 构造条件
LambdaQueryWrapper<${type}DO> wrapper = ${typeLower}DAO.wrapper()
#foreach($field in ${table.fields})
.eq(${type}DO::get${field.capitalName}, request.get${field.capitalName}())
#end
.orderByDesc(${type}DO::getId);
LambdaQueryWrapper<${type}DO> wrapper = ${typeLower}DAO.queryCondition(record);
// 查询
return ${typeLower}DAO.of()
.wrapper(wrapper)
.list(${type}Convert.MAPPER::to);
}
@Override
public DataGrid<${type}VO> get${type}Page(${type}QueryRequest request) {
// 转换
${type}DO record = ${type}Convert.MAPPER.to(request);
// 构造条件
LambdaQueryWrapper<${type}DO> wrapper = ${typeLower}DAO.queryCondition(record);
// 查询
return ${typeLower}DAO.of()
.wrapper(wrapper)
@@ -97,16 +107,16 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
}
@Override
public Integer delete${type}(Long id) {
public Integer delete${type}ById(Long id) {
int effect = ${typeLower}DAO.deleteById(id);
log.info("${type}Service-delete${type} id: {}, effect: {}", id, effect);
return effect;
}
@Override
public Integer batchDelete${type}(List<Long> idList) {
public Integer batchDelete${type}ByIdList(List<Long> idList) {
int effect = ${typeLower}DAO.deleteBatchIds(idList);
log.info("${type}Service-batchDelete${type} idList: {}, effect: {}", JSON.toJSONString(idList), effect);
log.info("${type}Service-batchDelete${type} idList: {}, effect: {}", idList, effect);
return effect;
}

View File

@@ -25,31 +25,39 @@ public interface ${table.serviceName} {
Long create${type}(${type}CreateRequest request);
/**
* ${apiComment.update}
* ${apiComment.updateById}
*
* @param request request
* @return effect
*/
Integer update${type}(${type}UpdateRequest request);
Integer update${type}ById(${type}UpdateRequest request);
/**
* ${apiComment.get}
* ${apiComment.getById}
*
* @param id id
* @return row
*/
${type}VO get${type}(Long id);
${type}VO get${type}ById(Long id);
/**
* ${apiComment.list}
* ${apiComment.listById}
*
* @param idList idList
* @return rows
*/
List<${type}VO> get${type}List(List<Long> idList);
List<${type}VO> get${type}ByIdList(List<Long> idList);
/**
* ${apiComment.query}
* ${apiComment.listAll}
*
* @param request request
* @return rows
*/
List<${type}VO> get${type}List(${type}QueryRequest request);
/**
* ${apiComment.queryPage}
*
* @param request request
* @return rows
@@ -57,19 +65,19 @@ public interface ${table.serviceName} {
DataGrid<${type}VO> get${type}Page(${type}QueryRequest request);
/**
* ${apiComment.delete}
* ${apiComment.deleteById}
*
* @param id id
* @return effect
*/
Integer delete${type}(Long id);
Integer delete${type}ById(Long id);
/**
* ${apiComment.batchDelete}
* ${apiComment.batchDeleteById}
*
* @param idList idList
* @return effect
*/
Integer batchDelete${type}(List<Long> idList);
Integer batchDelete${type}ByIdList(List<Long> idList);
}

View File

@@ -74,21 +74,21 @@ export function create${vue.featureFirstUpper}(request: ${vue.featureFirstUpper}
}
/**
* $apiComment.update
* $apiComment.updateById
*/
export function update${vue.featureFirstUpper}(request: ${vue.featureFirstUpper}UpdateRequest) {
return axios.put('/${package.ModuleName}/${typeHyphen}/update', request);
}
/**
* $apiComment.get
* $apiComment.getById
*/
export function get${vue.featureFirstUpper}(id: number) {
return axios.get<${vue.featureFirstUpper}QueryResponse>('/${package.ModuleName}/${typeHyphen}/get', { params: { id } });
}
/**
* $apiComment.list
* $apiComment.listById
*/
export function get${vue.featureFirstUpper}List(idList: Array<number>) {
return axios.get<${vue.featureFirstUpper}QueryResponse[]>('/${package.ModuleName}/${typeHyphen}/list', {
@@ -100,21 +100,28 @@ export function get${vue.featureFirstUpper}List(idList: Array<number>) {
}
/**
* $apiComment.query
* $apiComment.listAll
*/
export function get${vue.featureFirstUpper}ListAll(request: ${vue.featureFirstUpper}QueryRequest) {
return axios.post<Array<${vue.featureFirstUpper}QueryResponse>>('/${package.ModuleName}/${typeHyphen}/list-all', request);
}
/**
* $apiComment.queryPage
*/
export function get${vue.featureFirstUpper}Page(request: ${vue.featureFirstUpper}QueryRequest) {
return axios.post<DataGrid<${vue.featureFirstUpper}QueryResponse>>('/${package.ModuleName}/${typeHyphen}/query', request);
}
/**
* $apiComment.delete
* $apiComment.deleteById
*/
export function delete${vue.featureFirstUpper}(id: number) {
return axios.delete('/${package.ModuleName}/${typeHyphen}/delete', { params: { id } });
}
/**
* $apiComment.batchDelete
* $apiComment.batchDeleteById
*/
export function batchDelete${vue.featureFirstUpper}(idList: Array<number>) {
return axios.delete('/${package.ModuleName}/${typeHyphen}/delete-batch', {