feat(新增): 后端代码生成器

This commit is contained in:
Bunny 2024-10-02 14:48:56 +08:00
parent daf110417d
commit 2e53338e8f
11 changed files with 200 additions and 54 deletions

View File

@ -0,0 +1,21 @@
package cn.bunny.common.generator.entity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Schema(name = "BaseResultMap对象", title = "数据库基础字段返回映射", description = "数据库基础字段返回映射")
public class BaseResultMap {
@Schema(name = "column", title = "数据库字段")
private String column;
@Schema(name = "property", title = "实体类字段")
private String property;
}

View File

@ -1,12 +1,12 @@
package cn.bunny.common.generator.generator;
import cn.bunny.common.generator.entity.BaseField;
import cn.bunny.common.generator.entity.BaseResultMap;
import cn.bunny.common.generator.utils.GeneratorCodeUtils;
import cn.bunny.dao.dto.menuIcon.MenuIconAddDto;
import cn.bunny.dao.dto.menuIcon.MenuIconDto;
import cn.bunny.dao.dto.menuIcon.MenuIconUpdateDto;
import cn.bunny.dao.entity.system.MenuIcon;
import cn.bunny.dao.vo.menuIcon.MenuIconVo;
import com.baomidou.mybatisplus.annotation.TableName;
import com.google.common.base.CaseFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
@ -25,6 +25,8 @@ import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* * 代码生成器入口点
@ -50,8 +52,8 @@ public class WebGeneratorCode {
Class<MenuIcon> originalClass = MenuIcon.class;
Class<MenuIconDto> dtoClass = MenuIconDto.class;
Class<MenuIconAddDto> addDtoClass = MenuIconAddDto.class;
Class<MenuIconUpdateDto> updateDtoClass = MenuIconUpdateDto.class;
Class<MenuIconVo> voClass = MenuIconVo.class;
// Class<MenuIconUpdateDto> updateDtoClass = MenuIconUpdateDto.class;
// Class<MenuIconVo> voClass = MenuIconVo.class;
// 设置velocity资源加载器
Properties prop = new Properties();
@ -79,12 +81,19 @@ public class WebGeneratorCode {
context.put("leftBrace", "${");
context.put("date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
// 生成前端内容
generatorWebCode(dtoClass, addDtoClass, context);
// 生成后端
generatorServerCode(originalClass, dtoClass, context);
// 写入文件
writeFiles(lowercaseName, lowerHyphen, originalName, context);
}
/**
* * web端生成字段
*/
public static void generatorWebCode(Class<?> dtoClass, Class<?> addDtoClass, VelocityContext context) {
// 生成 Store form 表单内容
List<BaseField> formList = Arrays.stream(dtoClass.getDeclaredFields())
@ -124,6 +133,57 @@ public class WebGeneratorCode {
context.put("baseFieldList", baseFieldList);
}
/**
* 生成后端内容
*/
public static void generatorServerCode(Class<MenuIcon> originalClass, Class<MenuIconDto> dtoClass, VelocityContext context) {
Field[] superFields = originalClass.getSuperclass().getDeclaredFields();
Field[] declaredFields = originalClass.getDeclaredFields();
Field[] mergedArray = new Field[superFields.length + declaredFields.length];
System.arraycopy(superFields, 0, mergedArray, 0, superFields.length);
System.arraycopy(declaredFields, 0, mergedArray, superFields.length, declaredFields.length);
// 添加BaseResultMap
List<BaseResultMap> baseResultMaps = Arrays.stream(mergedArray).map(field -> {
// 转成下划线
String fieldName = field.getName();
String lowerUnderscore = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName);
BaseResultMap baseResultMap = new BaseResultMap();
baseResultMap.setColumn(lowerUnderscore);
baseResultMap.setProperty(fieldName);
return baseResultMap;
}).toList();
// 分页查询内容
List<BaseResultMap> pageQueryMap = Arrays.stream(dtoClass.getDeclaredFields()).map(field -> {
String name = field.getName();
String column = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name);
return BaseResultMap.builder().column(column).property(name).build();
}).toList();
// 类型加包名
String name = originalClass.getName();
// 生层Base_Column_List
String baseColumnList = Stream.of(mergedArray)
.map(field -> CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName()))
.collect(Collectors.joining(", "));
// 表名
String tableName = originalClass.getAnnotation(TableName.class).value();
context.put("baseResultMaps", baseResultMaps);
context.put("type", name);
context.put("baseColumnList", baseColumnList);
context.put("tableName", tableName);
context.put("pageQueryMap", pageQueryMap);
}
/**
* * 写入文件
*/
public static void writeFiles(String lowercaseName, String lowerHyphen, String originalName, VelocityContext context) throws IOException {
context.put("apiPath", GeneratorCodeUtils.ReplacePathHandle(apiPath) + lowercaseName);
context.put("typesPath", GeneratorCodeUtils.ReplacePathHandle(vuePath) + lowercaseName + "/utils/types");
@ -199,7 +259,7 @@ public class WebGeneratorCode {
// 写入resourceMapperPath模板
Template resourceMapperPathTemplate = Velocity.getTemplate("vms/server/resourceMapper.vm", "UTF-8");
FileWriter resourceMapperPathTemplateFileWriter = new FileWriter(mapperPath + originalName + "Mapper.xml");
FileWriter resourceMapperPathTemplateFileWriter = new FileWriter(resourceMapperPath + originalName + "Mapper.xml");
resourceMapperPathTemplate.merge(context, resourceMapperPathTemplateFileWriter);
resourceMapperPathTemplateFileWriter.close();
}

View File

@ -23,7 +23,7 @@ import java.util.List;
*/
@Tag(name = "${classTitle}", description = "${classTitle}相关接口")
@RestController
@RequestMapping("admin/${originalName}")
@RequestMapping("admin/${lowercaseName}")
public class ${originalName}Controller {
@Autowired

View File

@ -2,7 +2,8 @@ package cn.bunny.services.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**

View File

@ -1,21 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bunny.services.mapper.I18nTypeMapper">
<mapper namespace="cn.bunny.services.mapper.${originalName}Mapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="$type">
#foreach($field in $baseResultMaps)
<id column="$field.column" property="$field.property"/>
#end
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
$baseColumnList
</sql>
<!-- 分页查询${classTitle}内容 -->
<select id="selectListByPage" resultType="cn.bunny.dao.entity.i18n.I18n">
select *
from sys_i18n
<select id="selectListByPage" resultType="$type">
select <include refid="Base_Column_List"/>
from $tableName
<where>
<if test="dto.keyName != null and dto.keyName != ''">
key_name like CONCAT('%',#{dto.keyName},'%')
</if>
<if test="dto.translation != null and dto.translation != ''">
translation like CONCAT('%',#{dto.translation},'%')
</if>
<if test="dto.typeName != null and dto.typeName != ''">
type_name like CONCAT('%',#{dto.typeName},'%')
#foreach($field in $pageQueryMap)
<if test="dto.${field.property} != null and dto.${field.property} != ''">
$field.column like CONCAT('%',#{dto.${field.property}},'%')
</if>
#end
</where>
order by update_time
</select>
@ -23,7 +31,7 @@
<!-- 物理删除${classTitle} -->
<delete id="deleteBatchIdsWithPhysics">
delete
from sys_i18n_type
from $tableName
where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}

View File

@ -1,9 +1,7 @@
package cn.bunny.services.service.impl;
import cn.bunny.dao.dto.${originalName}.${originalName}Dto;
import cn.bunny.dao.entity.system.${originalName};
import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.vo.${originalName}.${originalName}Vo;
import cn.bunny.services.mapper.${originalName}Mapper;
import cn.bunny.services.service.${originalName}Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -33,7 +31,7 @@ public class ${originalName}ServiceImpl extends ServiceImpl<${originalName}Mappe
* @return 查询分页${classTitle}返回对象
*/
@Override
PageResult<${originalName}Vo> get${originalName}List(Page<${originalName}> pageParams, ${originalName}Dto dto) {
public PageResult<${originalName}Vo> get${originalName}List(Page<${originalName}> pageParams, ${originalName}Dto dto) {
// 分页查询菜单图标
IPage<${originalName}> page = baseMapper.selectListByPage(pageParams, dto);

View File

@ -26,11 +26,11 @@ import java.util.List;
* </p>
*
* @author Bunny
* @since 2024-10-01T21:55:05.597965300
* @since 2024-10-02 12:18:29
*/
@Tag(name = "系统菜单图标", description = "系统菜单图标相关接口")
@RestController
@RequestMapping("admin/MenuIcon")
@RequestMapping("admin/menuIcon")
public class MenuIconController {
@Autowired
@ -52,7 +52,7 @@ public class MenuIconController {
@Operation(summary = "添加系统菜单图标", description = "添加系统菜单图标")
@PostMapping("addMenuIcon")
public Mono<Result<String>> addMenuIcon(@Valid @RequestBody MenuIconAddDto dto) {
menuIconService.addmenuIcon(dto);
menuIconService.addMenuIcon(dto);
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
}

View File

@ -8,23 +8,32 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* <p>
* 系统菜单图标 Mapper 接口
* </p>
*
* @author Bunny
* @since 2024-09-26
* @since 2024-10-02 12:18:29
*/
@Mapper
public interface MenuIconMapper extends BaseMapper<MenuIcon> {
/**
* 分页查询菜单图标
* * 分页查询系统菜单图标内容
*
* @param pageParams 分页查询结果
* @param dto 系统菜单图标分页查询对象
* @return 分页查询结果返回内容
* @param pageParams 系统菜单图标分页参数
* @param dto 系统菜单图标查询表单
* @return 系统菜单图标分页结果
*/
IPage<MenuIcon> selectListByPage(@Param("page") Page<MenuIcon> pageParams, @Param("dto") MenuIconDto dto);
/**
* 物理删除系统菜单图标
*
* @param ids 删除 id 列表
*/
void deleteBatchIdsWithPhysics(List<Long> ids);
}

View File

@ -18,7 +18,7 @@ import java.util.List;
* </p>
*
* @author Bunny
* @since 2024-10-01T21:55:05.597965300
* @since 2024-10-02 12:18:29
*/
public interface MenuIconService extends IService<MenuIcon> {
@ -44,7 +44,7 @@ public interface MenuIconService extends IService<MenuIcon> {
void updateMenuIcon(@Valid MenuIconUpdateDto dto);
/**
* * 删除系统菜单图标类型
* * 删除|批量删除系统菜单图标类型
*
* @param ids 删除id列表
*/

View File

@ -1,6 +1,8 @@
package cn.bunny.services.service.impl;
import cn.bunny.dao.dto.menuIcon.MenuIconAddDto;
import cn.bunny.dao.dto.menuIcon.MenuIconDto;
import cn.bunny.dao.dto.menuIcon.MenuIconUpdateDto;
import cn.bunny.dao.entity.system.MenuIcon;
import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.vo.menuIcon.MenuIconVo;
@ -9,6 +11,7 @@ import cn.bunny.services.service.MenuIconService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
@ -20,27 +23,27 @@ import java.util.List;
* </p>
*
* @author Bunny
* @since 2024-09-26
* @since 2024-10-02 12:18:29
*/
@Service
public class MenuIconServiceImpl extends ServiceImpl<MenuIconMapper, MenuIcon> implements MenuIconService {
/**
* * 获取菜单Icon
* * 系统菜单图标 服务实现类
*
* @param pageParams 分页查询结果
* @param pageParams 系统菜单图标分页查询page对象
* @param dto 系统菜单图标分页查询对象
* @return 分页查询结果返回内容
* @return 查询分页系统菜单图标返回对象
*/
@Override
public PageResult<MenuIconVo> getMenuIconList(Page<MenuIcon> pageParams, MenuIconDto dto) {
// 分页查询菜单图标
IPage<MenuIcon> page = baseMapper.selectListByPage(pageParams, dto);
List<MenuIconVo> voList = page.getRecords().stream().map(menuIcon -> {
MenuIconVo menuIconVo = new MenuIconVo();
BeanUtils.copyProperties(menuIcon, menuIconVo);
return menuIconVo;
List<MenuIconVo> voList = page.getRecords().stream().map(MenuIcon -> {
MenuIconVo MenuIconVo = new MenuIconVo();
BeanUtils.copyProperties(MenuIcon, MenuIconVo);
return MenuIconVo;
}).toList();
return PageResult.<MenuIconVo>builder()
@ -50,4 +53,40 @@ public class MenuIconServiceImpl extends ServiceImpl<MenuIconMapper, MenuIcon> i
.total(page.getTotal())
.build();
}
/**
* 添加系统菜单图标
*
* @param dto 系统菜单图标添加
*/
@Override
public void addMenuIcon(@Valid MenuIconAddDto dto) {
// 保存数据
MenuIcon menuIcon = new MenuIcon();
BeanUtils.copyProperties(dto, menuIcon);
save(menuIcon);
}
/**
* 更新系统菜单图标
*
* @param dto 系统菜单图标更新
*/
@Override
public void updateMenuIcon(@Valid MenuIconUpdateDto dto) {
// 更新内容
MenuIcon menuIcon = new MenuIcon();
BeanUtils.copyProperties(dto, menuIcon);
updateById(menuIcon);
}
/**
* 删除|批量删除系统菜单图标
*
* @param ids 删除id列表
*/
@Override
public void deleteMenuIcon(List<Long> ids) {
baseMapper.deleteBatchIdsWithPhysics(ids);
}
}

View File

@ -5,29 +5,39 @@
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="cn.bunny.dao.entity.system.MenuIcon">
<id column="id" property="id"/>
<result column="icon_name" property="iconName"/>
<result column="create_user" property="createUser"/>
<result column="update_user" property="updateUser"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="is_deleted" property="isDeleted"/>
<id column="create_time" property="createTime"/>
<id column="update_time" property="updateTime"/>
<id column="create_user" property="createUser"/>
<id column="update_user" property="updateUser"/>
<id column="is_deleted" property="isDeleted"/>
<id column="icon_name" property="iconName"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, icon_name, create_user, update_user, create_time, update_time, is_deleted
id, create_time, update_time, create_user, update_user, is_deleted, icon_name
</sql>
<!-- 分页查询菜单图标 -->
<!-- 分页查询系统菜单图标内容 -->
<select id="selectListByPage" resultType="cn.bunny.dao.entity.system.MenuIcon">
select *
select <include refid="Base_Column_List"/>
from sys_menu_icon
<where>
<if test="dto.iconName != '' and dto.iconName != null">
icon_name like #{dto.iconName}
<if test="dto.iconName != null and dto.iconName != ''">
icon_name like CONCAT('%',#{dto.iconName},'%')
</if>
</where>
order by update_time
</select>
<!-- 物理删除系统菜单图标 -->
<delete id="deleteBatchIdsWithPhysics">
delete
from sys_menu_icon
where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
</mapper>