diff --git a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/GeneratorApp.java b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/GeneratorApp.java index 501c330..481e4fb 100644 --- a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/GeneratorApp.java +++ b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/GeneratorApp.java @@ -1,9 +1,22 @@ package cn.itlym.shoulder.generator; +import cn.itlym.shoulder.generator.service.SysGeneratorService; +import org.apache.commons.io.IOUtils; import org.mybatis.spring.annotation.MapperScan; +import org.shoulder.core.dto.response.ListResult; +import org.shoulder.core.dto.response.RestResult; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; /** * 代码生成器,根据数据库表,生成 Entity、RestApi、Controller、Service、ServiceImpl、Repository、Mapper、Mapper.xml、前端视图 @@ -14,8 +27,58 @@ import org.springframework.context.annotation.Configuration; @MapperScan(value = "cn.itlym.shoulder.generator.mapper") @Configuration @SpringBootApplication +@RestController +@RequestMapping("generator") public class GeneratorApp { + public static void main(String[] args) { SpringApplication.run(GeneratorApp.class, args); } + + public GeneratorApp(SysGeneratorService sysGeneratorService) { + this.sysGeneratorService = sysGeneratorService; + } + + // ====================== 由于本工程业务简单,直接写在启动类方便调试 ========================== + + private final SysGeneratorService sysGeneratorService; + + /** + * 列出数据库中所有表 + * 列出数据库中所有表 + */ + @ResponseBody + @RequestMapping("list") + public RestResult list(@RequestParam Map params) { + return RestResult.success(sysGeneratorService.queryList(params)); + } + + /** + * 生成代码 + * web 中不需要主动关闭流 + * 所有表 + * @param tables 表名,逗号分隔,_all 全部 + */ + @RequestMapping("code") + public void code(String tables, HttpServletResponse response) throws IOException { + + if (StringUtils.isEmpty(tables)) { + throw new IllegalArgumentException("tableName can't be empty"); + } + response.reset(); + byte[] data = "_all".equals(tables) ? sysGeneratorService.generatorCode(response.getOutputStream()) + :sysGeneratorService.generatorCode(tables.split(","), response.getOutputStream()); + if (data == null || data.length == 0) { + return; + } + response.setHeader("Content-Disposition", "attachment; filename=\"generator.zip\""); + response.setContentType("application/octet-stream; charset=UTF-8"); + // ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。加上这行有下载进度,不加可能报错 + response.addHeader("Content-Length", String.valueOf(data.length)); + + // response out put stream 会自动关闭 + IOUtils.write(data, response.getOutputStream()); + + } + } diff --git a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/controller/GeneratorController.java b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/controller/GeneratorController.java deleted file mode 100644 index a0f4a03..0000000 --- a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/controller/GeneratorController.java +++ /dev/null @@ -1,71 +0,0 @@ -package cn.itlym.shoulder.generator.controller; - -import cn.itlym.shoulder.generator.service.SysGeneratorService; -import com.fasterxml.jackson.databind.ObjectMapper; -import io.swagger.annotations.Api; -import org.apache.commons.io.IOUtils; -import org.shoulder.core.dto.response.ListResult; -import org.shoulder.core.dto.response.RestResult; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; - -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Map; - -/** - * @author lym - */ -@RestController -@Api(tags = "代码生成器") -@RequestMapping("/generator") -public class GeneratorController { - - @Autowired - private SysGeneratorService sysGeneratorService; - - private ObjectMapper objectMapper = new ObjectMapper(); - - /** - * 列表 - */ - @ResponseBody - @RequestMapping("/list") - public RestResult list(@RequestParam Map params) { - - return RestResult.success(sysGeneratorService.queryList(params)); - } - - /** - * 生成代码 - * web 中不需要主动关闭流 - * 所有表 - */ - @RequestMapping("/code") - public void code(String tables, HttpServletResponse response) throws IOException { - - if (StringUtils.isEmpty(tables)) { - throw new IllegalArgumentException("tableName can't be empty"); - } - response.reset(); - byte[] data = "_all".equals(tables) ? sysGeneratorService.generatorCode(response.getOutputStream()) - :sysGeneratorService.generatorCode(tables.split(","), response.getOutputStream()); - if (data == null || data.length == 0) { - return; - } - response.setHeader("Content-Disposition", "attachment; filename=\"generator.zip\""); - response.setContentType("application/octet-stream; charset=UTF-8"); - // ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。加上这行有下载进度,不加可能报错 - response.addHeader("Content-Length", String.valueOf(data.length)); - - // response out put stream 会自动关闭 - IOUtils.write(data, response.getOutputStream()); - - } - - -} diff --git a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/model/ColumnEntity.java b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/model/ColumnEntity.java index 9be72da..9969205 100644 --- a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/model/ColumnEntity.java +++ b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/model/ColumnEntity.java @@ -31,4 +31,10 @@ public class ColumnEntity { //auto_increment private String extra; + // 字段长度 + private Integer length; + + // 非空 + private Boolean notEmpty; + } diff --git a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/service/impl/SysGeneratorServiceImpl.java b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/service/impl/SysGeneratorServiceImpl.java index 3fe1b43..bac96e7 100644 --- a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/service/impl/SysGeneratorServiceImpl.java +++ b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/service/impl/SysGeneratorServiceImpl.java @@ -29,7 +29,6 @@ public class SysGeneratorServiceImpl implements SysGeneratorService { @Autowired private SysGeneratorMapper sysGeneratorMapper; - @Override public PageResult queryList(Map map) { //设置分页信息,分别是当前页数和每页显示的总记录数【记住:必须在mapper接口中的方法执行之前设置该分页信息】 diff --git a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/utils/GenUtils.java b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/utils/GenUtils.java index f976add..9ad3b34 100644 --- a/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/utils/GenUtils.java +++ b/shoulder-generator/src/main/java/cn/itlym/shoulder/generator/utils/GenUtils.java @@ -28,12 +28,15 @@ import java.util.zip.ZipOutputStream; @Slf4j public class GenUtils { + /** + * 要加载的模板(生成哪些类) + */ public static List getTemplates() { List templates = new ArrayList(); templates.add("template/JpaEntity.java.vm"); templates.add("template/JpaRepository.java.vm"); - templates.add("template/Entity.java.vm"); + templates.add("template/PO.java.vm"); templates.add("template/Mapper.java.vm"); templates.add("template/Mapper.xml.vm"); templates.add("template/Service.java.vm"); @@ -79,7 +82,8 @@ public class GenUtils { //封装模板数据 Map map = new HashMap<>(); map.put("tableName", tableEntity.getTableName()); - map.put("pkgName", className.toLowerCase()); + /*map.put("pkgName", className.toLowerCase());*/ + map.put("pkgName", "xxx"); map.put("comments", tableEntity.getComments()); map.put("pk", tableEntity.getPk()); map.put("className", tableEntity.getClassName()); @@ -186,15 +190,15 @@ public class GenUtils { String packagePath = "main" + File.separator + "java" + File.separator; tableName = tableName.replace("-", "").replace("_", "").toLowerCase(); if (StringUtils.isNotBlank(packageName)) { - packagePath += packageName.replace(".", File.separator) + File.separator + tableName + File.separator; + packagePath += packageName.replace(".", File.separator) + File.separator + "xxx" + File.separator;// + tableName + File.separator; } if (template.contains("JpaEntity.java.vm")) { return packagePath + "entity" + File.separator + className + "Entity.java"; } - if (template.contains("Entity.java.vm")) { - return packagePath + "entity" + File.separator + className + ".java"; + if (template.contains("PO.java.vm")) { + return packagePath + "po" + File.separator + className + "PO.java"; } if (template.contains("Mapper.java.vm")) { diff --git a/shoulder-generator/src/main/resources/template/Controller.java.vm b/shoulder-generator/src/main/resources/template/Controller.java.vm index 91d6ca7..5c22900 100644 --- a/shoulder-generator/src/main/resources/template/Controller.java.vm +++ b/shoulder-generator/src/main/resources/template/Controller.java.vm @@ -13,7 +13,7 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.shoulder.core.dto.response.PageResult; import org.shoulder.core.dto.response.RestResult; -import ${package}.${pkgName}.entity.${className}; +import ${package}.${pkgName}.po.${className}; import ${package}.${pkgName}.service.${className}Service; /** diff --git a/shoulder-generator/src/main/resources/template/DTO.java.vm b/shoulder-generator/src/main/resources/template/DTO.java.vm new file mode 100644 index 0000000..77fcd14 --- /dev/null +++ b/shoulder-generator/src/main/resources/template/DTO.java.vm @@ -0,0 +1,48 @@ +package ${package}.${pkgName}.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.experimental.Accessors; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +#if(${hasBigDecimal}) +import java.math.BigDecimal; +#end +import java.io.Serializable; +import java.util.Date; + +/** + * ${comments} + * + * @author ${author} + * @date ${datetime} + */ + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Accessors(chain = true) +@Builder +@ApiModel(value = "${className}DTO", description = "${tableName}") +public class ${className}DTO implements Serializable { + private static final long serialVersionUID = 1L; + +#foreach ($column in $columns) + + /** + * $column.comments + */ + #if($column.notEmpty) + @NotNull(message = "$column.comments 不能为空") + #end + #if($column.length > 0) + @Length(max = $column.length, message = "$column.comments 长度不能超过 $column.length") + #end + @ApiModelProperty(value = "$column.comments", notes = "$column.comments") + private $column.attrType $column.attributeName; +#end + +} diff --git a/shoulder-generator/src/main/resources/template/JpaRepository.java.vm b/shoulder-generator/src/main/resources/template/JpaRepository.java.vm index 55f5e1c..ed2d8bd 100644 --- a/shoulder-generator/src/main/resources/template/JpaRepository.java.vm +++ b/shoulder-generator/src/main/resources/template/JpaRepository.java.vm @@ -16,20 +16,20 @@ import java.util.Map; * @author ${author} * @date ${datetime} */ -public interface ${className}Repository extends JpaRepository<${className}, Long> { +public interface ${className}Repository extends JpaRepository<${className}Entity, Long> { /** * 根据名称查询 * @param name 名称 * @return 查询结果 */ - Optional<${className}> findByName(String name); + Optional<${className}Entity> findByName(String name); /** * 根据名称模糊查询 * @param name 名称 * @return 查询结果 */ - Page<${className}> findByNameLike(String name, Pageable pageable); + Page<${className}Entity> findByNameLike(String name, Pageable pageable); } diff --git a/shoulder-generator/src/main/resources/template/Mapper.java.vm b/shoulder-generator/src/main/resources/template/Mapper.java.vm index cbcc5b4..c4c732f 100644 --- a/shoulder-generator/src/main/resources/template/Mapper.java.vm +++ b/shoulder-generator/src/main/resources/template/Mapper.java.vm @@ -1,6 +1,6 @@ package ${package}.${pkgName}.dao; -import ${package}.${pkgName}.entity.${className}; +import ${package}.${pkgName}.po.${className}; import org.apache.ibatis.annotations.Mapper; import java.util.List; @@ -14,12 +14,12 @@ import java.util.Map; @Mapper public interface ${className}Mapper { - int insert(${className} ${lowClassName}); + int insert(${className}PO ${lowClassName}); - int update(${className} ${lowClassName}); + int update(${className}PO ${lowClassName}); int delete(Long id); - List<${className}> findAll(Map params); + List<${className}PO> findAll(Map params); } diff --git a/shoulder-generator/src/main/resources/template/Mapper.xml.vm b/shoulder-generator/src/main/resources/template/Mapper.xml.vm index f7d87b3..e0d03d5 100644 --- a/shoulder-generator/src/main/resources/template/Mapper.xml.vm +++ b/shoulder-generator/src/main/resources/template/Mapper.xml.vm @@ -4,7 +4,7 @@ - + #foreach($column in $columns) #end @@ -46,10 +46,12 @@ delete from ${tableName} where id = #{id} - select * from ${tableName} t + + #foreach($column in $columns) @@ -59,4 +61,6 @@ #end + + \ No newline at end of file diff --git a/shoulder-generator/src/main/resources/template/Entity.java.vm b/shoulder-generator/src/main/resources/template/Model.java.vm similarity index 61% rename from shoulder-generator/src/main/resources/template/Entity.java.vm rename to shoulder-generator/src/main/resources/template/Model.java.vm index 3cd7c29..8989418 100644 --- a/shoulder-generator/src/main/resources/template/Entity.java.vm +++ b/shoulder-generator/src/main/resources/template/Model.java.vm @@ -1,5 +1,8 @@ -package ${package}.${pkgName}.po; +package ${package}.${pkgName}.model; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data; import lombok.NoArgsConstructor; @@ -23,8 +26,13 @@ public class ${className} implements Serializable { #foreach ($column in $columns) + /** + * $column.columnName $column.comments + */ #if($column.columnName == $pk.columnName) + @TableId(type = IdType.AUTO) #end + @TableField("$column.columnName") private $column.attrType $column.attributeName; #end diff --git a/shoulder-generator/src/main/resources/template/PO.java.vm b/shoulder-generator/src/main/resources/template/PO.java.vm new file mode 100644 index 0000000..6e622a4 --- /dev/null +++ b/shoulder-generator/src/main/resources/template/PO.java.vm @@ -0,0 +1,39 @@ +package ${package}.${pkgName}.po; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; +import lombok.NoArgsConstructor; + +#if(${hasBigDecimal}) +import java.math.BigDecimal; +#end +import java.io.Serializable; +import java.util.Date; + +/** + * ${comments} + * + * @author ${author} + * @date ${datetime} + */ + +@Data +@NoArgsConstructor +public class ${className}PO implements Serializable { + private static final long serialVersionUID = 1L; + +#foreach ($column in $columns) + + /** + * $column.columnName $column.comments + */ + #if($column.columnName == $pk.columnName) + @TableId(type = IdType.AUTO) + #end + @TableField("$column.columnName") + private $column.attrType $column.attributeName; +#end + +} diff --git a/shoulder-generator/src/main/resources/template/Service.java.vm b/shoulder-generator/src/main/resources/template/Service.java.vm index fceab32..903f749 100644 --- a/shoulder-generator/src/main/resources/template/Service.java.vm +++ b/shoulder-generator/src/main/resources/template/Service.java.vm @@ -1,6 +1,6 @@ package ${package}.${pkgName}.service; -import ${package}.${pkgName}.entity.${className}; +import ${package}.${pkgName}.po.${className}; import org.shoulder.core.dto.response.PageResult; @@ -23,7 +23,7 @@ public interface ${className}Service { * 修改 * @param ${lowClassName} */ - int update(${className} ${lowClassName}); + int update(${className}PO ${lowClassName}); /** * 删除 @@ -37,7 +37,7 @@ public interface ${className}Service { * @param params * @return */ - PageResult<${className}> findAll(Map params); + PageResult<${className}PO> findAll(Map params); } diff --git a/shoulder-generator/src/main/resources/template/ServiceImpl.java.vm b/shoulder-generator/src/main/resources/template/ServiceImpl.java.vm index d5de8a8..d5d26e1 100644 --- a/shoulder-generator/src/main/resources/template/ServiceImpl.java.vm +++ b/shoulder-generator/src/main/resources/template/ServiceImpl.java.vm @@ -10,7 +10,7 @@ import java.util.List; import java.util.Map; import org.apache.commons.collections4.MapUtils; -import ${package}.${pkgName}.entity.${className}; +import ${package}.${pkgName}.po.${className}; import ${package}.${pkgName}.dao.${className}Mapper; import ${package}.${pkgName}.service.${className}Service;