rev: 代码生成器转为mybatis-plus的生成
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package org.nl.system.controller.generator;
|
||||
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import org.nl.system.service.generator.ICodeGenConfigService;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 代码生成配置表 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
@SaIgnore
|
||||
@RestController
|
||||
|
||||
@RequestMapping("api/genConfig")
|
||||
public class CodeGenConfigController {
|
||||
|
||||
@Autowired
|
||||
private ICodeGenConfigService genConfigService;
|
||||
|
||||
|
||||
@GetMapping(value = "/{tableName}")
|
||||
public ResponseEntity<Object> query(@PathVariable String tableName) {
|
||||
return new ResponseEntity<>(genConfigService.findByTableName(tableName), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@PutMapping
|
||||
public ResponseEntity<Object> update(@Validated @RequestBody CodeGenConfig genConfig) {
|
||||
return new ResponseEntity<>(genConfigService.update(genConfig.getTableName(), genConfig), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package org.nl.system.controller.generator;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import org.nl.common.TableDataInfo;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.system.service.generator.ICodeGenConfigService;
|
||||
import org.nl.system.service.generator.ICodeGeneratorService;
|
||||
import org.nl.system.service.generator.dao.CodeColumnConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 列的数据信息表 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
@SaIgnore
|
||||
@RestController
|
||||
@RequestMapping("/api/generator")
|
||||
|
||||
public class CodeGeneratorController {
|
||||
@Autowired
|
||||
private ICodeGeneratorService generatorService;
|
||||
@Autowired
|
||||
private ICodeGenConfigService genConfigService;
|
||||
|
||||
@Value("${generator.enabled}")
|
||||
private Boolean generatorEnabled;
|
||||
|
||||
|
||||
@GetMapping(value = "/tables")
|
||||
public ResponseEntity<Object> queryTables(@RequestParam(defaultValue = "") String name, PageQuery pageable) {
|
||||
return new ResponseEntity<>(TableDataInfo.build(generatorService.getTables(name, pageable)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(value = "/columns")
|
||||
public ResponseEntity<Object> queryColumns(@RequestParam String tableName) {
|
||||
return new ResponseEntity<>(TableDataInfo.build(generatorService.getColumns(tableName)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@PutMapping
|
||||
public ResponseEntity<HttpStatus> save(@RequestBody List<CodeColumnConfig> columnInfos) {
|
||||
generatorService.updateBatchById(columnInfos);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(value = "sync")
|
||||
public ResponseEntity<HttpStatus> sync(@RequestBody List<String> tables) {
|
||||
for (String table : tables) {
|
||||
generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(value = "/{tableName}/{type}")
|
||||
public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response) {
|
||||
if (!generatorEnabled && type == 0) {
|
||||
throw new BadRequestException("此环境不允许生成代码,请选择预览或者下载查看!");
|
||||
}
|
||||
switch (type) {
|
||||
// 生成代码
|
||||
case 0:
|
||||
generatorService.generator(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName));
|
||||
break;
|
||||
// 预览
|
||||
case 1:
|
||||
return generatorService.preview(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName));
|
||||
// 打包
|
||||
case 2:
|
||||
generatorService.download(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName), request, response);
|
||||
break;
|
||||
default:
|
||||
throw new BadRequestException("没有这个选项");
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.nl.system.service.generator;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 代码生成配置表 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
public interface ICodeGenConfigService extends IService<CodeGenConfig> {
|
||||
|
||||
/**
|
||||
* 根据表名查找
|
||||
*
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
CodeGenConfig findByTableName(String tableName);
|
||||
|
||||
/**
|
||||
* 根据表名更新
|
||||
*
|
||||
* @param tableName
|
||||
* @param genConfig
|
||||
* @return
|
||||
*/
|
||||
CodeGenConfig update(String tableName, CodeGenConfig genConfig);
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package org.nl.system.service.generator;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.system.service.generator.dao.CodeColumnConfig;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
import org.nl.system.service.generator.dto.TablesInfo;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 列的数据信息表 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
public interface ICodeGeneratorService extends IService<CodeColumnConfig> {
|
||||
|
||||
/**
|
||||
* 获得所有的表格信息
|
||||
*
|
||||
* @param name
|
||||
* @param pageQuery
|
||||
* @return
|
||||
*/
|
||||
IPage<TablesInfo> getTables(String name, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 得到数据表的元数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return /
|
||||
*/
|
||||
IPage<CodeColumnConfig> getColumns(String tableName);
|
||||
|
||||
/**
|
||||
* 根据表名查询表字段
|
||||
*
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
List<CodeColumnConfig> query(String tableName);
|
||||
|
||||
/**
|
||||
* 同步表数据
|
||||
*
|
||||
* @param columnInfos /
|
||||
* @param columnInfoList /
|
||||
*/
|
||||
@Async
|
||||
void sync(IPage<CodeColumnConfig> columnInfos, List<CodeColumnConfig> columnInfoList);
|
||||
|
||||
/**
|
||||
* 预览代码
|
||||
*
|
||||
* @param byTableName
|
||||
* @param columns
|
||||
* @return
|
||||
*/
|
||||
ResponseEntity<Object> preview(CodeGenConfig byTableName, IPage<CodeColumnConfig> columns);
|
||||
|
||||
/**
|
||||
* 打包下载
|
||||
*
|
||||
* @param genConfig 配置信息
|
||||
* @param columnsPage 字段信息分页数据
|
||||
* @param request /
|
||||
* @param response /
|
||||
*/
|
||||
void download(CodeGenConfig genConfig, IPage<CodeColumnConfig> columnsPage, HttpServletRequest request, HttpServletResponse response);
|
||||
|
||||
/**
|
||||
* 代码生成
|
||||
*
|
||||
* @param genConfig 配置信息
|
||||
* @param columnsPage 字段信息分页数据
|
||||
*/
|
||||
void generator(CodeGenConfig genConfig, IPage<CodeColumnConfig> columnsPage);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package org.nl.system.service.generator.dao;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import org.nl.common.utils.GenUtil;
|
||||
import org.nl.system.service.generator.dto.ColumnInfo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 列的数据信息表
|
||||
* </p>
|
||||
*
|
||||
* @author generator
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
@Data
|
||||
@TableName("code_column_config")
|
||||
public class CodeColumnConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 标识
|
||||
*/
|
||||
@TableId(value = "column_id", type = IdType.ASSIGN_ID)
|
||||
private String columnId;
|
||||
|
||||
@TableField(value = "table_name")
|
||||
private String tableName;
|
||||
|
||||
@TableField(value = "column_name")
|
||||
private String columnName;
|
||||
|
||||
@TableField(value = "column_type")
|
||||
private String columnType;
|
||||
|
||||
@TableField(value = "key_type")
|
||||
private String keyType;
|
||||
|
||||
@TableField(value = "extra")
|
||||
private String extra;
|
||||
|
||||
@TableField(value = "remark")
|
||||
private String remark;
|
||||
|
||||
@TableField(value = "not_null")
|
||||
private Boolean notNull;
|
||||
|
||||
@TableField(value = "list_show")
|
||||
private Boolean listShow;
|
||||
|
||||
@TableField(value = "form_show")
|
||||
private Boolean formShow;
|
||||
|
||||
@TableField(value = "form_type")
|
||||
private String formType;
|
||||
|
||||
@TableField(value = "query_type")
|
||||
private String queryType;
|
||||
|
||||
@TableField(value = "dict_name")
|
||||
private String dictName;
|
||||
|
||||
@TableField(value = "date_annotation")
|
||||
private String dateAnnotation;
|
||||
|
||||
/**
|
||||
* 创建默认的实体
|
||||
*
|
||||
* @param tableName
|
||||
* @param config
|
||||
* @return
|
||||
*/
|
||||
public static CodeColumnConfig createDefault(String tableName, ColumnInfo config) {
|
||||
CodeColumnConfig columnConfig = new CodeColumnConfig();
|
||||
columnConfig.setColumnId(IdUtil.getSnowflake(1, 1).nextIdStr());
|
||||
columnConfig.setTableName(tableName);
|
||||
columnConfig.setColumnName(config.getColumnName());
|
||||
columnConfig.setColumnType(config.getColumnType());
|
||||
columnConfig.setKeyType(config.getKeyType());
|
||||
columnConfig.setExtra(config.getExtra());
|
||||
columnConfig.setNotNull((ObjectUtil.isNotEmpty(config.getKeyType())
|
||||
&& ObjectUtil.isNotEmpty(config.getExtra())
|
||||
&& GenUtil.PK.equalsIgnoreCase(config.getKeyType())
|
||||
&& GenUtil.EXTRA.equalsIgnoreCase(config.getExtra()))
|
||||
? false : ObjectUtil.isNotEmpty(config.getNotNull()) ? config.getNotNull() : false);
|
||||
columnConfig.setRemark(ObjectUtil.isNotEmpty(config.getRemark()) ? config.getRemark() : null);
|
||||
columnConfig.setListShow(true);
|
||||
columnConfig.setFormShow(true);
|
||||
return columnConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package org.nl.system.service.generator.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 代码生成配置表
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("code_gen_config")
|
||||
public class CodeGenConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CodeGenConfig(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
@TableId(value = "config_id")
|
||||
private String configId;
|
||||
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
@TableField(value = "table_name")
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 作者
|
||||
*/
|
||||
private String author;
|
||||
|
||||
/**
|
||||
* 是否覆盖
|
||||
*/
|
||||
private Boolean cover;
|
||||
|
||||
/**
|
||||
* 模块名称
|
||||
*/
|
||||
@TableField(value = "module_name")
|
||||
private String moduleName;
|
||||
|
||||
/**
|
||||
* 包名
|
||||
*/
|
||||
private String pack;
|
||||
|
||||
/**
|
||||
* 路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* api路径
|
||||
*/
|
||||
@TableField(value = "api_path")
|
||||
private String apiPath;
|
||||
|
||||
/**
|
||||
* 表前缀
|
||||
*/
|
||||
private String prefix;
|
||||
|
||||
/**
|
||||
* 接口名称
|
||||
*/
|
||||
@TableField(value = "api_alias")
|
||||
private String apiAlias;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.nl.system.service.generator.dao.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.nl.system.service.generator.dao.CodeColumnConfig;
|
||||
import org.nl.system.service.generator.dto.ColumnInfo;
|
||||
import org.nl.system.service.generator.dto.TablesInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 列的数据信息表 Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
public interface CodeColumnConfigMapper extends BaseMapper<CodeColumnConfig> {
|
||||
|
||||
/**
|
||||
* 分页查找
|
||||
*
|
||||
* @param name 表名
|
||||
* @return 表信息
|
||||
*/
|
||||
List<TablesInfo> getTables(String name, int pageSize, int offset);
|
||||
|
||||
/**
|
||||
* 分页查询的总数
|
||||
*
|
||||
* @param name 表名
|
||||
* @return 表信息
|
||||
*/
|
||||
long getTablesTotal(String name);
|
||||
|
||||
/**
|
||||
* 获取
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 列数据
|
||||
*/
|
||||
List<ColumnInfo> getTablesByTableName(String tableName);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.nl.system.service.generator.dao.mapper.CodeColumnConfigMapper">
|
||||
|
||||
<select id="getTables" resultType="org.nl.system.service.generator.dto.TablesInfo">
|
||||
SELECT
|
||||
table_name,
|
||||
create_time,
|
||||
engine,
|
||||
table_collation AS coding,
|
||||
table_comment AS remark
|
||||
FROM
|
||||
information_schema.tables
|
||||
WHERE
|
||||
table_schema = (SELECT database())
|
||||
<if test="name != null and name != ''">
|
||||
AND table_name LIKE #{name}
|
||||
</if>
|
||||
ORDER BY create_time DESC
|
||||
LIMIT #{pageSize} OFFSET #{offset}
|
||||
</select>
|
||||
<select id="getTablesTotal" resultType="java.lang.Long">
|
||||
SELECT count(*)
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = (select database())
|
||||
<if test="name != null and name != ''">
|
||||
AND table_name LIKE #{name}
|
||||
</if>
|
||||
</select>
|
||||
<select id="getTablesByTableName" resultType="org.nl.system.service.generator.dto.ColumnInfo">
|
||||
SELECT
|
||||
column_name,
|
||||
is_nullable as is_null,
|
||||
data_type as column_type,
|
||||
column_comment as remark,
|
||||
column_key as key_type,
|
||||
extra
|
||||
FROM information_schema.columns
|
||||
WHERE
|
||||
table_schema = (SELECT database())
|
||||
<if test="tableName != null and tableName != ''">
|
||||
AND table_name = #{tableName}
|
||||
</if>
|
||||
ORDER BY ordinal_position
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.nl.system.service.generator.dao.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 代码生成配置表 Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author generator
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
public interface CodeGenConfigMapper extends BaseMapper<CodeGenConfig> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.nl.system.service.generator.dao.mapper.CodeGenConfigMapper">
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.nl.system.service.generator.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author: lyd
|
||||
* @Description: 字段信息
|
||||
* @Date: 2023/5/3
|
||||
*/
|
||||
@Data
|
||||
public class ColumnInfo {
|
||||
|
||||
@TableField(value = "column_name")
|
||||
private String columnName;
|
||||
|
||||
@TableField(value = "column_type")
|
||||
private String columnType;
|
||||
|
||||
@TableField(value = "key_type")
|
||||
private String keyType;
|
||||
|
||||
|
||||
private String extra;
|
||||
|
||||
|
||||
private String remark;
|
||||
|
||||
@TableField(value = "not_null")
|
||||
private Boolean notNull;
|
||||
|
||||
@TableField(value = "list_show")
|
||||
private Boolean listShow;
|
||||
|
||||
@TableField(value = "form_show")
|
||||
private Boolean formShow;
|
||||
|
||||
@TableField(value = "form_type")
|
||||
private String formType;
|
||||
|
||||
@TableField(value = "query_type")
|
||||
private String queryType;
|
||||
|
||||
@TableField(value = "dict_name")
|
||||
private String dictName;
|
||||
|
||||
@TableField(value = "date_annotation")
|
||||
private String dateAnnotation;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.nl.system.service.generator.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @Author: lyd
|
||||
* @Description: 表的数据信息
|
||||
* @Date: 2023/5/3
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TablesInfo {
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
@TableField(value = "table_name")
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(value = "create_time")
|
||||
private String createTime;
|
||||
|
||||
/**
|
||||
* 引擎
|
||||
*/
|
||||
private String ENGINE;
|
||||
|
||||
/**
|
||||
* 字符序
|
||||
*/
|
||||
private String coding;
|
||||
|
||||
/**
|
||||
* 注释
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.nl.system.service.generator.impl;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.nl.system.service.generator.ICodeGenConfigService;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
import org.nl.system.service.generator.dao.mapper.CodeGenConfigMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 代码生成配置表 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
@Service
|
||||
public class CodeGenConfigServiceImpl extends ServiceImpl<CodeGenConfigMapper, CodeGenConfig> implements ICodeGenConfigService {
|
||||
|
||||
@Autowired
|
||||
private CodeGenConfigMapper codeGenConfigMapper;
|
||||
|
||||
@Override
|
||||
public CodeGenConfig findByTableName(String tableName) {
|
||||
CodeGenConfig codeGenConfig = codeGenConfigMapper.selectOne(new LambdaQueryWrapper<CodeGenConfig>()
|
||||
.eq(CodeGenConfig::getTableName, tableName));
|
||||
if (ObjectUtil.isEmpty(codeGenConfig)) {
|
||||
return new CodeGenConfig(tableName);
|
||||
}
|
||||
return codeGenConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodeGenConfig update(String tableName, CodeGenConfig genConfig) {
|
||||
// 如果 api 路径为空,则自动生成路径
|
||||
if (StrUtil.isEmpty(genConfig.getApiPath())) {
|
||||
String separator = File.separator;
|
||||
String[] paths;
|
||||
String symbol = "\\";
|
||||
if (symbol.equals(separator)) {
|
||||
paths = genConfig.getPath().split("\\\\");
|
||||
} else {
|
||||
paths = genConfig.getPath().split(File.separator);
|
||||
}
|
||||
StringBuilder api = new StringBuilder();
|
||||
for (String path : paths) {
|
||||
api.append(path);
|
||||
api.append(separator);
|
||||
if ("src".equals(path)) {
|
||||
api.append("api");
|
||||
break;
|
||||
}
|
||||
}
|
||||
genConfig.setApiPath(api.toString());
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(genConfig.getConfigId())) {
|
||||
codeGenConfigMapper.updateById(genConfig);
|
||||
} else {
|
||||
genConfig.setConfigId(IdUtil.getSnowflake(1, 1).nextIdStr());
|
||||
codeGenConfigMapper.insert(genConfig);
|
||||
}
|
||||
return genConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package org.nl.system.service.generator.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.ZipUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.common.utils.GenUtil;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.modules.common.utils.FileUtil;
|
||||
import org.nl.system.service.generator.ICodeGeneratorService;
|
||||
import org.nl.system.service.generator.dao.CodeColumnConfig;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
import org.nl.system.service.generator.dao.mapper.CodeColumnConfigMapper;
|
||||
import org.nl.system.service.generator.dto.ColumnInfo;
|
||||
import org.nl.system.service.generator.dto.TablesInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 列的数据信息表 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
@Service
|
||||
public class CodeGeneratorServiceImpl extends ServiceImpl<CodeColumnConfigMapper, CodeColumnConfig> implements ICodeGeneratorService {
|
||||
|
||||
@Autowired
|
||||
private CodeColumnConfigMapper columnConfigMapper;
|
||||
|
||||
@Override
|
||||
public IPage<TablesInfo> getTables(String name, PageQuery pageQuery) {
|
||||
IPage<TablesInfo> pages = new Page<>();
|
||||
int page = pageQuery.getPage();
|
||||
int pageSize = pageQuery.getSize();
|
||||
int offset = page * pageSize;
|
||||
List<TablesInfo> tableInfos = columnConfigMapper.getTables(name, pageSize, offset);
|
||||
long num = columnConfigMapper.getTablesTotal(name);
|
||||
pages.setRecords(tableInfos);
|
||||
pages.setCurrent(page + 1);
|
||||
pages.setSize(pageSize);
|
||||
pages.setTotal(num);
|
||||
return pages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPage<CodeColumnConfig> getColumns(String tableName) {
|
||||
IPage<CodeColumnConfig> pages = new Page<>();
|
||||
// 查找行列数据表
|
||||
List<CodeColumnConfig> codeColumnConfigs = columnConfigMapper
|
||||
.selectList(new LambdaQueryWrapper<CodeColumnConfig>()
|
||||
.eq(CodeColumnConfig::getTableName, tableName));
|
||||
if (ObjectUtil.isEmpty(codeColumnConfigs)) {
|
||||
// 为空查找全新的数据
|
||||
codeColumnConfigs = query(tableName);
|
||||
// 保存
|
||||
this.saveBatch(codeColumnConfigs);
|
||||
}
|
||||
pages.setRecords(codeColumnConfigs);
|
||||
return pages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodeColumnConfig> query(String tableName) {
|
||||
List<ColumnInfo> columnConfigList = columnConfigMapper.getTablesByTableName(tableName);
|
||||
// 设置默认值
|
||||
List<CodeColumnConfig> columnInfos = columnConfigList.stream()
|
||||
.map(config -> CodeColumnConfig.createDefault(tableName, config))
|
||||
.collect(Collectors.toList());
|
||||
return columnInfos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sync(IPage<CodeColumnConfig> columnInfos, List<CodeColumnConfig> columnInfoList) {
|
||||
List<CodeColumnConfig> records = columnInfos.getRecords();
|
||||
// 第一种情况,数据库类字段改变或者新增字段
|
||||
for (CodeColumnConfig columnInfo : columnInfoList) {
|
||||
// 根据字段名称查找
|
||||
List<CodeColumnConfig> columns = records.stream().filter(c -> c.getColumnName().equals(columnInfo.getColumnName())).collect(Collectors.toList());
|
||||
// 如果能找到,就修改部分可能被字段
|
||||
if (CollectionUtil.isNotEmpty(columns)) {
|
||||
CodeColumnConfig column = columns.get(0);
|
||||
column.setColumnType(columnInfo.getColumnType());
|
||||
column.setExtra(columnInfo.getExtra());
|
||||
column.setKeyType(columnInfo.getKeyType());
|
||||
if (StrUtil.isEmpty(column.getRemark())) {
|
||||
column.setRemark(columnInfo.getRemark());
|
||||
}
|
||||
columnConfigMapper.updateById(column);
|
||||
} else {
|
||||
// 如果找不到,则保存新字段信息
|
||||
columnConfigMapper.insert(columnInfo);
|
||||
}
|
||||
}
|
||||
// 第二种情况,数据库字段删除了
|
||||
for (CodeColumnConfig columnInfo : records) {
|
||||
// 根据字段名称查找
|
||||
List<CodeColumnConfig> columns = columnInfoList.stream().filter(c -> c.getColumnName().equals(columnInfo.getColumnName())).collect(Collectors.toList());
|
||||
// 如果找不到,就代表字段被删除了,则需要删除该字段
|
||||
if (CollectionUtil.isEmpty(columns)) {
|
||||
columnConfigMapper.deleteById(columnInfo.getColumnId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Object> preview(CodeGenConfig genConfig, IPage<CodeColumnConfig> columns) {
|
||||
List<CodeColumnConfig> columnsRecords = columns.getRecords();
|
||||
if (genConfig.getConfigId() == null) {
|
||||
throw new BadRequestException("请先配置生成器");
|
||||
}
|
||||
List<Map<String, Object>> genList = GenUtil.preview(columnsRecords, genConfig);
|
||||
return new ResponseEntity<>(genList, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void download(CodeGenConfig genConfig, IPage<CodeColumnConfig> columnsPage, HttpServletRequest request, HttpServletResponse response) {
|
||||
List<CodeColumnConfig> columns = columnsPage.getRecords();
|
||||
if (genConfig.getConfigId() == null) {
|
||||
throw new BadRequestException("请先配置生成器");
|
||||
}
|
||||
try {
|
||||
File file = new File(GenUtil.download(columns, genConfig));
|
||||
String zipPath = file.getPath() + ".zip";
|
||||
ZipUtil.zip(file.getPath(), zipPath);
|
||||
FileUtil.downloadFile(request, response, new File(zipPath), true);
|
||||
} catch (IOException e) {
|
||||
throw new BadRequestException("打包失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generator(CodeGenConfig genConfig, IPage<CodeColumnConfig> columnsPage) {
|
||||
List<CodeColumnConfig> columns = columnsPage.getRecords();
|
||||
if (genConfig.getConfigId() == null) {
|
||||
throw new BadRequestException("请先配置生成器");
|
||||
}
|
||||
try {
|
||||
GenUtil.generatorCode(columns, genConfig);
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new BadRequestException("生成失败,请手动处理已生成的文件");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user