代码生成器优化

pull/214/head
dqjdda 2019-11-17 22:40:27 +08:00
parent 5ab7fb5b73
commit 41b562f374
13 changed files with 215 additions and 76 deletions

View File

@ -0,0 +1,74 @@
package me.zhengjie.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
/**
*
* @author Zheng Jie
* @date 2019-01-02
*/
@Data
@Entity
@NoArgsConstructor
@Table(name = "column_config")
public class ColumnInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
private String tableName;
// 数据库字段名称
private String columnName;
// 数据库字段类型
private String columnType;
// 数据库字段键类型
private String keyType;
// 字段额外的参数
private String extra;
// 数据库字段描述
private String remark;
// 必填
private Boolean notNull;
// 是否在列表显示
private Boolean listShow;
// 是否表单显示
private Boolean formShow;
// 表单类型
private String formType;
// 查询 1:模糊 2精确
private String queryType;
// 字典名称
private String dictName;
// 关联表名
private String joinName;
public ColumnInfo(String tableName, String columnName, Boolean notNull, String columnType, String remark, String keyType, String extra) {
this.tableName = tableName;
this.columnName = columnName;
this.columnType = columnType;
this.keyType = keyType;
this.extra = extra;
this.remark = remark;
this.notNull = notNull;
this.listShow = true;
this.formShow = true;
}
}

View File

@ -1,7 +1,9 @@
package me.zhengjie.domain; package me.zhengjie.domain;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotBlank;
/** /**
* *
@ -10,20 +12,35 @@ import javax.persistence.*;
*/ */
@Data @Data
@Entity @Entity
@NoArgsConstructor
@Table(name = "gen_config") @Table(name = "gen_config")
public class GenConfig { public class GenConfig {
public GenConfig(String tableName) {
this.cover = false;
this.moduleName = "eladmin-system";
this.tableName = tableName;
}
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id; private Long id;
@NotBlank
private String tableName;
// 包路径 // 包路径
@NotBlank
private String pack; private String pack;
// 模块名 // 模块名
@Column(name = "module_name") @Column(name = "module_name")
@NotBlank
private String moduleName; private String moduleName;
// 前端文件路径 // 前端文件路径
@NotBlank
private String path; private String path;
// 前端文件路径 // 前端文件路径

View File

@ -1,40 +0,0 @@
package me.zhengjie.domain.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @author Zheng Jie
* @date 2019-01-02
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ColumnInfo {
// 数据库字段名称
private Object columnName;
// 允许空值
private Object isNullable;
// 数据库字段类型
private Object columnType;
// 数据库字段注释
private Object columnComment;
// 数据库字段键类型
private Object columnKey;
// 额外的参数
private Object extra;
// 查询 1:模糊 2精确
private String columnQuery;
// 是否在列表显示
private String columnShow;
}

View File

@ -0,0 +1,14 @@
package me.zhengjie.repository;
import me.zhengjie.domain.ColumnInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* @author Zheng Jie
* @date 2019-01-14
*/
public interface ColumnInfoRepository extends JpaRepository<ColumnInfo,Long> {
List<ColumnInfo> findByTableNameOrderByIdAsc(String tableName);
}

View File

@ -8,4 +8,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
* @date 2019-01-14 * @date 2019-01-14
*/ */
public interface GenConfigRepository extends JpaRepository<GenConfig,Long> { public interface GenConfigRepository extends JpaRepository<GenConfig,Long> {
GenConfig findByTableName(String tableName);
} }

View File

@ -25,14 +25,14 @@ public class GenConfigController {
} }
@ApiOperation("查询") @ApiOperation("查询")
@GetMapping @GetMapping(value = "/{tableName}")
public ResponseEntity get(){ public ResponseEntity get(@PathVariable String tableName){
return new ResponseEntity<>(genConfigService.find(), HttpStatus.OK); return new ResponseEntity<>(genConfigService.find(tableName), HttpStatus.OK);
} }
@ApiOperation("修改") @ApiOperation("修改")
@PutMapping @PutMapping
public ResponseEntity emailConfig(@Validated @RequestBody GenConfig genConfig){ public ResponseEntity emailConfig(@Validated @RequestBody GenConfig genConfig){
return new ResponseEntity<>(genConfigService.update(genConfig),HttpStatus.OK); return new ResponseEntity<>(genConfigService.update(genConfig.getTableName(), genConfig),HttpStatus.OK);
} }
} }

View File

@ -1,12 +1,12 @@
package me.zhengjie.rest; package me.zhengjie.rest;
import cn.hutool.core.util.PageUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import me.zhengjie.domain.vo.ColumnInfo; import me.zhengjie.domain.ColumnInfo;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.service.GenConfigService; import me.zhengjie.service.GenConfigService;
import me.zhengjie.service.GeneratorService; import me.zhengjie.service.GeneratorService;
import me.zhengjie.utils.PageUtil;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -34,7 +34,7 @@ public class GeneratorController {
this.genConfigService = genConfigService; this.genConfigService = genConfigService;
} }
@ApiOperation("查询数据库数据") @ApiOperation("查询数据库数据")
@GetMapping(value = "/tables") @GetMapping(value = "/tables")
public ResponseEntity getTables(@RequestParam(defaultValue = "") String name, public ResponseEntity getTables(@RequestParam(defaultValue = "") String name,
@RequestParam(defaultValue = "0")Integer page, @RequestParam(defaultValue = "0")Integer page,
@ -43,10 +43,21 @@ public class GeneratorController {
return new ResponseEntity<>(generatorService.getTables(name,startEnd), HttpStatus.OK); return new ResponseEntity<>(generatorService.getTables(name,startEnd), HttpStatus.OK);
} }
@ApiOperation("查询表内元数据") @ApiOperation("查询字段数据")
@GetMapping(value = "/columns") @GetMapping(value = "/columns")
public ResponseEntity getTables(@RequestParam String tableName){ public ResponseEntity getTables(@RequestParam String tableName){
return new ResponseEntity<>(generatorService.getColumns(tableName), HttpStatus.OK); List<ColumnInfo> columnInfos = generatorService.getColumns(tableName);
// 异步同步表信息
generatorService.sync(columnInfos);
return new ResponseEntity<>(PageUtil.toPage(columnInfos,columnInfos.size()), HttpStatus.OK);
}
@ApiOperation("保存字段数据")
@PutMapping
public ResponseEntity save(@RequestBody List<ColumnInfo> columnInfos){
// 异步同步表信息
generatorService.save(columnInfos);
return new ResponseEntity(HttpStatus.OK);
} }
@ApiOperation("生成代码") @ApiOperation("生成代码")
@ -55,7 +66,7 @@ public class GeneratorController {
if(!generatorEnabled){ if(!generatorEnabled){
throw new BadRequestException("此环境不允许生成代码!"); throw new BadRequestException("此环境不允许生成代码!");
} }
generatorService.generator(columnInfos,genConfigService.find(),tableName); generatorService.generator(columnInfos,genConfigService.find(tableName),tableName);
return new ResponseEntity(HttpStatus.OK); return new ResponseEntity(HttpStatus.OK);
} }
} }

View File

@ -8,7 +8,7 @@ import me.zhengjie.domain.GenConfig;
*/ */
public interface GenConfigService { public interface GenConfigService {
GenConfig find(); GenConfig find(String tableName);
GenConfig update(GenConfig genConfig); GenConfig update(String tableName, GenConfig genConfig);
} }

View File

@ -1,7 +1,9 @@
package me.zhengjie.service; package me.zhengjie.service;
import me.zhengjie.domain.GenConfig; import me.zhengjie.domain.GenConfig;
import me.zhengjie.domain.vo.ColumnInfo; import me.zhengjie.domain.ColumnInfo;
import org.springframework.scheduling.annotation.Async;
import java.util.List; import java.util.List;
/** /**
@ -23,7 +25,7 @@ public interface GeneratorService {
* @param name * @param name
* @return / * @return /
*/ */
Object getColumns(String name); List<ColumnInfo> getColumns(String name);
/** /**
* *
@ -32,4 +34,17 @@ public interface GeneratorService {
* @param tableName * @param tableName
*/ */
void generator(List<ColumnInfo> columnInfos, GenConfig genConfig, String tableName); void generator(List<ColumnInfo> columnInfos, GenConfig genConfig, String tableName);
/**
*
* @param columnInfos /
*/
@Async
void sync(List<ColumnInfo> columnInfos);
/**
*
* @param columnInfos /
*/
void save(List<ColumnInfo> columnInfos);
} }

View File

@ -4,11 +4,10 @@ import me.zhengjie.domain.GenConfig;
import me.zhengjie.repository.GenConfigRepository; import me.zhengjie.repository.GenConfigRepository;
import me.zhengjie.service.GenConfigService; import me.zhengjie.service.GenConfigService;
import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.File; import java.io.File;
import java.util.Optional;
/** /**
* @author Zheng Jie * @author Zheng Jie
@ -25,16 +24,18 @@ public class GenConfigServiceImpl implements GenConfigService {
} }
@Override @Override
@Cacheable(key = "'1'") @Cacheable(key = "#p0")
public GenConfig find() { public GenConfig find(String tableName) {
Optional<GenConfig> genConfig = genConfigRepository.findById(1L); GenConfig genConfig = genConfigRepository.findByTableName(tableName);
return genConfig.orElseGet(GenConfig::new); if(genConfig == null){
return new GenConfig(tableName);
}
return genConfig;
} }
@Override @Override
@CacheEvict(allEntries = true) @CachePut(key = "#p0")
public GenConfig update(GenConfig genConfig) { public GenConfig update(String tableName, GenConfig genConfig) {
genConfig.setId(1L);
// 自动设置Api路径注释掉前需要同步取消前端的注释 // 自动设置Api路径注释掉前需要同步取消前端的注释
String separator = File.separator; String separator = File.separator;
String[] paths; String[] paths;

View File

@ -1,10 +1,12 @@
package me.zhengjie.service.impl; package me.zhengjie.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import me.zhengjie.domain.GenConfig; import me.zhengjie.domain.GenConfig;
import me.zhengjie.domain.vo.ColumnInfo; import me.zhengjie.domain.ColumnInfo;
import me.zhengjie.domain.vo.TableInfo; import me.zhengjie.domain.vo.TableInfo;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.repository.ColumnInfoRepository;
import me.zhengjie.service.GeneratorService; import me.zhengjie.service.GeneratorService;
import me.zhengjie.utils.GenUtil; import me.zhengjie.utils.GenUtil;
import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.PageUtil;
@ -27,6 +29,12 @@ public class GeneratorServiceImpl implements GeneratorService {
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;
private final ColumnInfoRepository columnInfoRepository;
public GeneratorServiceImpl(ColumnInfoRepository columnInfoRepository) {
this.columnInfoRepository = columnInfoRepository;
}
@Override @Override
@SuppressWarnings("all") @SuppressWarnings("all")
public Object getTables(String name, int[] startEnd) { public Object getTables(String name, int[] startEnd) {
@ -50,20 +58,49 @@ public class GeneratorServiceImpl implements GeneratorService {
} }
@Override @Override
public List<ColumnInfo> getColumns(String tableName) {
List<ColumnInfo> columnInfos = columnInfoRepository.findByTableNameOrderByIdAsc(tableName);
if(CollectionUtil.isNotEmpty(columnInfos)){
return columnInfos;
} else {
columnInfos = query(tableName);
return columnInfoRepository.saveAll(columnInfos);
}
}
@SuppressWarnings("all") @SuppressWarnings("all")
public Object getColumns(String name) { public List<ColumnInfo> query(String tableName){
// 使用预编译防止sql注入 // 使用预编译防止sql注入
String sql = "select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " + String sql = "select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " +
"where table_name = ? and table_schema = (select database()) order by ordinal_position"; "where table_name = ? and table_schema = (select database()) order by ordinal_position";
Query query = em.createNativeQuery(sql); Query query = em.createNativeQuery(sql);
query.setParameter(1, StringUtils.isNotBlank(name) ? name : null); query.setParameter(1,tableName);
List result = query.getResultList(); List result = query.getResultList();
List<ColumnInfo> columnInfos = new ArrayList<>(); List<ColumnInfo> columnInfos = new ArrayList<>();
for (Object obj : result) { for (Object obj : result) {
Object[] arr = (Object[]) obj; Object[] arr = (Object[]) obj;
columnInfos.add(new ColumnInfo(arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],null,"true")); columnInfos.add(
new ColumnInfo(
tableName,
arr[0].toString(),
arr[1].equals("NO"),
arr[2].toString(),
ObjectUtil.isNotNull(arr[3]) ? arr[3].toString() : null,
ObjectUtil.isNotNull(arr[4]) ? arr[4].toString() : null,
ObjectUtil.isNotNull(arr[5]) ? arr[5].toString() : null)
);
} }
return PageUtil.toPage(columnInfos,columnInfos.size()); return columnInfos;
}
@Override
public void sync(List<ColumnInfo> columnInfos) {
}
@Override
public void save(List<ColumnInfo> columnInfos) {
columnInfoRepository.saveAll(columnInfos);
} }
@Override @Override

View File

@ -4,7 +4,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.*; import cn.hutool.extra.template.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.domain.GenConfig; import me.zhengjie.domain.GenConfig;
import me.zhengjie.domain.vo.ColumnInfo; import me.zhengjie.domain.ColumnInfo;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
@ -95,13 +95,13 @@ public class GenUtil {
List<Map<String,Object>> queryColumns = new ArrayList<>(); List<Map<String,Object>> queryColumns = new ArrayList<>();
for (ColumnInfo column : columnInfos) { for (ColumnInfo column : columnInfos) {
Map<String,Object> listMap = new HashMap<>(); Map<String,Object> listMap = new HashMap<>();
listMap.put("columnComment",column.getColumnComment()); listMap.put("columnComment",column.getRemark());
listMap.put("columnKey",column.getColumnKey()); listMap.put("columnKey",column.getKeyType());
String colType = ColUtil.cloToJava(column.getColumnType().toString()); String colType = ColUtil.cloToJava(column.getColumnType().toString());
String changeColumnName = StringUtils.toCamelCase(column.getColumnName().toString()); String changeColumnName = StringUtils.toCamelCase(column.getColumnName().toString());
String capitalColumnName = StringUtils.toCapitalizeCamelCase(column.getColumnName().toString()); String capitalColumnName = StringUtils.toCapitalizeCamelCase(column.getColumnName().toString());
if(PK.equals(column.getColumnKey())){ if(PK.equals(column.getKeyType())){
map.put("pkColumnType",colType); map.put("pkColumnType",colType);
map.put("pkChangeColName",changeColumnName); map.put("pkChangeColName",changeColumnName);
map.put("pkCapitalColName",capitalColumnName); map.put("pkCapitalColName",capitalColumnName);
@ -117,14 +117,14 @@ public class GenUtil {
} }
listMap.put("columnType",colType); listMap.put("columnType",colType);
listMap.put("columnName",column.getColumnName()); listMap.put("columnName",column.getColumnName());
listMap.put("isNullable",column.getIsNullable()); listMap.put("isNullable",column.getNotNull());
listMap.put("columnShow",column.getColumnShow()); listMap.put("columnShow",column.getListShow());
listMap.put("changeColumnName",changeColumnName); listMap.put("changeColumnName",changeColumnName);
listMap.put("capitalColumnName",capitalColumnName); listMap.put("capitalColumnName",capitalColumnName);
// 判断是否有查询如有则把查询的字段set进columnQuery // 判断是否有查询如有则把查询的字段set进columnQuery
if(!StringUtils.isBlank(column.getColumnQuery())){ if(!StringUtils.isBlank(column.getQueryType())){
listMap.put("columnQuery",column.getColumnQuery()); listMap.put("columnQuery",column.getQueryType());
map.put("hasQuery",true); map.put("hasQuery",true);
if(TIMESTAMP.equals(colType)){ if(TIMESTAMP.equals(colType)){
map.put("queryHasTimestamp",true); map.put("queryHasTimestamp",true);

View File

@ -42,6 +42,14 @@ public class DictController {
dictService.download(dictService.queryAll(criteria), response); dictService.download(dictService.queryAll(criteria), response);
} }
@Log("查询字典")
@ApiOperation("查询字典")
@GetMapping(value = "/all")
@PreAuthorize("@el.check('dict:list')")
public ResponseEntity all(){
return new ResponseEntity<>(dictService.queryAll(new DictQueryCriteria()),HttpStatus.OK);
}
@Log("查询字典") @Log("查询字典")
@ApiOperation("查询字典") @ApiOperation("查询字典")
@GetMapping @GetMapping