代码生成功能

This commit is contained in:
TinyAnts 2022-06-13 16:44:18 +08:00
parent 9062c0fde3
commit b08bf59e63
16 changed files with 492 additions and 57 deletions

View File

@ -30,8 +30,9 @@ public class GenConstants {
public static final String[] COLUMN_TYPE_TIME = {"datetime", "time", "date", "timestamp"};
/** 数据库数字类型 */
public static final String[] COLUMN_TYPE_NUMBER = {"tinyint", "smallint", "mediumint", "int",
"number", "integer", "bit", "bigint", "float", "double", "decimal"};
public static final String[] COLUMN_TYPE_NUMBER = {
"tinyint", "smallint", "mediumint", "int", "number",
"integer", "bit", "bigint", "float", "double", "decimal"};
/** 页面不需要编辑字段 */
public static final String[] COLUMN_NAME_NOT_EDIT = {"id", "create_time", "update_time", "delete_time"};

View File

@ -2,6 +2,7 @@ package com.hxkj.generator.controller;
import com.hxkj.common.core.AjaxResult;
import com.hxkj.common.core.PageResult;
import com.hxkj.common.validator.annotation.IDMust;
import com.hxkj.generator.service.IGenerateService;
import com.hxkj.generator.validate.PageParam;
import org.springframework.validation.annotation.Validated;
@ -18,7 +19,7 @@ public class GenController {
IGenerateService iGenerateService;
/**
* 数据表列表
* 列表
*
* @author fzr
* @param pageParam 分页参数
@ -33,7 +34,32 @@ public class GenController {
}
/**
* 导入数据表
* 生成列表
*
* @author fzr
* @return Object
*/
@GetMapping("/genList")
public Object genList(@Validated PageParam pageParam,
@RequestParam Map<String, String> params) {
PageResult<Map<String, Object>> list = iGenerateService.genList(pageParam, params);
return AjaxResult.success(list);
}
/**
* 生成详情
*
* @author fzr
* @return Object
*/
@GetMapping("/genDetail")
public Object genDetail(@Validated @IDMust() @RequestParam("id") Integer id) {
Map<String, Object> maps = iGenerateService.genDetail(id);
return AjaxResult.success(maps);
}
/**
* 导入表结构
*
* @param tables 参数
* @return Object
@ -45,14 +71,60 @@ public class GenController {
return AjaxResult.success();
}
/**
* 编辑表结构
*
* @author fzr
* @return Object
*/
@PostMapping("/editTable")
public Object editTable(@Validated @IDMust() @RequestParam("id") Integer id) {
iGenerateService.editTable(id);
return AjaxResult.success();
}
/**
* 删除表结构
*
* @author fzr
* @return Object
*/
@PostMapping("/deleteTable")
public Object deleteTable(@Validated @IDMust() @RequestParam("id") Integer id) {
iGenerateService.deleteTable(id);
return AjaxResult.success();
}
/**
* 同步表结构
*
* @author fzr
* @return Object
*/
public Object syncTable(@Validated @IDMust() @RequestParam("id") Integer id) {
return null;
}
/**
* 预览代码
*
* @author fzr
* @return Object
*/
@GetMapping("/previewCode")
public Object previewCode() {
iGenerateService.previewCode();
public Object previewCode(@Validated @IDMust() @RequestParam("id") Integer id) {
Map<String, String> map = iGenerateService.previewCode(id);
return AjaxResult.success(map);
}
/**
* 生成代码
*
* @author fzr
* @return Object
*/
@GetMapping("genCode")
public Object genCode(@Validated @IDMust() @RequestParam("id") Integer id) {
return null;
}

View File

@ -29,8 +29,8 @@ public class GenTable implements Serializable {
private String genTpl;
private String genType;
private String genPath;
private String remarks;
private Long createTime;
private Long updateTime;
private Long deleteTime;
}

View File

@ -3,6 +3,7 @@ package com.hxkj.generator.service;
import com.hxkj.common.core.PageResult;
import com.hxkj.generator.validate.PageParam;
import java.util.List;
import java.util.Map;
/**
@ -11,7 +12,7 @@ import java.util.Map;
public interface IGenerateService {
/**
* 列表
* 列表
*
* @author fzr
* @param pageParam 分页参数
@ -21,13 +22,64 @@ public interface IGenerateService {
PageResult<Map<String, String>> db(PageParam pageParam, Map<String, String> params);
/**
* 导入表
* 生成列表
*
* @author fzr
* @return Object
*/
PageResult<Map<String, Object>> genList(PageParam pageParam, Map<String, String> params);
/**
* 生成详情
*
* @author fzr
* @return Object
*/
Map<String, Object> genDetail(Integer id);
/**
* 导入表结构
*
* @author fzr
* @param tableNames 参数
*/
void importTable(String[] tableNames);
Object previewCode();
/**
* 编辑表结构
*
* @author fzr
*/
void editTable(Integer id);
/**
* 删除表结构
*
* @author fzr
*/
void deleteTable(Integer id);
/**
* 同步数据表
*
* @author fzr
*/
void syncTable(Integer id);
/**
* 预览代码
*
* @author fzr
* @return Map<String, String>
*/
Map<String, String> previewCode(Integer id);
/**
* 生成代码
*
* @author fzr
* @return Object
*/
Object genCode(Integer id);
}

View File

@ -1,22 +1,23 @@
package com.hxkj.generator.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.hxkj.common.constant.GenConstants;
import com.hxkj.common.core.PageResult;
import com.hxkj.common.utils.StringUtil;
import com.hxkj.common.utils.TimeUtil;
import com.hxkj.generator.entity.GenTable;
import com.hxkj.generator.entity.GenTableColumn;
import com.hxkj.generator.mapper.GenTableColumnMapper;
import com.hxkj.generator.mapper.GenTableMapper;
import com.hxkj.generator.service.IGenerateService;
import com.hxkj.generator.util.GenUtil;
import com.hxkj.generator.util.VelocityUtil;
import com.hxkj.generator.validate.PageParam;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -36,7 +37,7 @@ public class GenerateServiceImpl implements IGenerateService {
GenTableColumnMapper genTableColumnMapper;
/**
* 数据表列表
* 列表
*
* @author fzr
* @param pageParam 分页参数
@ -64,6 +65,96 @@ public class GenerateServiceImpl implements IGenerateService {
return PageResult.pageHelper(tables, list);
}
/**
* 生成列表
*
* @param pageParam 分页参数
* @param params 搜索参数
* @return PageResult<Map<String, Object>>
*/
@Override
public PageResult<Map<String, Object>> genList(PageParam pageParam, Map<String, String> params) {
Integer page = pageParam.getPageNo();
Integer limit = pageParam.getPageSize();
QueryWrapper<GenTable> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("id");
queryWrapper.select("id,entity_name,table_name,table_comment,create_time,update_time");
genTableMapper.setSearch(queryWrapper, params, new String[]{
"like:tableName@table_name:str",
"like:tableComment@table_comment:str",
"datetime:startTime-endTime@create_time:str"
});
PageHelper.startPage(page, limit);
List<Map<String, Object>> tables = genTableMapper.selectMaps(queryWrapper);
List<Map<String, Object>> list = new LinkedList<>();
for (Map<String, Object> item : tables) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", item.get("id"));
map.put("tableName", item.get("table_name"));
map.put("entityName", item.get("entity_name"));
map.put("tableComment", item.get("table_comment"));
map.put("createTime", TimeUtil.timestampToDate(item.get("create_time").toString()));
map.put("updateTime", TimeUtil.timestampToDate(item.get("update_time").toString()));
list.add(map);
}
return PageResult.pageHelper(tables, list);
}
/**
* 生成详情
*
* @author fzr
* @return Object
*/
@Override
public Map<String, Object> genDetail(Integer id) {
Map<String, Object> maps = new LinkedHashMap<>();
GenTable genTable = genTableMapper.selectById(id);
// 基本信息
Map<String, Object> base = new LinkedHashMap<>();
base.put("id", genTable.getId());
base.put("tableName", genTable.getTableName());
base.put("entityName", genTable.getEntityName());
base.put("tableComment", genTable.getTableComment());
base.put("functionAuthor", genTable.getFunctionName());
base.put("createTime", TimeUtil.timestampToDate(genTable.getCreateTime()));
base.put("updateTime", TimeUtil.timestampToDate(genTable.getUpdateTime()));
maps.put("base", base);
// 生成信息
Map<String, Object> gen = new LinkedHashMap<>();
gen.put("genTpl", genTable.getGenTpl());
gen.put("genType", genTable.getGenType());
gen.put("genPath", genTable.getGenPath());
gen.put("moduleName", genTable.getModuleName());
gen.put("packageName", genTable.getPackageName());
gen.put("businessName", genTable.getBusinessName());
gen.put("functionName", genTable.getFunctionName());
maps.put("gen", gen);
// 字段信息
List<GenTableColumn> columns = genTableColumnMapper.selectList(
new QueryWrapper<GenTableColumn>()
.eq("table_id", id)
.orderByDesc("sort"));
maps.put("column", columns);
return maps;
}
/**
* 导入表结构
*
* @author fzr
* @param tableNames 参数
*/
@Override
public void importTable(String[] tableNames) {
List<Map<String, String>> tables = genTableMapper.selectDbTableListByNames(tableNames);
@ -171,30 +262,75 @@ public class GenerateServiceImpl implements IGenerateService {
}
}
/**
* 编辑表结构
*
* @author fzr
* @param id 主键
*/
@Override
public Object previewCode() {
try{
Velocity.init(getDefaultProp());
VelocityContext context = new VelocityContext();
context.put("hello", "Hello World!");
StringWriter w = new StringWriter();
Template t = Velocity.getTemplate("vm/java/controller.java.vm");
t.merge(context, w);
System.out.println("template:" + w);
}catch (Exception e){
e.printStackTrace();
public void editTable(Integer id) {
}
/**
* 删除表结构
*
* @author fzr
* @param id 主键
*/
@Override
public void deleteTable(Integer id) {
}
/**
* 同步数据表
*
* @author fzr
*/
@Override
public void syncTable(Integer id) {
}
/**
* 预览代码
*
* @author fzr
* @return Map<String, String>
*/
@Override
public Map<String, String> previewCode(Integer id) {
GenTable table = genTableMapper.selectById(id);
// 初始模板
VelocityUtil.initVelocity();
VelocityContext context = VelocityUtil.prepareContext(table);
// 渲染模板
Map<String, String> map = new LinkedHashMap<>();
List<String> templates = VelocityUtil.getTemplateList("curd");
for (String template : templates) {
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, "UTF-8");
tpl.merge(context, sw);
map.put(template, sw.toString());
}
return map;
}
/**
* 生成代码
*
* @author fzr
* @return Object
*/
@Override
public Object genCode(Integer id) {
return null;
}
public Properties getDefaultProp(){
Properties prop = new Properties();
// prop.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
// prop.setProperty(RuntimeConstants.RESOURCE_LOADER_CLASS, "classpath");
prop.setProperty(RuntimeConstants.RESOURCE_LOADERS, "classpath");
// prop.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
prop.setProperty("resource.loader.file.class", ClasspathResourceLoader.class.getName());
return prop;
}
}

View File

@ -1,13 +1,16 @@
package com.hxkj.generator.util;
import com.hxkj.common.utils.StringUtil;
import com.hxkj.common.utils.TimeUtil;
import com.hxkj.generator.entity.GenTable;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import java.sql.Time;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
@ -43,10 +46,16 @@ public class VelocityUtil {
String functionName = genTable.getFunctionName();
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("tplCategory", genTable.getGenTpl());
velocityContext.put("genTpl", genTable.getGenTpl());
velocityContext.put("tableName", genTable.getTableName());
velocityContext.put("functionName", StringUtil.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
velocityContext.put("ClassName", genTable.getEntityName());
velocityContext.put("moduleName", genTable.getModuleName());
velocityContext.put("BusinessName", StringUtil.capitalize(genTable.getBusinessName()));
velocityContext.put("businessName", genTable.getBusinessName());
velocityContext.put("packageName", packageName);
velocityContext.put("author", genTable.getFunctionAuthor());
velocityContext.put("datetime", TimeUtil.nowDate());
return velocityContext;
}
@ -55,30 +64,18 @@ public class VelocityUtil {
*
* @return 模板列表
*/
public static List<String> getTemplateList(String tplCategory)
public static List<String> getTemplateList(String genTpl)
{
List<String> templates = new ArrayList<String>();
// templates.add("vm/java/domain.java.vm");
// templates.add("vm/java/mapper.java.vm");
// templates.add("vm/java/service.java.vm");
// templates.add("vm/java/serviceImpl.java.vm");
templates.add("vm/java/controller.java.vm");
List<String> templates = new LinkedList<>();
templates.add("java/controller.java.vm");
// templates.add("java/domain.java.vm");
// templates.add("java/mapper.java.vm");
// templates.add("java/service.java.vm");
// templates.add("java/serviceImpl.java.vm");
// templates.add("vm/xml/mapper.xml.vm");
// templates.add("vm/sql/sql.vm");
// templates.add("vm/js/api.js.vm");
// if (GenConstants.TPL_CRUD.equals(tplCategory))
// {
// templates.add("vm/vue/index.vue.vm");
// }
// else if (GenConstants.TPL_TREE.equals(tplCategory))
// {
// templates.add("vm/vue/index-tree.vue.vm");
// }
// else if (GenConstants.TPL_SUB.equals(tplCategory))
// {
// templates.add("vm/vue/index.vue.vm");
// templates.add("vm/java/sub-domain.java.vm");
// }
return templates;
}

View File

@ -0,0 +1,81 @@
package com.hxkj.generator.validate;
import com.hxkj.common.validator.annotation.IDMust;
import com.hxkj.common.validator.annotation.IntegerContains;
import com.hxkj.common.validator.annotation.StringContains;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 生成参数
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class GenParam implements Serializable {
@IDMust(message = "id参数必传且需大于0")
private Integer id;
@NotNull(message = "tableName参数缺失")
@NotEmpty(message = "表名称不能为空")
@Length(min = 1, max = 200, message = "名称不能大于200个字符")
private String tableName;
@NotNull(message = "entityName参数缺失")
@NotEmpty(message = "实体类名称不能为空")
@Length(min = 1, max = 200, message = "实体类名称不能大于200个字符")
private String entityName;
@NotNull(message = "tableComment参数缺失")
@NotEmpty(message = "表描述不能为空")
@Length(min = 1, max = 200, message = "表描述不能大于200个字符")
private String tableComment;
@NotNull(message = "functionAuthor参数缺失")
@NotEmpty(message = "表描述不能为空")
@Length(min = 1, max = 60, message = "表描述不能大于200个字符")
private String functionAuthor;
@Length(max = 60, message = "备注不能大于200个字符")
private String remarks;
@NotNull(message = "genTpl参数缺失")
@NotEmpty(message = "请选择生成模板")
@StringContains(values = {"curd", "tree"}, message = "选择的生成模板不符合")
private String genTpl;
@NotNull(message = "packageName参数缺失")
@NotEmpty(message = "生成包路径不能为空")
@Length(min = 1, max = 60, message = "生成包路径不能大于200个字符")
private String packageName;
@NotNull(message = "moduleName参数缺失")
@NotEmpty(message = "生成模块名不能为空")
@Length(min = 1, max = 60, message = "生成模块名不能大于60个字符")
private String moduleName;
@NotNull(message = "businessName参数缺失")
@NotEmpty(message = "生成业务名不能为空")
@Length(min = 1, max = 60, message = "生成业务名不能大于60个字符")
private String businessName;
@NotNull(message = "functionName参数缺失")
@NotEmpty(message = "生成功能名不能为空")
@Length(min = 1, max = 60, message = "生成功能名不能大于60个字符")
private String functionName;
@NotNull(message = "genType参数缺失")
@IntegerContains(values = {0, 1}, message = "选择的生成代码方式不符合")
private Integer genType;
@Length(max = 200, message = "生成代码路径不能大于200个字符")
private String genPath;
}

View File

@ -0,0 +1,96 @@
package ${packageName}.controller;
import com.hxkj.generator.LikeAdminThreadLocal;
import com.hxkj.generator.config.aop.Log;
import com.hxkj.generator.service.ISystemAdminService;
import com.hxkj.generator.validate.PageParam;
import com.hxkj.generator.validate.system.SystemAdminParam;
import com.hxkj.generator.vo.system.SystemAdminVo;
import com.hxkj.generator.vo.system.SystemSelfVo;
import com.hxkj.common.core.AjaxResult;
import com.hxkj.common.core.PageResult;
import com.hxkj.common.validator.annotation.IDMust;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;
/**
* ${functionName}管理
*/
@RestController
@RequestMapping("api/${moduleName}/${businessName}")
public class ${ClassName}Controller {
@Resource
ISystemAdminService iSystemAdminService;
/**
* ${functionName}列表
*
* @author fzr
* @return Object
*/
@GetMapping("/list")
public Object list(@Validated PageParam pageParam,
@RequestParam Map<String, String> params) {
PageResult<SystemAdminVo> list = iSystemAdminService.list(pageParam, params);
return AjaxResult.success(list);
}
/**
* ${functionName}详情
*
* @author fzr
* @param id 主键ID
* @return Object
*/
@GetMapping("/detail")
public Object detail(@Validated @IDMust() @RequestParam("id") Integer id) {
SystemAdminVo vo = iSystemAdminService.detail(id);
return AjaxResult.success(vo);
}
/**
* ${functionName}新增
*
* @author fzr
* @param systemAdminParam 参数
* @return Object
*/
@Log(title = "${functionName}新增")
@PostMapping("/add")
public Object add(@Validated(value = SystemAdminParam.create.class) @RequestBody SystemAdminParam systemAdminParam) {
iSystemAdminService.add(systemAdminParam);
return AjaxResult.success();
}
/**
* ${functionName}编辑
*
* @author fzr
* @param systemAdminParam 参数
* @return Object
*/
@Log(title = "${functionName}编辑")
@PostMapping("/edit")
public Object edit(@Validated(value = SystemAdminParam.update.class) @RequestBody SystemAdminParam systemAdminParam) {
iSystemAdminService.edit(systemAdminParam);
return AjaxResult.success();
}
/**
* ${functionName}删除
*
* @author fzr
* @return Object
*/
@Log(title = "${functionName}删除")
@PostMapping("/del")
public Object del(@Validated(value = SystemAdminParam.delete.class) @RequestBody SystemAdminParam systemAdminParam) {
iSystemAdminService.del(systemAdminParam.getId());
return AjaxResult.success();
}
}

View File

@ -0,0 +1,2 @@
$hello
ffff