代码生成
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package org.nl.modules.common.generator.domain;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.nl.modules.common.generator.utils.GenUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description:
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
@Data
|
||||
public class ColumnInfo implements Serializable {
|
||||
|
||||
/** 防止精度丢失 */
|
||||
@JsonSerialize(using= ToStringSerializer.class)
|
||||
private Long column_id;
|
||||
|
||||
@ApiModelProperty(value = "表名")
|
||||
private String table_name;
|
||||
|
||||
@ApiModelProperty(value = "数据库字段名称")
|
||||
private String column_name;
|
||||
|
||||
@ApiModelProperty(value = "数据库字段类型")
|
||||
private String column_type;
|
||||
|
||||
@ApiModelProperty(value = "数据库字段键类型")
|
||||
private String key_type;
|
||||
|
||||
@ApiModelProperty(value = "字段额外的参数")
|
||||
private String extra;
|
||||
|
||||
@ApiModelProperty(value = "数据库字段描述")
|
||||
private String remark;
|
||||
|
||||
@ApiModelProperty(value = "是否必填")
|
||||
private String not_null;
|
||||
|
||||
@ApiModelProperty(value = "是否在列表显示")
|
||||
private String list_show;
|
||||
|
||||
@ApiModelProperty(value = "是否表单显示")
|
||||
private String form_show;
|
||||
|
||||
@ApiModelProperty(value = "表单类型")
|
||||
private String form_type;
|
||||
|
||||
@ApiModelProperty(value = "查询 1:模糊 2:精确")
|
||||
private String query_type;
|
||||
|
||||
@ApiModelProperty(value = "字典名称")
|
||||
private String dict_name;
|
||||
|
||||
@ApiModelProperty(value = "日期注解")
|
||||
private String date_annotation;
|
||||
|
||||
public ColumnInfo(String tableName, Long column_id, String columnName, Boolean notNull, String columnType, String remark, String keyType, String extra) {
|
||||
this.table_name = tableName;
|
||||
this.column_id = column_id;
|
||||
this.column_name = columnName;
|
||||
this.column_type = columnType;
|
||||
this.key_type = keyType;
|
||||
this.extra = extra;
|
||||
this.not_null = notNull?"1":"0";
|
||||
if(GenUtil.PK.equalsIgnoreCase(keyType) && GenUtil.EXTRA.equalsIgnoreCase(extra)){
|
||||
this.not_null = "0";
|
||||
}
|
||||
this.remark = remark;
|
||||
this.list_show = "1";
|
||||
this.form_show = "1";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.nl.modules.common.generator.domain;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description: code_gen_config
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
@Data
|
||||
public class GenConfig implements Serializable {
|
||||
public GenConfig(String tableName) {
|
||||
this.table_name = tableName;
|
||||
}
|
||||
/** 防止精度丢失 */
|
||||
@JsonSerialize(using= ToStringSerializer.class)
|
||||
private Long config_id;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "表名")
|
||||
private String table_name;
|
||||
|
||||
@ApiModelProperty(value = "接口名称")
|
||||
private String api_alias;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "包路径")
|
||||
private String pack;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "模块名")
|
||||
private String module_name;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "前端文件路径")
|
||||
private String path;
|
||||
|
||||
@ApiModelProperty(value = "前端文件路径")
|
||||
private String api_path;
|
||||
|
||||
@ApiModelProperty(value = "作者")
|
||||
private String author;
|
||||
|
||||
@ApiModelProperty(value = "表前缀")
|
||||
private String prefix;
|
||||
|
||||
@ApiModelProperty(value = "是否覆盖")
|
||||
private Boolean cover = false;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.nl.modules.common.generator.domain.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description: 表的数据信息
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TableInfo {
|
||||
|
||||
/** 表名称 */
|
||||
private Object tableName;
|
||||
|
||||
/** 创建日期 */
|
||||
private Object createTime;
|
||||
|
||||
/** 数据库引擎 */
|
||||
private Object engine;
|
||||
|
||||
/** 编码集 */
|
||||
private Object coding;
|
||||
|
||||
/** 备注 */
|
||||
private Object remark;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.nl.modules.common.generator.rest;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.nl.modules.common.generator.domain.GenConfig;
|
||||
import org.nl.modules.common.generator.service.GenConfigService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description: 代码生成器配置管理
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/genConfig")
|
||||
@Api(tags = "系统:代码生成器配置管理")
|
||||
public class GenConfigController {
|
||||
private final GenConfigService genConfigService;
|
||||
|
||||
@ApiOperation("查询")
|
||||
@GetMapping(value = "/{tableName}")
|
||||
public ResponseEntity<Object> query(@PathVariable String tableName){
|
||||
return new ResponseEntity<>(genConfigService.find(tableName), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("修改")
|
||||
@PutMapping
|
||||
public ResponseEntity<Object> update(@Validated @RequestBody JSONObject genConfig){
|
||||
return new ResponseEntity<>(genConfigService.update(genConfig),HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.nl.modules.common.generator.rest;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.modules.common.generator.domain.ColumnInfo;
|
||||
import org.nl.modules.common.generator.service.GenConfigService;
|
||||
import org.nl.modules.common.generator.service.GeneratorService;
|
||||
import org.nl.modules.common.utils.PageUtil;
|
||||
import org.nl.modules.system.service.dto.UserQueryCriteria;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description: 代码生成管理
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/generator")
|
||||
@Api(tags = "系统:代码生成管理")
|
||||
public class GeneratorController {
|
||||
private final GeneratorService generatorService;
|
||||
private final GenConfigService genConfigService;
|
||||
|
||||
@Value("${generator.enabled}")
|
||||
private Boolean generatorEnabled;
|
||||
|
||||
@ApiOperation("查询数据库数据")
|
||||
@GetMapping(value = "/tables/all")
|
||||
public ResponseEntity<Object> queryTables(){
|
||||
return new ResponseEntity<>(generatorService.getTables(), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("查询数据库数据")
|
||||
@GetMapping(value = "/tables")
|
||||
public ResponseEntity<Object> queryTables(@RequestParam(defaultValue = "") String name,
|
||||
Pageable pageable){
|
||||
return new ResponseEntity<>(generatorService.getTables(name,pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("查询字段数据")
|
||||
@GetMapping(value = "/columns")
|
||||
public ResponseEntity<Object> queryColumns(@RequestParam String tableName){
|
||||
return new ResponseEntity<>(generatorService.getColumns(tableName), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("保存字段数据")
|
||||
@PutMapping
|
||||
public ResponseEntity<HttpStatus> save(@RequestBody JSONArray columnInfos){
|
||||
generatorService.save(columnInfos);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("同步字段数据")
|
||||
@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);
|
||||
}
|
||||
|
||||
@ApiOperation("生成代码")
|
||||
@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.find(tableName), generatorService.getColumns(tableName));
|
||||
break;
|
||||
// 预览
|
||||
case 1: return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName));
|
||||
// 下载
|
||||
case 2: generatorService.download(genConfigService.find(tableName), generatorService.getColumns(tableName), request, response);
|
||||
break;
|
||||
default: throw new BadRequestException("没有这个选项");
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.nl.modules.common.generator.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.nl.modules.common.generator.domain.GenConfig;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description:
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
public interface GenConfigService {
|
||||
/**
|
||||
* 查询表配置
|
||||
* @param tableName 表名
|
||||
* @return 表配置
|
||||
*/
|
||||
JSONObject find(String tableName);
|
||||
|
||||
/**
|
||||
* 更新表配置
|
||||
* @param genConfig 表配置
|
||||
* @return 表配置
|
||||
*/
|
||||
JSONObject update(JSONObject genConfig);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package org.nl.modules.common.generator.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.nl.modules.common.generator.domain.ColumnInfo;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description:
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
public interface GeneratorService {
|
||||
|
||||
/**
|
||||
* 查询数据库元数据
|
||||
* @param name 表名
|
||||
* @param pageable 分页参数
|
||||
* @return /
|
||||
*/
|
||||
Object getTables(String name, Pageable pageable);
|
||||
|
||||
/**
|
||||
* 得到数据表的元数据
|
||||
* @param name 表名
|
||||
* @return /
|
||||
*/
|
||||
JSONObject getColumns(String name);
|
||||
|
||||
/**
|
||||
* 同步表数据
|
||||
* @param columnInfos / content
|
||||
* @param columnInfoList /
|
||||
*/
|
||||
@Async
|
||||
void sync(JSONObject columnInfos, JSONArray columnInfoList);
|
||||
|
||||
/**
|
||||
* 保持数据
|
||||
* @param columnInfos /
|
||||
*/
|
||||
// void save(List<ColumnInfo> columnInfos);
|
||||
|
||||
/**
|
||||
* 获取所有table
|
||||
* @return /
|
||||
*/
|
||||
Object getTables();
|
||||
|
||||
/**
|
||||
* 代码生成
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息
|
||||
*/
|
||||
// void generator(GenConfig genConfig, List<ColumnInfo> columns);
|
||||
|
||||
/**
|
||||
* 预览
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息
|
||||
* @return /
|
||||
*/
|
||||
// ResponseEntity<Object> preview(GenConfig genConfig, List<ColumnInfo> columns);
|
||||
|
||||
/**
|
||||
* 打包下载
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息
|
||||
* @param request /
|
||||
* @param response /
|
||||
*/
|
||||
// void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response);
|
||||
|
||||
/**
|
||||
* 查询数据库的表字段数据数据
|
||||
* @param table /
|
||||
* @return /
|
||||
*/
|
||||
JSONArray query(String table);
|
||||
|
||||
/**
|
||||
* 保存字段
|
||||
* @param columnInfos
|
||||
*/
|
||||
void save(JSONArray columnInfos);
|
||||
|
||||
/**
|
||||
* 预览
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息 数组存在content
|
||||
* @return /
|
||||
*/
|
||||
ResponseEntity<Object> preview(JSONObject genConfig, JSONObject columns);
|
||||
|
||||
/**
|
||||
* 代码生成
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息 数组存在content
|
||||
*/
|
||||
void generator(JSONObject genConfig, JSONObject columns);
|
||||
|
||||
/**
|
||||
* 打包下载
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息 数组存在content
|
||||
* @param request /
|
||||
* @param response /
|
||||
*/
|
||||
void download(JSONObject genConfig, JSONObject columns, HttpServletRequest request, HttpServletResponse response);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.nl.modules.common.generator.service.impl;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.nl.modules.common.generator.domain.GenConfig;
|
||||
import org.nl.modules.common.generator.service.GenConfigService;
|
||||
import org.nl.modules.wql.core.bean.WQLObject;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description:
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GenConfigServiceImpl implements GenConfigService {
|
||||
/**
|
||||
* 查询表配置
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 表配置
|
||||
*/
|
||||
@Override
|
||||
public JSONObject find(String tableName) {
|
||||
WQLObject genTab = WQLObject.getWQLObject("code_gen_config");
|
||||
JSONObject jsonObject = genTab.query("table_name = '" + tableName + "'").uniqueResult(0);
|
||||
if (ObjectUtil.isEmpty(jsonObject))
|
||||
return JSONObject.parseObject(JSONObject.toJSONString(new GenConfig(tableName)));
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新表配置
|
||||
*
|
||||
* @param genConfig 表名 + 表配置
|
||||
* @return 表配置
|
||||
*/
|
||||
@Override
|
||||
public JSONObject update(JSONObject genConfig) {
|
||||
WQLObject genTab = WQLObject.getWQLObject("code_gen_config");
|
||||
if (ObjectUtil.isEmpty(genConfig.getString("config_id"))) {
|
||||
// 创建
|
||||
genConfig.put("config_id", IdUtil.getSnowflake(1, 1).nextId());
|
||||
genTab.insert(genConfig);
|
||||
} else {
|
||||
// 更新
|
||||
genTab.update(genConfig);
|
||||
}
|
||||
return genConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
package org.nl.modules.common.generator.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.ZipUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.modules.common.generator.domain.ColumnInfo;
|
||||
import org.nl.modules.common.generator.service.GeneratorService;
|
||||
import org.nl.modules.common.generator.utils.GenUtil;
|
||||
import org.nl.modules.common.utils.FileUtil;
|
||||
import org.nl.modules.wql.WQL;
|
||||
import org.nl.modules.wql.core.bean.WQLObject;
|
||||
import org.nl.modules.wql.util.WqlUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description:
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GeneratorServiceImpl implements GeneratorService {
|
||||
private static final Logger log = LoggerFactory.getLogger(GeneratorServiceImpl.class);
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
/**
|
||||
* 查询数据库元数据
|
||||
*
|
||||
* @param name 表名
|
||||
* @param pageable 分页参数
|
||||
* @return /
|
||||
*/
|
||||
@Override
|
||||
public Object getTables(String name, Pageable pageable) {
|
||||
JSONObject map = new JSONObject();
|
||||
map.put("flag", "1");
|
||||
map.put("table_name", name);
|
||||
JSONObject json = WQL.getWO("Generator").addParamMap(map).pageQuery(WqlUtil.getHttpContext(pageable), "create_time desc");
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到数据表的元数据
|
||||
*
|
||||
* @param name 表名
|
||||
* @return /
|
||||
*/
|
||||
@Override
|
||||
public JSONObject getColumns(String name) {
|
||||
WQLObject colTab = WQLObject.getWQLObject("code_column_config");
|
||||
JSONObject content = new JSONObject();
|
||||
JSONArray array = colTab.query("table_name = '" + name + "'").getResultJSONArray(0);
|
||||
if (ObjectUtil.isEmpty(array)) {
|
||||
array = query(name);
|
||||
// 保存
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject jsonObject = array.getJSONObject(i);
|
||||
colTab.insert(jsonObject);
|
||||
}
|
||||
}
|
||||
content.put("content", array);
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步表数据
|
||||
*
|
||||
* @param content /
|
||||
* @param columnInfoList /
|
||||
*/
|
||||
@Override
|
||||
public void sync(JSONObject content, JSONArray columnInfoList) {
|
||||
WQLObject colTab = WQLObject.getWQLObject("code_column_config");
|
||||
JSONArray columnInfos = content.getJSONArray("content");
|
||||
// 第一种情况,数据库类字段改变或者新增字段
|
||||
for (int i = 0; i < columnInfoList.size(); i++) {
|
||||
JSONObject columnInfoObj = columnInfoList.getJSONObject(i);
|
||||
JSONArray columns = new JSONArray();
|
||||
for (int j = 0; j < columnInfos.size(); j++) {
|
||||
JSONObject columnInfosObj = columnInfos.getJSONObject(j);
|
||||
if (columnInfoObj.getString("column_name").equals(columnInfosObj.getString("column_name"))) {
|
||||
columns.add(columnInfosObj);
|
||||
}
|
||||
}
|
||||
// 如果能找到,就修改部分可能被字段
|
||||
if (CollectionUtil.isNotEmpty(columns)) {
|
||||
JSONObject column = columns.getJSONObject(0);
|
||||
column.put("column_type", columnInfoObj.getString("column_type"));
|
||||
column.put("extra", columnInfoObj.getString("extra"));
|
||||
column.put("key_type", columnInfoObj.getString("key_type"));
|
||||
if (StrUtil.isEmpty(column.getString("remark"))) {
|
||||
column.put("remark", columnInfoObj.getString("remark"));
|
||||
}
|
||||
colTab.update(column);
|
||||
} else {
|
||||
// 如果找不到,则保存新字段信息
|
||||
colTab.insert(columnInfoObj);
|
||||
}
|
||||
}
|
||||
// 第二种情况,数据库字段删除了
|
||||
for (int k = 0; k < columnInfos.size(); k++) {
|
||||
JSONArray columns = new JSONArray();
|
||||
JSONObject columnInfo = columnInfos.getJSONObject(k);
|
||||
for (int l = 0; l < columnInfoList.size(); l++) {
|
||||
JSONObject c = columnInfoList.getJSONObject(l);
|
||||
if (columnInfo.getString("column_name").equals(c.getString("column_name"))) {
|
||||
columns.add(c);
|
||||
}
|
||||
}
|
||||
if (ObjectUtil.isEmpty(columnInfo)) colTab.delete(columnInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONArray query(String tableName) {
|
||||
JSONObject map = new JSONObject();
|
||||
map.put("flag", "2");
|
||||
map.put("table_name", tableName);
|
||||
JSONArray generator = WQL.getWO("Generator").addParamMap(map).process().getResultJSONArray(0);
|
||||
JSONArray columnInfos = new JSONArray();
|
||||
for (int i = 0; i < generator.size(); i++) {
|
||||
JSONObject obj = generator.getJSONObject(i);
|
||||
columnInfos.add(
|
||||
JSONObject.parseObject(JSON.toJSONString(new ColumnInfo(
|
||||
tableName,
|
||||
IdUtil.getSnowflake(1,1).nextId(),
|
||||
obj.getString("column_name"),
|
||||
"NO".equals(obj.getString("is_nullable")),
|
||||
obj.getString("data_type"),
|
||||
ObjectUtil.isNotEmpty(obj.getString("column_comment")) ? obj.getString("column_comment") : null,
|
||||
ObjectUtil.isNotEmpty(obj.getString("column_key")) ? obj.getString("column_key") : null,
|
||||
ObjectUtil.isNotEmpty(obj.getString("extra")) ? obj.getString("extra") : null
|
||||
)))
|
||||
);
|
||||
}
|
||||
return columnInfos;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存字段
|
||||
*
|
||||
* @param columnInfos
|
||||
*/
|
||||
@Override
|
||||
public void save(JSONArray columnInfos) {
|
||||
WQLObject colTab = WQLObject.getWQLObject("code_column_config");
|
||||
for (int i = 0; i < columnInfos.size(); i++) {
|
||||
JSONObject object = columnInfos.getJSONObject(i);
|
||||
colTab.update(object);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预览
|
||||
*
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息 数组存在content
|
||||
* @return /
|
||||
*/
|
||||
@Override
|
||||
public ResponseEntity<Object> preview(JSONObject genConfig, JSONObject columns) {
|
||||
JSONArray column = columns.getJSONArray("content");
|
||||
if (ObjectUtil.isEmpty(genConfig.getString("config_id"))) throw new BadRequestException("请先配置生成器");
|
||||
JSONArray genList = GenUtil.preview(column, genConfig);
|
||||
return new ResponseEntity<>(genList, HttpStatus.OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* 代码生成
|
||||
*
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息 数组存在content
|
||||
*/
|
||||
@Override
|
||||
public void generator(JSONObject genConfig, JSONObject columns) {
|
||||
JSONArray column = columns.getJSONArray("content");
|
||||
if (ObjectUtil.isEmpty(genConfig.getString("config_id"))) throw new BadRequestException("请先配置生成器");
|
||||
try {
|
||||
GenUtil.generatorCode(column, genConfig);
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new BadRequestException("生成失败,请手动处理已生成的文件");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打包下载
|
||||
*
|
||||
* @param genConfig 配置信息
|
||||
* @param columns 字段信息 数组存在content
|
||||
* @param request /
|
||||
* @param response /
|
||||
*/
|
||||
@Override
|
||||
public void download(JSONObject genConfig, JSONObject columns, HttpServletRequest request, HttpServletResponse response) {
|
||||
JSONArray column = columns.getJSONArray("content");
|
||||
if (ObjectUtil.isEmpty(genConfig.getString("config_id"))) throw new BadRequestException("请先配置生成器");
|
||||
try {
|
||||
File file = new File(GenUtil.download(column, 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("下载失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有table
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
@Override
|
||||
public Object getTables() {
|
||||
// 使用预编译防止sql注入
|
||||
JSONObject map = new JSONObject();
|
||||
map.put("flag", "1");
|
||||
JSONArray generator = WQL.getWO("Generator").addParamMap(map).process().getResultJSONArray(0);
|
||||
return generator;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.nl.modules.common.generator.utils;
|
||||
|
||||
import org.apache.commons.configuration.Configuration;
|
||||
import org.apache.commons.configuration.ConfigurationException;
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description: sql字段转java
|
||||
* @Date: 2022/12/3
|
||||
*/
|
||||
public class ColUtil {
|
||||
private static final Logger log = LoggerFactory.getLogger(ColUtil.class);
|
||||
|
||||
/**
|
||||
* 转换mysql数据类型为java数据类型
|
||||
*
|
||||
* @param type 数据库字段类型
|
||||
* @return String
|
||||
*/
|
||||
static String cloToJava(String type) {
|
||||
Configuration config = getConfig();
|
||||
assert config != null;
|
||||
return config.getString(type, "unknowType");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*/
|
||||
public static PropertiesConfiguration getConfig() {
|
||||
try {
|
||||
return new PropertiesConfiguration("generator.properties");
|
||||
} catch (ConfigurationException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
package org.nl.modules.common.generator.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.*;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.modules.common.utils.FileUtil;
|
||||
import org.nl.modules.common.utils.StringUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.nl.modules.common.utils.FileUtil.SYS_TEM_DIR;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description: 代码生成
|
||||
* @Date: 2022/12/2
|
||||
*/
|
||||
@Slf4j
|
||||
@SuppressWarnings({"unchecked", "all"})
|
||||
public class GenUtil {
|
||||
private static final String TIMESTAMP = "Timestamp";
|
||||
|
||||
private static final String Date = "Date";
|
||||
|
||||
private static final String BIGDECIMAL = "BigDecimal";
|
||||
|
||||
public static final String PK = "PRI";
|
||||
|
||||
public static final String EXTRA = "auto_increment";
|
||||
|
||||
/**
|
||||
* 获取后端代码模板名称
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
private static List<String> getAdminTemplateNames() {
|
||||
List<String> templateNames = new ArrayList<>();
|
||||
// templateNames.add("Entity");
|
||||
templateNames.add("Dto");
|
||||
// templateNames.add("Mapper");
|
||||
templateNames.add("Controller");
|
||||
//templateNames.add("QueryCriteria");
|
||||
templateNames.add("Service");
|
||||
templateNames.add("ServiceImpl");
|
||||
// templateNames.add("Repository");
|
||||
return templateNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前端代码模板名称
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
private static List<String> getFrontTemplateNames() {
|
||||
List<String> templateNames = new ArrayList<>();
|
||||
templateNames.add("index");
|
||||
templateNames.add("api");
|
||||
return templateNames;
|
||||
}
|
||||
|
||||
public static JSONArray preview(JSONArray columns, JSONObject genConfig) {
|
||||
JSONObject genMap = getGenMap(columns, genConfig);
|
||||
JSONArray genList = new JSONArray();
|
||||
// 获取后端模版
|
||||
List<String> templates = getAdminTemplateNames();
|
||||
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
for (String templateName : templates) {
|
||||
Map<String, Object> map = new HashMap<>(1);
|
||||
Template template = engine.getTemplate("generator/admin/" + templateName + ".ftl");
|
||||
map.put("content", template.render(genMap));
|
||||
map.put("name", templateName);
|
||||
genList.add(map);
|
||||
}
|
||||
// 获取前端模版
|
||||
templates = getFrontTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Map<String, Object> map = new HashMap<>(1);
|
||||
Template template = engine.getTemplate("generator/front/" + templateName + ".ftl");
|
||||
map.put(templateName, template.render(genMap));
|
||||
map.put("content", template.render(genMap));
|
||||
map.put("name", templateName);
|
||||
genList.add(map);
|
||||
}
|
||||
return genList;
|
||||
}
|
||||
|
||||
public static void generatorCode(JSONArray columns, JSONObject genConfig) throws IOException {
|
||||
JSONObject genMap = getGenMap(columns, genConfig);
|
||||
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
// 生成后端代码
|
||||
List<String> templates = getAdminTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Template template = engine.getTemplate("generator/admin/" + templateName + ".ftl");
|
||||
String filePath = getAdminFilePath(templateName, genConfig, genMap.get("className").toString(), System.getProperty("user.dir"));
|
||||
|
||||
assert filePath != null;
|
||||
File file = new File(filePath);
|
||||
|
||||
// 如果非覆盖生成
|
||||
if (!genConfig.getString("cover").equals("1") && FileUtil.exist(file)) {
|
||||
continue;
|
||||
}
|
||||
// 生成代码
|
||||
genFile(file, template, genMap);
|
||||
}
|
||||
// 生成前端代码
|
||||
templates = getFrontTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Template template = engine.getTemplate("generator/front/" + templateName + ".ftl");
|
||||
// api 和 vue文件都放一起,所以这里不需要传api_path
|
||||
String filePath = getFrontFilePath(templateName, genConfig.getString("path"), genConfig.getString("path"), genMap.get("changeClassName").toString());
|
||||
|
||||
assert filePath != null;
|
||||
File file = new File(filePath);
|
||||
|
||||
// 如果非覆盖生成
|
||||
if (!genConfig.getString("cover").equals("1") && FileUtil.exist(file)) {
|
||||
continue;
|
||||
}
|
||||
// 生成代码
|
||||
genFile(file, template, genMap);
|
||||
}
|
||||
}
|
||||
|
||||
// 下载
|
||||
public static String download(JSONArray columns, JSONObject genConfig) throws IOException {
|
||||
// 拼接的路径:/tmpnladmin-gen-temp/,这个路径在Linux下需要root用户才有权限创建,非root用户会权限错误而失败,更改为: /tmp/nladmin-gen-temp/
|
||||
// String tempPath =SYS_TEM_DIR + "nladmin-gen-temp" + File.separator + genConfig.getTableName() + File.separator;
|
||||
String tempPath = SYS_TEM_DIR + "nladmin-gen-temp" + File.separator + genConfig.getString("table_name") + File.separator;
|
||||
JSONObject genMap = getGenMap(columns, genConfig);
|
||||
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("template", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
// 生成后端代码
|
||||
List<String> templates = getAdminTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Template template = engine.getTemplate("generator/admin/" + templateName + ".ftl");
|
||||
String filePath = getAdminFilePath(templateName, genConfig, genMap.get("className").toString(), tempPath + "eladmin" + File.separator);
|
||||
assert filePath != null;
|
||||
File file = new File(filePath);
|
||||
// 如果非覆盖生成
|
||||
if (!genConfig.getString("cover").equals("1") && FileUtil.exist(file)) {
|
||||
continue;
|
||||
}
|
||||
// 生成代码
|
||||
genFile(file, template, genMap);
|
||||
}
|
||||
// 生成前端代码
|
||||
templates = getFrontTemplateNames();
|
||||
for (String templateName : templates) {
|
||||
Template template = engine.getTemplate("generator/front/" + templateName + ".ftl");
|
||||
// api 和 vue文件都放一起,所以这里不需要传api_path
|
||||
String filePath = getFrontFilePath(templateName, genConfig.getString("path"), genConfig.getString("path"), genMap.get("changeClassName").toString());
|
||||
|
||||
assert filePath != null;
|
||||
File file = new File(filePath);
|
||||
|
||||
// 如果非覆盖生成
|
||||
if (!genConfig.getString("cover").equals("1") && FileUtil.exist(file)) {
|
||||
continue;
|
||||
}
|
||||
// 生成代码
|
||||
genFile(file, template, genMap);
|
||||
}
|
||||
return tempPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义后端文件路径以及名称
|
||||
*/
|
||||
private static String getAdminFilePath(String templateName, JSONObject genConfig, String className, String rootPath) {
|
||||
// projectPath: rootPath(项目绝对路径到项目名)+File.separator(\)+genConfig.getString("module_name")(前端输入的模块名)
|
||||
// eg: D:\code\work\nl-sso-server\nladmin-system\nlsso-server
|
||||
String projectPath = rootPath + File.separator + genConfig.getString("module_name");
|
||||
// packagePath: 包路径
|
||||
// eg:D:\code\work\nl-sso-server\nladmin-system\nlsso-server\src\main\java\输入的包名
|
||||
String packagePath = projectPath + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator;
|
||||
if (!ObjectUtils.isEmpty(genConfig.getString("pack"))) { // 将点转成\
|
||||
packagePath += genConfig.getString("pack").replace(".", File.separator) + File.separator;
|
||||
}
|
||||
if ("Entity".equals(templateName)) {
|
||||
return packagePath + "domain" + File.separator + className + ".java";
|
||||
}
|
||||
|
||||
if ("Controller".equals(templateName)) {
|
||||
return packagePath + "rest" + File.separator + className + "Controller.java";
|
||||
}
|
||||
|
||||
if ("Service".equals(templateName)) {
|
||||
return packagePath + "service" + File.separator + className + "Service.java";
|
||||
}
|
||||
|
||||
if ("ServiceImpl".equals(templateName)) {
|
||||
return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
|
||||
}
|
||||
|
||||
if ("Dto".equals(templateName)) {
|
||||
return packagePath + "service" + File.separator + "dto" + File.separator + className + "Dto.java";
|
||||
}
|
||||
|
||||
if ("QueryCriteria".equals(templateName)) {
|
||||
return packagePath + "service" + File.separator + "dto" + File.separator + className + "QueryCriteria.java";
|
||||
}
|
||||
|
||||
if ("Mapper".equals(templateName)) {
|
||||
return packagePath + "service" + File.separator + "mapstruct" + File.separator + className + "Mapper.java";
|
||||
}
|
||||
|
||||
if ("Repository".equals(templateName)) {
|
||||
return packagePath + "repository" + File.separator + className + "Repository.java";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* 定义前端文件路径以及名称
|
||||
*/
|
||||
private static String getFrontFilePath(String templateName, String apiPath, String path, String apiName) {
|
||||
if ("api".equals(templateName)) {
|
||||
return apiPath + File.separator + apiName + ".js";
|
||||
}
|
||||
|
||||
if ("index".equals(templateName)) {
|
||||
return path + File.separator + "index.vue";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// 获取模版数据
|
||||
private static JSONObject getGenMap(JSONArray columnInfos, JSONObject genConfig) {
|
||||
// 存储模版字段数据
|
||||
JSONObject genMap = new JSONObject(16);
|
||||
// 接口别名
|
||||
genMap.put("apiAlias", genConfig.getString("api_alias"));
|
||||
// 包名称
|
||||
genMap.put("package", genConfig.getString("pack"));
|
||||
// 模块名称
|
||||
genMap.put("moduleName", genConfig.getString("module_name"));
|
||||
// 作者
|
||||
genMap.put("author", genConfig.getString("author"));
|
||||
// 创建日期
|
||||
genMap.put("date", LocalDate.now().toString());
|
||||
// 表名
|
||||
genMap.put("tableName", genConfig.getString("table_name"));
|
||||
// 大写开头的类名
|
||||
String className = StringUtils.toCapitalizeCamelCase(genConfig.getString("table_name"));
|
||||
// 小写开头的类名
|
||||
String changeClassName = StringUtils.toCamelCase(genConfig.getString("table_name"));
|
||||
// 判断是否去除表前缀
|
||||
if (StrUtil.isNotEmpty(genConfig.getString("prefix"))) {
|
||||
className = StringUtils.toCapitalizeCamelCase(StrUtil.removePrefix(genConfig.getString("table_name"), genConfig.getString("prefix")));
|
||||
changeClassName = StringUtils.toCamelCase(StrUtil.removePrefix(genConfig.getString("table_name"), genConfig.getString("prefix")));
|
||||
}
|
||||
// 保存类名
|
||||
genMap.put("className", className);
|
||||
// 保存小写开头的类名
|
||||
genMap.put("changeClassName", changeClassName);
|
||||
// 存在 Timestamp 字段
|
||||
genMap.put("hasTimestamp", false);
|
||||
// 查询类中存在 Timestamp 字段
|
||||
genMap.put("queryHasTimestamp", false);
|
||||
// 存在 BigDecimal 字段
|
||||
genMap.put("hasBigDecimal", false);
|
||||
// 查询类中存在 BigDecimal 字段
|
||||
genMap.put("queryHasBigDecimal", false);
|
||||
// 是否需要创建查询
|
||||
genMap.put("hasQuery", false);
|
||||
// 自增主键
|
||||
genMap.put("auto", false);
|
||||
// 存在字典
|
||||
genMap.put("hasDict", false);
|
||||
// 存在日期注解
|
||||
genMap.put("hasDateAnnotation", false);
|
||||
// 日期包
|
||||
genMap.put("hasDate", false);
|
||||
// 保存字段信息
|
||||
List<Map<String, Object>> columns = new ArrayList<>();
|
||||
// 保存查询字段的信息
|
||||
List<Map<String, Object>> queryColumns = new ArrayList<>();
|
||||
// 存储字典信息
|
||||
List<String> dicts = new ArrayList<>();
|
||||
// 存储 between 信息
|
||||
List<Map<String, Object>> betweens = new ArrayList<>();
|
||||
// 存储不为空的字段信息
|
||||
List<Map<String, Object>> isNotNullColumns = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < columnInfos.size(); i++) {
|
||||
JSONObject column = columnInfos.getJSONObject(i);
|
||||
JSONObject listMap = new JSONObject(16);
|
||||
// 字段描述
|
||||
listMap.put("remark", column.getString("remark"));
|
||||
// 字段类型
|
||||
listMap.put("columnKey", column.getString("key_type"));
|
||||
// 主键类型
|
||||
String colType = ColUtil.cloToJava(column.getString("column_type"));
|
||||
// 小写开头的字段名
|
||||
//String changeColumnName = StringUtils.toCamelCase(column.getColumnName());
|
||||
String changeColumnName = column.getString("column_name");
|
||||
// 大写开头的字段名
|
||||
String capitalColumnName = StringUtils.toCapitalizeCamelCase(column.getString("column_name"));
|
||||
if (PK.equals(column.getString("key_type"))) {
|
||||
// 存储主键类型
|
||||
genMap.put("pkColumnType", colType);
|
||||
// 存储小写开头的字段名
|
||||
genMap.put("pkChangeColName", changeColumnName);
|
||||
// 存储大写开头的字段名
|
||||
genMap.put("pkCapitalColName", capitalColumnName);
|
||||
}
|
||||
// 是否存在 Timestamp 类型的字段
|
||||
if (TIMESTAMP.equals(colType)) {
|
||||
genMap.put("hasTimestamp", true);
|
||||
}
|
||||
// 是否存在 Timestamp 类型的字段
|
||||
if (Date.equals(colType)) {
|
||||
genMap.put("hasDate", true);
|
||||
}
|
||||
// 是否存在 BigDecimal 类型的字段
|
||||
if (BIGDECIMAL.equals(colType)) {
|
||||
genMap.put("hasBigDecimal", true);
|
||||
}
|
||||
// 主键是否自增
|
||||
if (EXTRA.equals(column.getString("extra"))) {
|
||||
genMap.put("auto", true);
|
||||
}
|
||||
// 主键存在字典
|
||||
if (StrUtil.isNotEmpty(column.getString("dict_name"))) {
|
||||
genMap.put("hasDict", true);
|
||||
dicts.add(column.getString("dict_name"));
|
||||
}
|
||||
|
||||
// 存储字段类型
|
||||
listMap.put("columnType", colType);
|
||||
// 存储字原始段名称
|
||||
listMap.put("columnName", column.getString("column_name"));
|
||||
// 不为空
|
||||
listMap.put("istNotNull", (column.getString("not_null").equals("1"))?true:false);
|
||||
// 字段列表显示
|
||||
listMap.put("columnShow", (column.getString("list_show").equals("1"))?true:false);
|
||||
// 表单显示
|
||||
listMap.put("formShow", (column.getString("form_show").equals("1"))?true:false);
|
||||
// 表单组件类型
|
||||
listMap.put("formType", StrUtil.isNotEmpty(column.getString("form_type")) ? column.getString("form_type") : "Input");
|
||||
// 小写开头的字段名称
|
||||
listMap.put("changeColumnName", changeColumnName);
|
||||
//大写开头的字段名称
|
||||
listMap.put("capitalColumnName", capitalColumnName);
|
||||
// 字典名称
|
||||
listMap.put("dictName", column.getString("dict_name"));
|
||||
// 日期注解
|
||||
listMap.put("dateAnnotation", column.getString("date_annotation"));
|
||||
if (StrUtil.isNotEmpty(column.getString("date_annotation"))) {
|
||||
genMap.put("hasDateAnnotation", true);
|
||||
}
|
||||
// 添加非空字段信息
|
||||
if (column.getString("not_null").equals("1")) {
|
||||
isNotNullColumns.add(listMap);
|
||||
}
|
||||
// 判断是否有查询,如有则把查询的字段set进columnQuery
|
||||
if (!StrUtil.isEmpty(column.getString("query_type"))) {
|
||||
// 查询类型
|
||||
listMap.put("queryType", column.getString("query_type"));
|
||||
// 是否存在查询
|
||||
genMap.put("hasQuery", true);
|
||||
if (TIMESTAMP.equals(colType)) {
|
||||
// 查询中存储 Timestamp 类型
|
||||
genMap.put("queryHasTimestamp", true);
|
||||
}
|
||||
if (BIGDECIMAL.equals(colType)) {
|
||||
// 查询中存储 BigDecimal 类型
|
||||
genMap.put("queryHasBigDecimal", true);
|
||||
}
|
||||
if ("between".equalsIgnoreCase(column.getString("query_type"))) {
|
||||
betweens.add(listMap);
|
||||
} else {
|
||||
// 添加到查询列表中
|
||||
queryColumns.add(listMap);
|
||||
}
|
||||
}
|
||||
// 添加到字段列表中
|
||||
columns.add(listMap);
|
||||
}
|
||||
// 保存字段列表
|
||||
genMap.put("columns", columns);
|
||||
// 保存查询列表
|
||||
genMap.put("queryColumns", queryColumns);
|
||||
// 保存字段列表
|
||||
genMap.put("dicts", dicts);
|
||||
// 保存查询列表
|
||||
genMap.put("betweens", betweens);
|
||||
// 保存非空字段信息
|
||||
genMap.put("isNotNullColumns", isNotNullColumns);
|
||||
return genMap;
|
||||
}
|
||||
|
||||
// 生成文件
|
||||
private static void genFile(File file, Template template, Map<String, Object> map) throws IOException {
|
||||
// 生成目标文件
|
||||
Writer writer = null;
|
||||
try {
|
||||
FileUtil.touch(file);
|
||||
writer = new FileWriter(file);
|
||||
template.render(map, writer);
|
||||
} catch (TemplateException | IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
assert writer != null;
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
[交易说明]
|
||||
交易名: 任务分页查询
|
||||
所属模块:
|
||||
功能简述:
|
||||
版权所有:
|
||||
表引用:
|
||||
版本经历:
|
||||
|
||||
[数据库]
|
||||
--指定数据库,为空采用默认值,默认为db.properties中列出的第一个库
|
||||
|
||||
[IO定义]
|
||||
#################################################
|
||||
## 表字段对应输入参数
|
||||
#################################################
|
||||
输入.flag TYPEAS s_string
|
||||
输入.table_name TYPEAS s_string
|
||||
|
||||
[临时表]
|
||||
--这边列出来的临时表就会在运行期动态创建
|
||||
|
||||
[临时变量]
|
||||
--所有中间过程变量均可在此处定义
|
||||
|
||||
[业务过程]
|
||||
|
||||
##########################################
|
||||
# 1、输入输出检查 #
|
||||
##########################################
|
||||
|
||||
|
||||
##########################################
|
||||
# 2、主过程前处理 #
|
||||
##########################################
|
||||
|
||||
|
||||
##########################################
|
||||
# 3、业务主过程 #
|
||||
##########################################
|
||||
|
||||
IF 输入.flag = "1"
|
||||
PAGEQUERY
|
||||
SELECT
|
||||
table_name,
|
||||
DATE_FORMAT(now(),"%Y-%m-%d %T") create_time,
|
||||
ENGINE,
|
||||
table_collation as coding,
|
||||
table_comment as remark
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
table_schema = (SELECT DATABASE())
|
||||
OPTION 输入.table_name <> ""
|
||||
table_name like "%" 输入.table_name "%"
|
||||
ENDOPTION
|
||||
ENDSELECT
|
||||
ENDPAGEQUERY
|
||||
ENDIF
|
||||
|
||||
IF 输入.flag = "2"
|
||||
PAGEQUERY
|
||||
SELECT
|
||||
column_name,
|
||||
is_nullable,
|
||||
data_type,
|
||||
column_comment,
|
||||
column_key,
|
||||
extra
|
||||
FROM
|
||||
information_schema.COLUMNS
|
||||
WHERE
|
||||
table_schema = (
|
||||
SELECT DATABASE
|
||||
())
|
||||
OPTION 输入.table_name <> ""
|
||||
table_name = 输入.table_name
|
||||
ENDOPTION
|
||||
ORDER BY
|
||||
ordinal_position
|
||||
ENDSELECT
|
||||
ENDPAGEQUERY
|
||||
ENDIF
|
||||
Binary file not shown.
@@ -1,7 +1,8 @@
|
||||
|
||||
package ${package}.rest;
|
||||
|
||||
|
||||
import ${package}.service.${className}Service;
|
||||
import ${package}.service.dto.${className}Dto;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.nl.modules.logging.annotation.Log;
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
package ${package}.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
<#if hasTimestamp>
|
||||
import java.sql.Timestamp;
|
||||
</#if>
|
||||
<#if hasDate>
|
||||
import java.util.Date;
|
||||
import java.util.Date;
|
||||
</#if>
|
||||
<#if hasBigDecimal>
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigDecimal;
|
||||
</#if>
|
||||
import java.io.Serializable;
|
||||
<#if !auto && pkColumnType = 'Long'>
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
</#if>
|
||||
|
||||
/**
|
||||
@@ -21,18 +24,18 @@ import java.io.Serializable;
|
||||
@Data
|
||||
public class ${className}Dto implements Serializable {
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
<#list columns as column>
|
||||
|
||||
<#if column.remark != ''>
|
||||
/** ${column.remark} */
|
||||
</#if>
|
||||
<#if column.columnKey = 'PRI'>
|
||||
<#if !auto && pkColumnType = 'Long'>
|
||||
/** 防止精度丢失 */
|
||||
@JsonSerialize(using= ToStringSerializer.class)
|
||||
</#if>
|
||||
</#if>
|
||||
private ${column.columnType} ${column.changeColumnName};
|
||||
</#list>
|
||||
<#if column.remark != ''>
|
||||
/** ${column.remark} */
|
||||
</#if>
|
||||
<#if column.columnKey = 'PRI'>
|
||||
<#if !auto && pkColumnType = 'Long'>
|
||||
/** 防止精度丢失 */
|
||||
@JsonSerialize(using= ToStringSerializer.class)
|
||||
</#if>
|
||||
</#if>
|
||||
private ${column.columnType} ${column.changeColumnName};
|
||||
</#list>
|
||||
</#if>
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
package ${package}.service;
|
||||
|
||||
import ${package}.service.dto.${className}Dto;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
@@ -14,51 +14,51 @@ import javax.servlet.http.HttpServletResponse;
|
||||
**/
|
||||
public interface ${className}Service {
|
||||
|
||||
/**
|
||||
* 查询数据分页
|
||||
* @param whereJson 条件
|
||||
* @param page 分页参数
|
||||
* @return Map<String,Object>
|
||||
*/
|
||||
Map<String,Object> queryAll(Map whereJson, Pageable page);
|
||||
/**
|
||||
* 查询数据分页
|
||||
* @param whereJson 条件
|
||||
* @param page 分页参数
|
||||
* @return Map<String,Object>
|
||||
*/
|
||||
Map<String,Object> queryAll(Map whereJson, Pageable page);
|
||||
|
||||
/**
|
||||
* 查询所有数据不分页
|
||||
* @param whereJson 条件参数
|
||||
* @return List<${className}Dto>
|
||||
/**
|
||||
* 查询所有数据不分页
|
||||
* @param whereJson 条件参数
|
||||
* @return List<${className}Dto>
|
||||
*/
|
||||
List<${className}Dto> queryAll(Map whereJson);
|
||||
|
||||
/**
|
||||
* 根据ID查询
|
||||
* @param ${pkChangeColName} ID
|
||||
* @return ${className}
|
||||
*/
|
||||
${className}Dto findById(${pkColumnType} ${pkChangeColName});
|
||||
/**
|
||||
* 根据ID查询
|
||||
* @param ${pkChangeColName} ID
|
||||
* @return ${className}
|
||||
*/
|
||||
${className}Dto findById(${pkColumnType} ${pkChangeColName});
|
||||
|
||||
/**
|
||||
* 根据编码查询
|
||||
* @param code code
|
||||
* @return ${className}
|
||||
*/
|
||||
${className}Dto findByCode(String code);
|
||||
/**
|
||||
* 根据编码查询
|
||||
* @param code code
|
||||
* @return ${className}
|
||||
*/
|
||||
${className}Dto findByCode(String code);
|
||||
|
||||
|
||||
/**
|
||||
* 创建
|
||||
* @param dto /
|
||||
*/
|
||||
void create(${className}Dto dto);
|
||||
/**
|
||||
* 创建
|
||||
* @param dto /
|
||||
*/
|
||||
void create(${className}Dto dto);
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
* @param dto /
|
||||
*/
|
||||
void update(${className}Dto dto);
|
||||
/**
|
||||
* 编辑
|
||||
* @param dto /
|
||||
*/
|
||||
void update(${className}Dto dto);
|
||||
|
||||
/**
|
||||
* 多选删除
|
||||
* @param ids /
|
||||
*/
|
||||
void deleteAll(${pkColumnType}[] ids);
|
||||
}
|
||||
/**
|
||||
* 多选删除
|
||||
* @param ids /
|
||||
*/
|
||||
void deleteAll(${pkColumnType}[] ids);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
|
||||
package ${package}.service.impl;
|
||||
|
||||
|
||||
import ${package}.service.${className}Service;
|
||||
import ${package}.service.dto.${className}Dto;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -31,98 +34,98 @@ import cn.hutool.core.util.ObjectUtil;
|
||||
@Slf4j
|
||||
public class ${className}ServiceImpl implements ${className}Service {
|
||||
|
||||
@Override
|
||||
public Map<String,Object> queryAll(Map whereJson, Pageable page){
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
ResultBean rb = wo.pagequery(WqlUtil.getHttpContext(page), "1=1", "update_time desc");
|
||||
final JSONObject json = rb.pageResult();
|
||||
return json;
|
||||
}
|
||||
@Override
|
||||
public Map<String,Object> queryAll(Map whereJson, Pageable page){
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
ResultBean rb = wo.pagequery(WqlUtil.getHttpContext(page), "1=1", "update_time desc");
|
||||
final JSONObject json = rb.pageResult();
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<${className}Dto> queryAll(Map whereJson){
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONArray arr = wo.query().getResultJSONArray(0);
|
||||
if (ObjectUtil.isNotEmpty(arr)) return arr.toJavaList(${className}Dto.class);
|
||||
return null;
|
||||
@Override
|
||||
public List<${className}Dto> queryAll(Map whereJson){
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONArray arr = wo.query().getResultJSONArray(0);
|
||||
if (ObjectUtil.isNotEmpty(arr)) return arr.toJavaList(${className}Dto.class);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ${className}Dto findById(${pkColumnType} ${pkChangeColName}) {
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONObject json = wo.query("${pkChangeColName} = '" + ${pkChangeColName} + "'").uniqueResult(0);
|
||||
if (ObjectUtil.isNotEmpty(json)){
|
||||
return json.toJavaObject( ${className}Dto.class);
|
||||
}
|
||||
return null;
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONObject json = wo.query("${pkChangeColName} = '" + ${pkChangeColName} + "'").uniqueResult(0);
|
||||
if (ObjectUtil.isNotEmpty(json)){
|
||||
return json.toJavaObject( ${className}Dto.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ${className}Dto findByCode(String code) {
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONObject json = wo.query("code ='" + code + "'").uniqueResult(0);
|
||||
if (ObjectUtil.isNotEmpty(json)){
|
||||
return json.toJavaObject( ${className}Dto.class);
|
||||
}
|
||||
return null;
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONObject json = wo.query("code ='" + code + "'").uniqueResult(0);
|
||||
if (ObjectUtil.isNotEmpty(json)){
|
||||
return json.toJavaObject( ${className}Dto.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void create(${className}Dto dto) {
|
||||
Long currentUserId = SecurityUtils.getCurrentUserId();
|
||||
String nickName = SecurityUtils.getCurrentNickName();
|
||||
String now = DateUtil.now();
|
||||
Long currentUserId = SecurityUtils.getCurrentUserId();
|
||||
String nickName = SecurityUtils.getCurrentNickName();
|
||||
String now = DateUtil.now();
|
||||
|
||||
dto.set${pkChangeColName ? cap_first }(IdUtil.getSnowflake(1, 1).nextId());
|
||||
dto.setCreate_id(currentUserId);
|
||||
dto.setCreate_name(nickName);
|
||||
dto.setUpdate_optid(currentUserId);
|
||||
dto.setUpdate_optname(nickName);
|
||||
dto.setUpdate_time(now);
|
||||
dto.setCreate_time(now);
|
||||
dto.set${pkChangeColName ? cap_first }(IdUtil.getSnowflake(1, 1).nextId());
|
||||
dto.setCreate_id(currentUserId);
|
||||
dto.setCreate_name(nickName);
|
||||
dto.setUpdate_optid(currentUserId);
|
||||
dto.setUpdate_optname(nickName);
|
||||
dto.setUpdate_time(now);
|
||||
dto.setCreate_time(now);
|
||||
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONObject json = JSONObject.parseObject(JSON.toJSONString(dto));
|
||||
wo.insert(json);
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONObject json = JSONObject.parseObject(JSON.toJSONString(dto));
|
||||
wo.insert(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(${className}Dto dto) {
|
||||
${className}Dto entity = this.findById(dto.get${pkChangeColName ? cap_first }());
|
||||
if (entity == null) throw new BadRequestException("被删除或无权限,操作失败!");
|
||||
${className}Dto entity = this.findById(dto.get${pkChangeColName ? cap_first }());
|
||||
if (entity == null) throw new BadRequestException("被删除或无权限,操作失败!");
|
||||
|
||||
Long currentUserId = SecurityUtils.getCurrentUserId();
|
||||
String nickName = SecurityUtils.getCurrentNickName();
|
||||
Long currentUserId = SecurityUtils.getCurrentUserId();
|
||||
String nickName = SecurityUtils.getCurrentNickName();
|
||||
|
||||
String now = DateUtil.now();
|
||||
dto.setUpdate_time(now);
|
||||
dto.setUpdate_optid(currentUserId);
|
||||
dto.setUpdate_optname(nickName);
|
||||
String now = DateUtil.now();
|
||||
dto.setUpdate_time(now);
|
||||
dto.setUpdate_optid(currentUserId);
|
||||
dto.setUpdate_optname(nickName);
|
||||
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONObject json = JSONObject.parseObject(JSON.toJSONString(dto));
|
||||
wo.update(json);
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
JSONObject json = JSONObject.parseObject(JSON.toJSONString(dto));
|
||||
wo.update(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteAll(${pkColumnType}[] ids) {
|
||||
Long currentUserId = SecurityUtils.getCurrentUserId();
|
||||
String nickName = SecurityUtils.getCurrentNickName();
|
||||
String now = DateUtil.now();
|
||||
Long currentUserId = SecurityUtils.getCurrentUserId();
|
||||
String nickName = SecurityUtils.getCurrentNickName();
|
||||
String now = DateUtil.now();
|
||||
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
for (${pkColumnType} ${pkChangeColName}: ids) {
|
||||
JSONObject param = new JSONObject();
|
||||
param.put("${pkChangeColName}", String.valueOf(${pkChangeColName}));
|
||||
param.put("is_delete", "1");
|
||||
param.put("update_optid", currentUserId);
|
||||
param.put("update_optname", nickName);
|
||||
param.put("update_time", now);
|
||||
wo.update(param);
|
||||
}
|
||||
WQLObject wo = WQLObject.getWQLObject("${tableName}");
|
||||
for (${pkColumnType} ${pkChangeColName}: ids) {
|
||||
JSONObject param = new JSONObject();
|
||||
param.put("${pkChangeColName}", String.valueOf(${pkChangeColName}));
|
||||
param.put("is_delete", "1");
|
||||
param.put("update_optid", currentUserId);
|
||||
param.put("update_optname", nickName);
|
||||
param.put("update_time", now);
|
||||
wo.update(param);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,14 +113,14 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import crud${className} from '@/views/${changeClassName}'
|
||||
import CRUD, {crud, form, header, presenter} from '@crud/crud'
|
||||
import rrOperation from '@crud/RR.operation'
|
||||
import crudOperation from '@crud/CRUD.operation'
|
||||
import udOperation from '@crud/UD.operation'
|
||||
import pagination from '@crud/Pagination'
|
||||
import crud${className} from './${changeClassName}'
|
||||
import CRUD, {crud, form, header, presenter} from '@crud/crud'
|
||||
import rrOperation from '@crud/RR.operation'
|
||||
import crudOperation from '@crud/CRUD.operation'
|
||||
import udOperation from '@crud/UD.operation'
|
||||
import pagination from '@crud/Pagination'
|
||||
|
||||
const defaultForm = { <#if columns??><#list columns as column>${column.changeColumnName}: null<#if column_has_next>, </#if></#list></#if> }
|
||||
const defaultForm = { <#if columns??><#list columns as column>${column.changeColumnName}: null<#if column_has_next>, </#if></#list></#if> }
|
||||
export default {
|
||||
name: '${className}',
|
||||
components: { pagination, crudOperation, rrOperation, udOperation },
|
||||
|
||||
Reference in New Issue
Block a user