日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot代碼生成器,讓你釋放雙手,從此不用手?jǐn)]代碼

前言

通常在開始開發(fā)項(xiàng)目的時(shí)候,首先會(huì)建立好數(shù)據(jù)庫相關(guān)表,然后根據(jù)表結(jié)構(gòu)生成 Controller、Service、DAO、Model以及一些前端頁面。

站在用戶的角度思考問題,與客戶深入溝通,找到南江網(wǎng)站設(shè)計(jì)與南江網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋南江地區(qū)。

如果開發(fā)前沒有強(qiáng)制的約束,而每個(gè)程序員都有自己的編碼習(xí)慣,最終會(huì)導(dǎo)致一個(gè)項(xiàng)目呈現(xiàn)出多種編碼風(fēng)格。再有就是一些CRUD的列表功能,基本是沒啥挑戰(zhàn)性的,純粹苦力活,浪費(fèi)時(shí)間。

所以,根據(jù)公司現(xiàn)有框架,開發(fā)一款統(tǒng)一風(fēng)格的代碼生成器還是很有必要的。

技術(shù)選型

開發(fā)框架:SpringBoot+JPA,考慮到會(huì)生成各種前后端代碼文件,這里我們選用freemarker模板引擎來制作相應(yīng)的模板。

實(shí)現(xiàn)思路

獲取表結(jié)構(gòu)信息

首先我們定義一個(gè)實(shí)體類,為了使用方便,把表和字段信息放到了一個(gè)類中:

 
 
 
 
  1. /**
  2. * 表以及相關(guān)字段信息
  3. */
  4. @Data
  5. public class AppGen extends PageBean implements Serializable {
  6. /**
  7. * 表名
  8. */
  9. private String tableName;
  10. /**
  11. * 實(shí)體類名
  12. */
  13. private String entityName;
  14. /**
  15. * 實(shí)體類名 首字母小寫
  16. */
  17. private String lowerEntityName;
  18. /**
  19. * 表備注
  20. */
  21. private String tableComment;
  22. /**
  23. * 表前綴
  24. */
  25. private String prefix;
  26. /**
  27. * 功能描述
  28. */
  29. private String function;
  30. /**
  31. * 列名
  32. */
  33. private String columnName;
  34. /**
  35. * 實(shí)體列名
  36. */
  37. private String entityColumnName;
  38. /**
  39. * 列描述
  40. */
  41. private String columnComment;
  42. /**
  43. * 類型
  44. */
  45. private String dataType;
  46. /**
  47. * 自增
  48. */
  49. private Object columnExtra;
  50. /**
  51. * 長度
  52. */
  53. private Object columnLength;
  54. private List list;
  55. }

獲取表列表:

 
 
 
 
  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 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 list = dynamicQuery.nativeQueryPagingListModel(AppGen.class,pageable, nativeSql);
  13. data = new PageBean<>(list, totalCount);
  14. }
  15. return Result.ok(data);
  16. }

制作模板

模板太多了,這里只以Controller模板為例,貼一下實(shí)現(xiàn)代碼,更多模板見源碼:

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

說白了其實(shí)就是傳遞參數(shù),把一些可變的代碼片段使用${name}形式編寫。

代碼生成

有點(diǎn)長,慢慢看,其實(shí)就是渲染各種前后端模板:

 
 
 
 
  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 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. * 生成靜態(tài)頁面
  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 實(shí)現(xiàn)
  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 實(shí)現(xiàn)
  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();
  97. }

生成邏輯還是很傻瓜的,后期會(huì)慢慢優(yōu)化,比如根據(jù)字段類型生成不同的表單形式,可以自定義字段是否顯示等的。

小結(jié)

總的來說,還是比較容易上手的,相對(duì)于一些簡單的列表功能分分鐘擼出效果,開發(fā)一分鐘,喝茶一整天。當(dāng)然對(duì)于一些復(fù)雜的效果,還是自己一一去實(shí)現(xiàn),但這并不影響生成器的實(shí)用性。

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


網(wǎng)頁題目:SpringBoot代碼生成器,讓你釋放雙手,從此不用手?jǐn)]代碼
本文網(wǎng)址:http://www.5511xx.com/article/dpjsphi.html