diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenFormConfController.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenFormConfController.java index 2ad1077d3..a9feaa99c 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenFormConfController.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenFormConfController.java @@ -72,7 +72,7 @@ public class GenFormConfController { /** * 通过id查询生成记录 * - * @param dsName 数据源ID + * @param dsName 数据源ID * @param tableName tableName * @return R */ diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GeneratorController.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GeneratorController.java index 8cddc4419..2d4949ad0 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GeneratorController.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GeneratorController.java @@ -20,6 +20,7 @@ package com.pig4cloud.pigx.codegen.controller; import cn.hutool.core.io.IoUtil; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.pig4cloud.pigx.codegen.entity.GenConfig; +import com.pig4cloud.pigx.codegen.service.GenTableColumnService; import com.pig4cloud.pigx.codegen.service.GeneratorService; import com.pig4cloud.pigx.common.core.util.R; import lombok.AllArgsConstructor; @@ -40,6 +41,7 @@ import javax.servlet.http.HttpServletResponse; @RequestMapping("/generator") public class GeneratorController { private final GeneratorService generatorService; + private final GenTableColumnService columnService; /** * 列表 @@ -53,8 +55,34 @@ public class GeneratorController { return R.ok(generatorService.getPage(page, tableName, dsName)); } + /** + * 预览代码 + * + * @param genConfig 数据表配置 + * @return + */ + @GetMapping("/preview") + public R previewCode(GenConfig genConfig) { + return R.ok(generatorService.previewCode(genConfig)); + } + + /** + * 查询表的列信息 + * + * @param page 分页 + * @param genConfig 数据表配置 + * @return + */ + @GetMapping("/table") + public R getTable(Page page, GenConfig genConfig) { + return R.ok(columnService.listTable(page, genConfig)); + } + + /** * 生成代码 + * + * @param genConfig 数据表配置 */ @SneakyThrows @PostMapping("/code") diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/ColumnEntity.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/ColumnEntity.java index 9f783d733..6e676c27e 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/ColumnEntity.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/ColumnEntity.java @@ -34,6 +34,10 @@ public class ColumnEntity { * 数据类型 */ private String dataType; + /** + * JAVA 数据类型 + */ + private String javaType; /** * 备注 */ diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenTableColumnMapper.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenTableColumnMapper.java new file mode 100644 index 000000000..9541de956 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenTableColumnMapper.java @@ -0,0 +1,52 @@ +package com.pig4cloud.pigx.codegen.mapper; + +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.codegen.entity.ColumnEntity; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Map; + +/** + * @author lengleng + * @date 2020/5/18 + */ +@Mapper +public interface GenTableColumnMapper extends BaseMapper { + + /** + * 分页查询表分页信息 + * + * @param page + * @param tableName + * @param dsName + * @return + */ + @DS("#last") + IPage selectTableColumn(Page page, @Param("tableName") String tableName, @Param("dsName") String dsName); + + /** + * 查询表全部列信息 + * + * @param tableName + * @param dsName + * @return + */ + @DS("#last") + List selectTableColumn(@Param("tableName") String tableName, @Param("dsName") String dsName); + + + /** + * 查询表全部列信息 + * + * @param tableName 表名称 + * @param dsName 数据源名称 + * @return + */ + @DS("#last") + List> selectMapTableColumn(@Param("tableName") String tableName, String dsName); +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GeneratorMapper.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GeneratorMapper.java index 6e5639d82..fafbc2125 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GeneratorMapper.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GeneratorMapper.java @@ -38,8 +38,8 @@ public interface GeneratorMapper { /** * 分页查询表格 * - * @param page - * @param tableName + * @param page 分页信息 + * @param tableName 表名称 * @return */ IPage>> queryList(Page page, @Param("tableName") String tableName); @@ -53,14 +53,4 @@ public interface GeneratorMapper { */ @DS("#last") Map queryTable(@Param("tableName") String tableName, String dsName); - - /** - * 查询表列信息 - * - * @param tableName 表名称 - * @param dsName 数据源名称 - * @return - */ - @DS("#last") - List> queryColumns(@Param("tableName") String tableName, String dsName); } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenTableColumnService.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenTableColumnService.java new file mode 100755 index 000000000..6be08d4c5 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenTableColumnService.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.codegen.service; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.IService; +import com.pig4cloud.pigx.codegen.entity.ColumnEntity; +import com.pig4cloud.pigx.codegen.entity.GenConfig; + +/** + * 表字段管理 + * + * @author lengleng + * @date 2020-05-18 + */ +public interface GenTableColumnService extends IService { + + /** + * 查询表的字段信息 + * + * @param page + * @param genConfig 查询条件 + * @return + */ + IPage listTable(Page page, GenConfig genConfig); +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GeneratorService.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GeneratorService.java index 52c038cb1..4651e9a43 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GeneratorService.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GeneratorService.java @@ -29,21 +29,29 @@ import java.util.Map; * @date 2018/7/29 */ public interface GeneratorService { - /** - * 生成代码 - * - * @param tableNames 表名称 - * @return - */ - byte[] generatorCode(GenConfig tableNames); + /** + * 生成代码 + * + * @param genConfig 生成信息 + * @return + */ + byte[] generatorCode(GenConfig genConfig); - /** - * 分页查询表 - * - * @param page 分页信息 - * @param tableName 表名 - * @param name 数据源ID - * @return - */ - IPage>> getPage(Page page, String tableName, String name); + /** + * 分页查询表 + * + * @param page 分页信息 + * @param tableName 表名 + * @param name 数据源ID + * @return + */ + IPage>> getPage(Page page, String tableName, String name); + + /** + * 预览代码 + * + * @param genConfig 生成信息 + * @return + */ + Map previewCode(GenConfig genConfig); } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenFormConfServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenFormConfServiceImpl.java index 29a5d7a10..e5cf08c12 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenFormConfServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenFormConfServiceImpl.java @@ -23,7 +23,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.pig4cloud.pigx.codegen.entity.ColumnEntity; import com.pig4cloud.pigx.codegen.entity.GenFormConf; import com.pig4cloud.pigx.codegen.mapper.GenFormConfMapper; -import com.pig4cloud.pigx.codegen.mapper.GeneratorMapper; +import com.pig4cloud.pigx.codegen.mapper.GenTableColumnMapper; import com.pig4cloud.pigx.codegen.service.GenFormConfService; import com.pig4cloud.pigx.codegen.util.GenUtils; import lombok.AllArgsConstructor; @@ -38,7 +38,6 @@ import org.springframework.stereotype.Service; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.Properties; /** @@ -50,7 +49,7 @@ import java.util.Properties; @Service @AllArgsConstructor public class GenFormConfServiceImpl extends ServiceImpl implements GenFormConfService { - private final GeneratorMapper generatorMapper; + private final GenTableColumnMapper tableColumnMapper; /** * 1. 根据数据源、表名称,查询已配置表单信息 @@ -71,7 +70,7 @@ public class GenFormConfServiceImpl extends ServiceImpl> columns = generatorMapper.queryColumns(tableName,dsName); + List columns = tableColumnMapper.selectTableColumn(tableName, dsName); //设置velocity资源加载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", ClasspathResourceLoader.class.getName()); @@ -80,10 +79,10 @@ public class GenFormConfServiceImpl extends ServiceImpl columnList = new ArrayList<>(); - for (Map column : columns) { + for (ColumnEntity column : columns) { ColumnEntity columnEntity = new ColumnEntity(); - columnEntity.setComments(column.get("columnComment")); - columnEntity.setLowerAttrName(StringUtils.uncapitalize(GenUtils.columnToJava(column.get("columnName")))); + columnEntity.setComments(column.getComments()); + columnEntity.setLowerAttrName(StringUtils.uncapitalize(GenUtils.columnToJava(column.getColumnName()))); columnList.add(columnEntity); } context.put("columns", columnList); diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTableColumnServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTableColumnServiceImpl.java new file mode 100644 index 000000000..43079eeb2 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTableColumnServiceImpl.java @@ -0,0 +1,38 @@ +package com.pig4cloud.pigx.codegen.service.impl; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.pig4cloud.pigx.codegen.entity.ColumnEntity; +import com.pig4cloud.pigx.codegen.entity.GenConfig; +import com.pig4cloud.pigx.codegen.mapper.GenTableColumnMapper; +import com.pig4cloud.pigx.codegen.service.GenTableColumnService; +import com.pig4cloud.pigx.codegen.util.GenUtils; +import lombok.AllArgsConstructor; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.lang.StringUtils; +import org.springframework.stereotype.Service; + +/** + * 表字段信息管理 + * + * @author lengleng + * @date 2020/5/18 + */ +@Service +@AllArgsConstructor +public class GenTableColumnServiceImpl extends ServiceImpl implements GenTableColumnService { + @Override + public IPage listTable(Page page, GenConfig genConfig) { + IPage columnPage = baseMapper.selectTableColumn(page, genConfig.getTableName(), genConfig.getDsName()); + + // 处理 数据库类型和 Java 类型关系 + Configuration config = GenUtils.getConfig(); + columnPage.getRecords().forEach(column -> { + String attrType = config.getString(column.getDataType(), "unknowType"); + column.setLowerAttrName(StringUtils.uncapitalize(GenUtils.columnToJava(column.getColumnName()))); + column.setJavaType(attrType); + }); + return columnPage; + } +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java index ed5e11d9c..1f86c14e9 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java @@ -19,6 +19,7 @@ package com.pig4cloud.pigx.codegen.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; @@ -28,6 +29,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.pig4cloud.pigx.codegen.entity.GenConfig; import com.pig4cloud.pigx.codegen.entity.GenFormConf; import com.pig4cloud.pigx.codegen.mapper.GenFormConfMapper; +import com.pig4cloud.pigx.codegen.mapper.GenTableColumnMapper; import com.pig4cloud.pigx.codegen.mapper.GeneratorMapper; import com.pig4cloud.pigx.codegen.service.GeneratorService; import com.pig4cloud.pigx.codegen.util.GenUtils; @@ -48,6 +50,7 @@ import java.util.zip.ZipOutputStream; @Service @AllArgsConstructor public class GeneratorServiceImpl implements GeneratorService { + private final GenTableColumnMapper tableColumnMapper; private final GeneratorMapper generatorMapper; private final GenFormConfMapper genFormConfMapper; @@ -64,6 +67,31 @@ public class GeneratorServiceImpl implements GeneratorService { return generatorMapper.queryList(page, tableName); } + @Override + public Map previewCode(GenConfig genConfig) { + //根据tableName 查询最新的表单配置 + List formConfList = genFormConfMapper.selectList(Wrappers.lambdaQuery() + .eq(GenFormConf::getTableName, genConfig.getTableName()) + .orderByDesc(GenFormConf::getCreateTime)); + + DynamicDataSourceContextHolder.push(genConfig.getDsName()); + + String tableNames = genConfig.getTableName(); + for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) { + //查询表信息 + Map table = generatorMapper.queryTable(tableName, genConfig.getDsName()); + //查询列信息 + List> columns = tableColumnMapper.selectMapTableColumn(tableName, genConfig.getDsName()); + //生成代码 + if (CollUtil.isNotEmpty(formConfList)) { + return GenUtils.generatorCode(genConfig, table, columns, null, formConfList.get(0)); + } else { + return GenUtils.generatorCode(genConfig, table, columns, null, null); + } + } + return MapUtil.empty(); + } + /** * 生成代码 * @@ -84,9 +112,9 @@ public class GeneratorServiceImpl implements GeneratorService { String tableNames = genConfig.getTableName(); for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) { //查询表信息 - Map table = generatorMapper.queryTable(tableName,genConfig.getDsName()); + Map table = generatorMapper.queryTable(tableName, genConfig.getDsName()); //查询列信息 - List> columns = generatorMapper.queryColumns(tableName, genConfig.getDsName()); + List> columns = tableColumnMapper.selectMapTableColumn(tableName, genConfig.getDsName()); //生成代码 if (CollUtil.isNotEmpty(formConfList)) { GenUtils.generatorCode(genConfig, table, columns, zip, formConfList.get(0)); diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenUtils.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenUtils.java index f1dae2987..25efb0120 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenUtils.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenUtils.java @@ -41,6 +41,7 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import java.io.File; +import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.*; @@ -100,10 +101,12 @@ public class GenUtils { /** * 生成代码 + * + * @return */ @SneakyThrows - public void generatorCode(GenConfig genConfig, Map table, - List> columns, ZipOutputStream zip, GenFormConf formConf) { + public Map generatorCode(GenConfig genConfig, Map table, + List> columns, ZipOutputStream zip, GenFormConf formConf) { //配置信息 Configuration config = getConfig(); boolean hasBigDecimal = false; @@ -171,10 +174,7 @@ public class GenUtils { tableEntity.setPk(tableEntity.getColumns().get(0)); } - //设置velocity资源加载器 - Properties prop = new Properties(); - prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); - Velocity.init(prop); + //封装模板数据 Map map = new HashMap<>(16); map.put("tableName", tableEntity.getTableName()); @@ -211,18 +211,49 @@ public class GenUtils { map.put("package", config.getString("package")); map.put("mainPath", config.getString("mainPath")); } + + // 渲染数据 + return renderData(genConfig, zip, formConf, tableEntity, map); + } + + /** + * 渲染数据 + * + * @param genConfig 配置信息 + * @param zip 流 (为空,直接返回Map) + * @param formConf 表单信息 + * @param tableEntity 表基本信息 + * @param map 模板参数 + * @return map key-filename value-contents + * @throws IOException + */ + private Map renderData(GenConfig genConfig, ZipOutputStream zip, GenFormConf formConf, TableEntity tableEntity, Map map) throws IOException { + //设置velocity资源加载器 + Properties prop = new Properties(); + prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); + Velocity.init(prop); VelocityContext context = new VelocityContext(map); //获取模板列表 List templates = getTemplates(genConfig); + Map resultMap = new HashMap<>(8); + for (String template : templates) { // 如果是crud if (template.contains(AVUE_CRUD_JS_VM) && formConf != null) { - zip.putNextEntry(new ZipEntry(Objects - .requireNonNull(getFileName(template, tableEntity.getCaseClassName() - , map.get("package").toString(), map.get("moduleName").toString())))); - IoUtil.write(zip, StandardCharsets.UTF_8, false, CRUD_PREFIX + formConf.getFormInfo()); - zip.closeEntry(); + + String fileName = getFileName(template, tableEntity.getCaseClassName() + , map.get("package").toString(), map.get("moduleName").toString()); + String contents = CRUD_PREFIX + formConf.getFormInfo(); + + if (zip != null) { + zip.putNextEntry(new ZipEntry(Objects + .requireNonNull(fileName))); + IoUtil.write(zip, StandardCharsets.UTF_8, false, contents); + zip.closeEntry(); + } + + resultMap.put(template, contents); continue; } @@ -233,13 +264,20 @@ public class GenUtils { tpl.merge(context, sw); //添加到zip - zip.putNextEntry(new ZipEntry(Objects - .requireNonNull(getFileName(template, tableEntity.getCaseClassName() - , map.get("package").toString(), map.get("moduleName").toString())))); - IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString()); - IoUtil.close(sw); - zip.closeEntry(); + String fileName = getFileName(template, tableEntity.getCaseClassName() + , map.get("package").toString(), map.get("moduleName").toString()); + + if (zip != null) { + zip.putNextEntry(new ZipEntry(Objects + .requireNonNull(fileName))); + IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString()); + IoUtil.close(sw); + zip.closeEntry(); + } + resultMap.put(template, sw.toString()); } + + return resultMap; } @@ -263,7 +301,7 @@ public class GenUtils { /** * 获取配置信息 */ - private Configuration getConfig() { + public Configuration getConfig() { try { return new PropertiesConfiguration("generator.properties"); } catch (ConfigurationException e) { diff --git a/pigx-visual/pigx-codegen/src/main/resources/mapper/GenTableColumnMapper.xml b/pigx-visual/pigx-codegen/src/main/resources/mapper/GenTableColumnMapper.xml new file mode 100755 index 000000000..0ad2f47b1 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/resources/mapper/GenTableColumnMapper.xml @@ -0,0 +1,47 @@ + + + + + + + + + + SELECT + column_name columnName, + data_type dataType, + column_comment comments, + column_key columnKey, + extra, + is_nullable AS isNullable, + column_type AS columnType + FROM + information_schema.COLUMNS + WHERE + table_name = #{tableName} and table_schema = (select database()) order by ordinal_position + + + + + + + diff --git a/pigx-visual/pigx-codegen/src/main/resources/mapper/GeneratorMapper.xml b/pigx-visual/pigx-codegen/src/main/resources/mapper/GeneratorMapper.xml index 372e0931a..50905ea51 100755 --- a/pigx-visual/pigx-codegen/src/main/resources/mapper/GeneratorMapper.xml +++ b/pigx-visual/pigx-codegen/src/main/resources/mapper/GeneratorMapper.xml @@ -16,8 +16,4 @@ where table_schema = (select database()) and table_name = #{tableName} -