GeneratorApp.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package cn.itlym.shoulder.generator;
  2. import cn.itlym.shoulder.generator.service.SysGeneratorService;
  3. import org.apache.commons.io.IOUtils;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.shoulder.core.dto.response.ListResult;
  6. import org.shoulder.core.dto.response.BaseResult;
  7. import org.shoulder.core.util.StringUtils;
  8. import org.springframework.boot.SpringApplication;
  9. import org.springframework.boot.autoconfigure.SpringBootApplication;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.IOException;
  17. import java.util.Map;
  18. /**
  19. * 代码生成器,根据数据库表,生成 Entity、RestApi、Controller、Service、ServiceImpl、Repository、Mapper、Mapper.xml、前端视图
  20. *
  21. * @author lym
  22. */
  23. @MapperScan(value = "cn.itlym.shoulder.generator.mapper")
  24. @Configuration
  25. @SpringBootApplication
  26. @RestController
  27. @RequestMapping("generator")
  28. public class GeneratorApp {
  29. public static void main(String[] args) {
  30. SpringApplication.run(GeneratorApp.class, args);
  31. }
  32. public GeneratorApp(SysGeneratorService sysGeneratorService) {
  33. this.sysGeneratorService = sysGeneratorService;
  34. }
  35. // ====================== 由于本工程业务简单,直接写在启动类方便调试 ==========================
  36. private final SysGeneratorService sysGeneratorService;
  37. /**
  38. * ===========【 列出数据库中所有表 】===========
  39. * <a href="http://localhost:8080/generator/list?page=1&limit=100">列出数据库中所有表</a>
  40. */
  41. @ResponseBody
  42. @RequestMapping("list")
  43. public BaseResult<ListResult> list(@RequestParam Map<String, Object> params) {
  44. return BaseResult.success(sysGeneratorService.queryList(params));
  45. }
  46. /**
  47. * ===========【 生成代码 】===========
  48. * web 中不需要主动关闭流
  49. * <a href="http://localhost:8080/generator/code?tables=_all">所有表生成代码</a>
  50. * <a href="http://localhost:8080/generator/code?tables=system_lock">所有表生成代码</a>
  51. * @param tables 表名,逗号分隔,_all 全部
  52. */
  53. @RequestMapping("code")
  54. public void code(String tables, HttpServletResponse response) throws IOException {
  55. if (StringUtils.isEmpty(tables)) {
  56. throw new IllegalArgumentException("tableName can't be empty");
  57. }
  58. response.reset();
  59. byte[] data = "_all".equals(tables) ? sysGeneratorService.generatorCode(response.getOutputStream())
  60. :sysGeneratorService.generatorCode(tables.split(","), response.getOutputStream());
  61. if (data == null || data.length == 0) {
  62. return;
  63. }
  64. response.setHeader("Content-Disposition", "attachment; filename=\"generator.zip\"");
  65. response.setContentType("application/octet-stream; charset=UTF-8");
  66. // ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。加上这行有下载进度,不加可能报错
  67. response.addHeader("Content-Length", String.valueOf(data.length));
  68. // response out put stream 会自动关闭
  69. IOUtils.write(data, response.getOutputStream());
  70. }
  71. }