/**
• 表以及相关字段信息
*/
@Data
public class AppGen extends PageBean implements Serializable {
/**
• 表名
*/
private String tableName;
/**
• 实体类名
*/
private String entityName;
/**
• 实体类名 首字母小写
*/
private String lowerEntityName;
/**
• 表备注
*/
private String tableComment;
/**
• 表前缀
*/
private String prefix;
/**
• 功能描述
*/
private String function;
/**
• 列名
*/
private String columnName;
/**
• 实体列名
*/
private String entityColumnName;
/**
• 列描述
*/
private String columnComment;
/**
• 类型
*/
private String dataType;
/**
• 自增
*/
private Object columnExtra;
/**
• 长度
*/
private Object columnLength;
private List list;
}
获取表列表:
@Override
@Transactional(readOnly = true)
public Result list(AppGen gen){
String countSql = "SELECT COUNT(*) FROM information_schema.tables ";
countSql +=“WHERE table_schema=‘tools’”;
Long totalCount = dynamicQuery.nativeQueryCount(countSql);
PageBean data = new PageBean<>();
if(totalCount>0){
String nativeSql = "SELECT table_name as tableName,table_comment as tableComment ";
nativeSql+=“FROM information_schema.tables WHERE table_schema=‘tools’”;
Pageable pageable = PageRequest.of(gen.getPageNo(),gen.getPageSize());
List list = dynamicQuery.nativeQueryPagingListModel(AppGen.class,pageable, nativeSql);
data = new PageBean<>(list, totalCount);
}
return Result.ok(data);
}
制作模板
模板太多了,这里只以Controller模板为例,贴一下实现代码,更多模板见源码:
package com.tools.module.${prefix}.web;
import com.tools.common.config.AbstractController;
import com.tools.common.model.Result;
import com.tools.module.{entityName};import com.tools.module.{entityName}Service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(“/{function}”)public class ${entityName}Controller extends AbstractController {
@Autowired
private ${entityName}Service ${function}Service;
/**
• 列表
*/
@PostMapping(“/list”)
public Result list(${entityName} ${function}){
return {function});}
/**
• 查询
*/
@PostMapping(“/get”)
public Result get(Long id){
return ${function}Service.get(id);
}
/**
• 保存
*/
@PostMapping(“/save”)
public Result save(@RequestBody ${entityName} ${function}){
return {function});}
/**
• 删除
*/
@PostMapping(“/delete”)
public Result delete(Long id){
return ${function}Service.delete(id);
}
}
说白了其实就是传递参数,把一些可变的代码片段使用${name}形式编写。
代码生成
有点长,慢慢看,其实就是渲染各种前后端模板:
/**
• 生成代码
• @param gen
• @return
• @throws IOException
• @throws TemplateException
*/
@PostMapping(“/create”)
public Result create(@RequestBody AppGen gen) throws IOException, TemplateException {
/**
• 获取表字段以及注释
*/
List list = genService.getByTable(gen);
String name = gen.getTableName();
String[] table = StringUtils.split(name,“_”);
gen.setPrefix(table[0]);
gen.setFunction(table[1]);
gen.setEntityName(GenUtils.allInitialCapital(gen.getTableName()));
list.stream().forEach(column-> {
column.setEntityColumnName(GenUtils.secInitialCapital(column.getColumnName()));
});
gen.setList(list);
String baseFile = filePath+ SystemConstant.SF_FILE_SEPARATOR+“com”+
SystemConstant.SF_FILE_SEPARATOR+ “tools”+
SystemConstant.SF_FILE_SEPARATOR+ “module”+
SystemConstant.SF_FILE_SEPARATOR+ gen.getPrefix()+SystemConstant.SF_FILE_SEPARATOR;
/**
• 后端代码
*/
File entityFile = FileUtil.touch(baseFile+“entity”+
SystemConstant.SF_FILE_SEPARATOR+gen.getEntityName()+“.java”);
File repositoryFile = FileUtil.touch(baseFile+“repository”+
SystemConstant.SF_FILE_SEPARATOR+gen.getEntityName()+“Repository.java”);
File serviceFile = FileUtil.touch(baseFile+“service”+
SystemConstant.SF_FILE_SEPARATOR+gen.getEntityName()+“Service.java”);
File serviceImplFile = FileUtil.touch(baseFile+“service”+
SystemConstant.SF_FILE_SEPARATOR+“impl”+SystemConstant.SF_FILE_SEPARATOR+