feat(common): 调整生成器

This commit is contained in:
15858193327
2020-11-02 01:59:53 +08:00
parent 482dec0a41
commit 6fa9d14345
14 changed files with 192 additions and 92 deletions

View File

@@ -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;
/**
* 列出数据库中所有表
* <a href="http://localhost:8080/generator/list?page=1&limit=100">列出数据库中所有表</a>
*/
@ResponseBody
@RequestMapping("list")
public RestResult<ListResult> list(@RequestParam Map<String, Object> params) {
return RestResult.success(sysGeneratorService.queryList(params));
}
/**
* 生成代码
* web 中不需要主动关闭流
* <a href="http://localhost:8080/generator/code?tables=_all">所有表</a>
* @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());
}
}

View File

@@ -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<ListResult> list(@RequestParam Map<String, Object> params) {
return RestResult.success(sysGeneratorService.queryList(params));
}
/**
* 生成代码
* web 中不需要主动关闭流
* <a href="http://localhost:8080/generator/code?tables=_all">所有表</a>
*/
@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());
}
}

View File

@@ -31,4 +31,10 @@ public class ColumnEntity {
//auto_increment
private String extra;
// 字段长度
private Integer length;
// 非空
private Boolean notEmpty;
}

View File

@@ -29,7 +29,6 @@ public class SysGeneratorServiceImpl implements SysGeneratorService {
@Autowired
private SysGeneratorMapper sysGeneratorMapper;
@Override
public PageResult queryList(Map<String, Object> map) {
//设置分页信息分别是当前页数和每页显示的总记录数【记住必须在mapper接口中的方法执行之前设置该分页信息】

View File

@@ -28,12 +28,15 @@ import java.util.zip.ZipOutputStream;
@Slf4j
public class GenUtils {
/**
* 要加载的模板(生成哪些类)
*/
public static List<String> getTemplates() {
List<String> templates = new ArrayList<String>();
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<String, Object> 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")) {

View File

@@ -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;
/**

View File

@@ -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
}

View File

@@ -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);
}

View File

@@ -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<String, Object> params);
List<${className}PO> findAll(Map<String, Object> params);
}

View File

@@ -4,7 +4,7 @@
<mapper namespace="${package}.${pkgName}.dao.${className}Mapper">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="${package}.${pkgName}.entity.${className}" id="${lowClassName}Map">
<resultMap type="${package}.${pkgName}.po.${className}PO" id="${lowClassName}Map">
#foreach($column in $columns)
<result property="${column.attributeName}" column="${column.columnName}"/>
#end
@@ -46,10 +46,12 @@
delete from ${tableName} where id = #{id}
</delete>
<select id="findAll" resultType="${package}.${pkgName}.entity.${className}">
<select id="findAll" resultType="${package}.${pkgName}.po.${className}PO">
select * from ${tableName} t
<include refid="where"/>
</select>
<sql id="where">
<where>
#foreach($column in $columns)
@@ -59,4 +61,6 @@
#end
</where>
</sql>
</mapper>

View File

@@ -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

View File

@@ -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
}

View File

@@ -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<String, Object> params);
PageResult<${className}PO> findAll(Map<String, Object> params);
}

View File

@@ -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;