SpringBoot代码生成器,让你释放双手,从此不用手撸代码

新闻 前端
通常在开始开发项目的时候,首先会建立好数据库相关表,然后根据表结构生成 Controller、Service、DAO、Model以及一些前端页面。

 [[327501]]

前言

通常在开始开发项目的时候,首先会建立好数据库相关表,然后根据表结构生成 Controller、Service、DAO、Model以及一些前端页面。

如果开发前没有强制的约束,而每个程序员都有自己的编码习惯,最终会导致一个项目呈现出多种编码风格。再有就是一些CRUD的列表功能,基本是没啥挑战性的,纯粹苦力活,浪费时间。

所以,根据公司现有框架,开发一款统一风格的代码生成器还是很有必要的。

技术选型

开发框架:SpringBoot+JPA,考虑到会生成各种前后端代码文件,这里我们选用freemarker模板引擎来制作相应的模板。

实现思路

获取表结构信息

首先我们定义一个实体类,为了使用方便,把表和字段信息放到了一个类中:

  1. /** 
  2. * 表以及相关字段信息 
  3. */ 
  4. @Data 
  5. public class AppGen extends PageBean implements Serializable { 
  6.  
  7. /** 
  8. * 表名 
  9. */ 
  10. private String tableName; 
  11. /** 
  12. * 实体类名 
  13. */ 
  14. private String entityName; 
  15. /** 
  16. * 实体类名 首字母小写 
  17. */ 
  18. private String lowerEntityName; 
  19. /** 
  20. * 表备注 
  21. */ 
  22. private String tableComment; 
  23. /** 
  24. * 表前缀 
  25. */ 
  26. private String prefix; 
  27. /** 
  28. * 功能描述 
  29. */ 
  30. private String function; 
  31.  
  32. /** 
  33. * 列名 
  34. */ 
  35. private String columnName; 
  36. /** 
  37. * 实体列名 
  38. */ 
  39. private String entityColumnName; 
  40. /** 
  41. * 列描述 
  42. */ 
  43. private String columnComment; 
  44.  
  45. /** 
  46. * 类型 
  47. */ 
  48. private String dataType; 
  49.  
  50. /** 
  51. * 自增 
  52. */ 
  53. private Object columnExtra; 
  54. /** 
  55. * 长度 
  56. */ 
  57. private Object columnLength; 
  58.  
  59. private List<AppGen> list; 
  60.  

获取表列表:

  1. @Override 
  2. @Transactional(readOnly = true
  3. public Result list(AppGen gen){ 
  4. String countSql = "SELECT COUNT(*) FROM information_schema.tables "
  5. countSql +="WHERE table_schema='tools'"
  6. Long totalCount = dynamicQuery.nativeQueryCount(countSql); 
  7. PageBean<AppGen> data = new PageBean<>(); 
  8. if(totalCount>0){ 
  9. String nativeSql = "SELECT table_name as tableName,table_comment as tableComment "
  10. nativeSql+="FROM information_schema.tables WHERE table_schema='tools'"
  11. Pageable pageable = PageRequest.of(gen.getPageNo(),gen.getPageSize()); 
  12. List<AppGen> list = dynamicQuery.nativeQueryPagingListModel(AppGen.class,pageable, nativeSql); 
  13. data = new PageBean<>(list, totalCount); 
  14. return Result.ok(data); 
SpringBoot代码生成器,让你释放双手,从此不用手撸代码

制作模板

模板太多了,这里只以Controller模板为例,贴一下实现代码,更多模板见源码:

  1. package com.tools.module.${prefix}.web; 
  2.  
  3. import com.tools.common.config.AbstractController; 
  4. import com.tools.common.model.Result; 
  5. import com.tools.module.${prefix}.entity.${entityName}; 
  6. import com.tools.module.${prefix}.service.${entityName}Service; 
  7. import org.springframework.beans.factory.annotation.Autowired; 
  8. import org.springframework.web.bind.annotation.PostMapping; 
  9. import org.springframework.web.bind.annotation.RequestBody; 
  10. import org.springframework.web.bind.annotation.RequestMapping; 
  11. import org.springframework.web.bind.annotation.RestController; 
  12.  
  13.  
  14. @RestController 
  15. @RequestMapping("/${prefix}/${function}"
  16. public class ${entityName}Controller extends AbstractController { 
  17.  
  18. @Autowired 
  19. private ${entityName}Service ${function}Service; 
  20.  
  21. /** 
  22. * 列表 
  23. */ 
  24. @PostMapping("/list"
  25. public Result list(${entityName} ${function}){ 
  26. return ${function}Service.list(${function}); 
  27. /** 
  28. * 查询 
  29. */ 
  30. @PostMapping("/get"
  31. public Result get(Long id){ 
  32. return ${function}Service.get(id); 
  33. /** 
  34. * 保存 
  35. */ 
  36. @PostMapping("/save"
  37. public Result save(@RequestBody ${entityName} ${function}){ 
  38. return ${function}Service.save(${function}); 
  39.  
  40. /** 
  41. * 删除 
  42. */ 
  43. @PostMapping("/delete"
  44. public Result delete(Long id){ 
  45. return ${function}Service.delete(id); 

说白了其实就是传递参数,把一些可变的代码片段使用${name}形式编写。

代码生成

有点长,慢慢看,其实就是渲染各种前后端模板:

  1. /** 
  2. * 生成代码 
  3. * @param gen 
  4. * @return 
  5. * @throws IOException 
  6. * @throws TemplateException 
  7. */ 
  8. @PostMapping("/create"
  9. public Result create(@RequestBody AppGen gen) throws IOException, TemplateException { 
  10. /** 
  11. * 获取表字段以及注释 
  12. */ 
  13. List<AppGen> list = genService.getByTable(gen); 
  14. String name = gen.getTableName(); 
  15. String[] table = StringUtils.split(name,"_"); 
  16. gen.setPrefix(table[0]); 
  17. gen.setFunction(table[1]); 
  18. gen.setEntityName(GenUtils.allInitialCapital(gen.getTableName())); 
  19. list.stream().forEach(column-> { 
  20. column.setEntityColumnName(GenUtils.secInitialCapital(column.getColumnName())); 
  21. }); 
  22. gen.setList(list); 
  23. String baseFile = filePath+ SystemConstant.SF_FILE_SEPARATOR+"com"
  24. SystemConstant.SF_FILE_SEPARATOR+ "tools"
  25. SystemConstant.SF_FILE_SEPARATOR+ "module"
  26. SystemConstant.SF_FILE_SEPARATOR+ gen.getPrefix()+SystemConstant.SF_FILE_SEPARATOR; 
  27. /** 
  28. * 后端代码 
  29. */ 
  30. File entityFile = FileUtil.touch(baseFile+"entity"
  31. SystemConstant.SF_FILE_SEPARATOR+gen.getEntityName()+".java"); 
  32. File repositoryFile = FileUtil.touch(baseFile+"repository"
  33. SystemConstant.SF_FILE_SEPARATOR+gen.getEntityName()+"Repository.java"); 
  34. File serviceFile = FileUtil.touch(baseFile+"service"
  35. SystemConstant.SF_FILE_SEPARATOR+gen.getEntityName()+"Service.java"); 
  36. File serviceImplFile = FileUtil.touch(baseFile+"service"
  37. SystemConstant.SF_FILE_SEPARATOR+"impl"+SystemConstant.SF_FILE_SEPARATOR+ 
  38. gen.getEntityName()+"ServiceImpl.java"); 
  39. File controllerFile = FileUtil.touch(baseFile+"web"
  40. SystemConstant.SF_FILE_SEPARATOR + gen.getEntityName() + "Controller.java"); 
  41. /** 
  42. * 前端代码 
  43. */ 
  44. String htmlPath = filePath+ 
  45. SystemConstant.SF_FILE_SEPARATOR + "templates"
  46. SystemConstant.SF_FILE_SEPARATOR + gen.getPrefix()+ 
  47. SystemConstant.SF_FILE_SEPARATOR + gen.getFunction()+SystemConstant.SF_FILE_SEPARATOR; 
  48. File listFile = FileUtil.touch(htmlPath + "list.html"); 
  49. File formFile = FileUtil.touch(htmlPath + "form.html"); 
  50. /** 
  51. * 生成静态页面 
  52. */ 
  53. Template template = configuration.getTemplate("html/list.ftl"); 
  54. String text = FreeMarkerTemplateUtils.processTemplateIntoString( 
  55. template, gen); 
  56. FileUtil.writeString(text,listFile,"UTF-8"); 
  57. template = configuration.getTemplate("html/form.ftl"); 
  58. text = FreeMarkerTemplateUtils.processTemplateIntoString( 
  59. template, gen); 
  60. FileUtil.writeString(text,formFile,"UTF-8"); 
  61. /** 
  62. * 生成后端代码 repository 
  63. */ 
  64. template = configuration.getTemplate("java/repository.ftl"); 
  65. text = FreeMarkerTemplateUtils.processTemplateIntoString( 
  66. template, gen); 
  67. FileUtil.writeString(text,repositoryFile,"UTF-8"); 
  68. /** 
  69. * 生成后端代码 entity 
  70. */ 
  71. template = configuration.getTemplate("java/entity.ftl"); 
  72. text = FreeMarkerTemplateUtils.processTemplateIntoString( 
  73. template, gen); 
  74. FileUtil.writeString(text,entityFile,"UTF-8"); 
  75. /** 
  76. * 生成后端代码 service 
  77. */ 
  78. template = configuration.getTemplate("java/service.ftl"); 
  79. text = FreeMarkerTemplateUtils.processTemplateIntoString( 
  80. template, gen); 
  81. FileUtil.writeString(text,serviceFile,"UTF-8"); 
  82. /** 
  83. * 生成后端代码 service 实现 
  84. */ 
  85. template = configuration.getTemplate("java/serviceImpl.ftl"); 
  86. text = FreeMarkerTemplateUtils.processTemplateIntoString( 
  87. template, gen); 
  88. FileUtil.writeString(text,serviceImplFile,"UTF-8"); 
  89. /** 
  90. * 生成后端代码 controller 实现 
  91. */ 
  92. template = configuration.getTemplate("java/controller.ftl"); 
  93. text = FreeMarkerTemplateUtils.processTemplateIntoString( 
  94. template, gen); 
  95. FileUtil.writeString(text,controllerFile,"UTF-8"); 
  96. return Result.ok(); 

生成逻辑还是很傻瓜的,后期会慢慢优化,比如根据字段类型生成不同的表单形式,可以自定义字段是否显示等的。

小结

总的来说,还是比较容易上手的,相对于一些简单的列表功能分分钟撸出效果,开发一分钟,喝茶一整天。当然对于一些复杂的效果,还是自己一一去实现,但这并不影响生成器的实用性。

源码
https://gitee.com/52itstyle/SPTools

责任编辑:张燕妮 来源: 今日头条
相关推荐

2023-05-17 16:02:00

CSS工具代码生成器

2015-08-25 15:54:17

程序员代码生成器

2021-07-23 11:24:54

Create Inc开源G代码生成器

2021-05-08 17:51:45

IDEA插件代码

2023-01-06 07:52:52

代码生成器开发

2021-03-05 17:07:41

工具在线代码

2012-03-30 09:31:44

WEBCSS

2021-12-10 09:45:19

生成器配置代码

2020-10-20 09:53:11

代码IDEA生成器

2021-10-29 11:25:41

代码编程语言Java

2017-03-20 17:49:21

Java Web模板代码

2011-08-16 10:17:12

XCode模版引擎XTemplate

2022-03-10 10:48:30

PolyCoder自动代码生成器语言

2009-07-03 09:29:24

KeelKit

2015-04-22 09:36:27

JAVA代码生成器

2020-04-30 21:40:14

C#特性编程语言

2022-05-19 14:57:30

CSS代码工具

2012-03-21 09:49:42

Java

2023-08-25 09:51:21

前端开发

2023-08-04 09:00:00

点赞
收藏

51CTO技术栈公众号