mirror of https://github.com/elunez/eladmin
代码生成器优化
parent
5ab7fb5b73
commit
41b562f374
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package me.zhengjie.domain;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 代码生成配置
|
||||
|
@ -10,20 +12,35 @@ import javax.persistence.*;
|
|||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@NoArgsConstructor
|
||||
@Table(name = "gen_config")
|
||||
public class GenConfig {
|
||||
|
||||
public GenConfig(String tableName) {
|
||||
this.cover = false;
|
||||
this.moduleName = "eladmin-system";
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@NotBlank
|
||||
private String tableName;
|
||||
|
||||
// 包路径
|
||||
@NotBlank
|
||||
private String pack;
|
||||
|
||||
// 模块名
|
||||
@Column(name = "module_name")
|
||||
@NotBlank
|
||||
private String moduleName;
|
||||
|
||||
// 前端文件路径
|
||||
@NotBlank
|
||||
private String path;
|
||||
|
||||
// 前端文件路径
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -8,4 +8,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||
* @date 2019-01-14
|
||||
*/
|
||||
public interface GenConfigRepository extends JpaRepository<GenConfig,Long> {
|
||||
|
||||
GenConfig findByTableName(String tableName);
|
||||
}
|
||||
|
|
|
@ -25,14 +25,14 @@ public class GenConfigController {
|
|||
}
|
||||
|
||||
@ApiOperation("查询")
|
||||
@GetMapping
|
||||
public ResponseEntity get(){
|
||||
return new ResponseEntity<>(genConfigService.find(), HttpStatus.OK);
|
||||
@GetMapping(value = "/{tableName}")
|
||||
public ResponseEntity get(@PathVariable String tableName){
|
||||
return new ResponseEntity<>(genConfigService.find(tableName), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("修改")
|
||||
@PutMapping
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package me.zhengjie.rest;
|
||||
|
||||
import cn.hutool.core.util.PageUtil;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import me.zhengjie.domain.vo.ColumnInfo;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.service.GenConfigService;
|
||||
import me.zhengjie.service.GeneratorService;
|
||||
import me.zhengjie.utils.PageUtil;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
@ -34,7 +34,7 @@ public class GeneratorController {
|
|||
this.genConfigService = genConfigService;
|
||||
}
|
||||
|
||||
@ApiOperation("查询数据库元数据")
|
||||
@ApiOperation("查询数据库数据")
|
||||
@GetMapping(value = "/tables")
|
||||
public ResponseEntity getTables(@RequestParam(defaultValue = "") String name,
|
||||
@RequestParam(defaultValue = "0")Integer page,
|
||||
|
@ -43,10 +43,21 @@ public class GeneratorController {
|
|||
return new ResponseEntity<>(generatorService.getTables(name,startEnd), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("查询表内元数据")
|
||||
@ApiOperation("查询字段数据")
|
||||
@GetMapping(value = "/columns")
|
||||
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("生成代码")
|
||||
|
@ -55,7 +66,7 @@ public class GeneratorController {
|
|||
if(!generatorEnabled){
|
||||
throw new BadRequestException("此环境不允许生成代码!");
|
||||
}
|
||||
generatorService.generator(columnInfos,genConfigService.find(),tableName);
|
||||
generatorService.generator(columnInfos,genConfigService.find(tableName),tableName);
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import me.zhengjie.domain.GenConfig;
|
|||
*/
|
||||
public interface GenConfigService {
|
||||
|
||||
GenConfig find();
|
||||
GenConfig find(String tableName);
|
||||
|
||||
GenConfig update(GenConfig genConfig);
|
||||
GenConfig update(String tableName, GenConfig genConfig);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package me.zhengjie.service;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
|
@ -23,7 +25,7 @@ public interface GeneratorService {
|
|||
* @param name 表名
|
||||
* @return /
|
||||
*/
|
||||
Object getColumns(String name);
|
||||
List<ColumnInfo> getColumns(String name);
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
|
@ -32,4 +34,17 @@ public interface GeneratorService {
|
|||
* @param 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);
|
||||
}
|
||||
|
|
|
@ -4,11 +4,10 @@ import me.zhengjie.domain.GenConfig;
|
|||
import me.zhengjie.repository.GenConfigRepository;
|
||||
import me.zhengjie.service.GenConfigService;
|
||||
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.stereotype.Service;
|
||||
import java.io.File;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
|
@ -25,16 +24,18 @@ public class GenConfigServiceImpl implements GenConfigService {
|
|||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'1'")
|
||||
public GenConfig find() {
|
||||
Optional<GenConfig> genConfig = genConfigRepository.findById(1L);
|
||||
return genConfig.orElseGet(GenConfig::new);
|
||||
@Cacheable(key = "#p0")
|
||||
public GenConfig find(String tableName) {
|
||||
GenConfig genConfig = genConfigRepository.findByTableName(tableName);
|
||||
if(genConfig == null){
|
||||
return new GenConfig(tableName);
|
||||
}
|
||||
return genConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
@CacheEvict(allEntries = true)
|
||||
public GenConfig update(GenConfig genConfig) {
|
||||
genConfig.setId(1L);
|
||||
@CachePut(key = "#p0")
|
||||
public GenConfig update(String tableName, GenConfig genConfig) {
|
||||
// 自动设置Api路径,注释掉前需要同步取消前端的注释
|
||||
String separator = File.separator;
|
||||
String[] paths;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package me.zhengjie.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
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.exception.BadRequestException;
|
||||
import me.zhengjie.repository.ColumnInfoRepository;
|
||||
import me.zhengjie.service.GeneratorService;
|
||||
import me.zhengjie.utils.GenUtil;
|
||||
import me.zhengjie.utils.PageUtil;
|
||||
|
@ -27,6 +29,12 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
private final ColumnInfoRepository columnInfoRepository;
|
||||
|
||||
public GeneratorServiceImpl(ColumnInfoRepository columnInfoRepository) {
|
||||
this.columnInfoRepository = columnInfoRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("all")
|
||||
public Object getTables(String name, int[] startEnd) {
|
||||
|
@ -50,20 +58,49 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||
}
|
||||
|
||||
@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")
|
||||
public Object getColumns(String name) {
|
||||
public List<ColumnInfo> query(String tableName){
|
||||
// 使用预编译防止sql注入
|
||||
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";
|
||||
Query query = em.createNativeQuery(sql);
|
||||
query.setParameter(1, StringUtils.isNotBlank(name) ? name : null);
|
||||
query.setParameter(1,tableName);
|
||||
List result = query.getResultList();
|
||||
List<ColumnInfo> columnInfos = new ArrayList<>();
|
||||
for (Object obj : result) {
|
||||
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
|
||||
|
|
|
@ -4,7 +4,7 @@ import cn.hutool.core.util.StrUtil;
|
|||
import cn.hutool.extra.template.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.domain.GenConfig;
|
||||
import me.zhengjie.domain.vo.ColumnInfo;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
@ -95,13 +95,13 @@ public class GenUtil {
|
|||
List<Map<String,Object>> queryColumns = new ArrayList<>();
|
||||
for (ColumnInfo column : columnInfos) {
|
||||
Map<String,Object> listMap = new HashMap<>();
|
||||
listMap.put("columnComment",column.getColumnComment());
|
||||
listMap.put("columnKey",column.getColumnKey());
|
||||
listMap.put("columnComment",column.getRemark());
|
||||
listMap.put("columnKey",column.getKeyType());
|
||||
|
||||
String colType = ColUtil.cloToJava(column.getColumnType().toString());
|
||||
String changeColumnName = StringUtils.toCamelCase(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("pkChangeColName",changeColumnName);
|
||||
map.put("pkCapitalColName",capitalColumnName);
|
||||
|
@ -117,14 +117,14 @@ public class GenUtil {
|
|||
}
|
||||
listMap.put("columnType",colType);
|
||||
listMap.put("columnName",column.getColumnName());
|
||||
listMap.put("isNullable",column.getIsNullable());
|
||||
listMap.put("columnShow",column.getColumnShow());
|
||||
listMap.put("isNullable",column.getNotNull());
|
||||
listMap.put("columnShow",column.getListShow());
|
||||
listMap.put("changeColumnName",changeColumnName);
|
||||
listMap.put("capitalColumnName",capitalColumnName);
|
||||
|
||||
// 判断是否有查询,如有则把查询的字段set进columnQuery
|
||||
if(!StringUtils.isBlank(column.getColumnQuery())){
|
||||
listMap.put("columnQuery",column.getColumnQuery());
|
||||
if(!StringUtils.isBlank(column.getQueryType())){
|
||||
listMap.put("columnQuery",column.getQueryType());
|
||||
map.put("hasQuery",true);
|
||||
if(TIMESTAMP.equals(colType)){
|
||||
map.put("queryHasTimestamp",true);
|
||||
|
|
|
@ -42,6 +42,14 @@ public class DictController {
|
|||
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("查询字典")
|
||||
@ApiOperation("查询字典")
|
||||
@GetMapping
|
||||
|
|
Loading…
Reference in New Issue