|
@@ -3,6 +3,7 @@ package com.xjrsoft.xjrsoftboot;
|
|
|
import cn.dev33.satoken.secure.SaSecureUtil;
|
|
import cn.dev33.satoken.secure.SaSecureUtil;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import cn.hutool.db.ds.simple.SimpleDataSource;
|
|
|
import cn.hutool.db.meta.Column;
|
|
import cn.hutool.db.meta.Column;
|
|
|
import cn.hutool.db.meta.MetaUtil;
|
|
import cn.hutool.db.meta.MetaUtil;
|
|
|
import cn.hutool.db.meta.Table;
|
|
import cn.hutool.db.meta.Table;
|
|
@@ -18,6 +19,7 @@ import com.xjrsoft.module.generator.entity.FieldConfig;
|
|
|
import com.xjrsoft.module.generator.entity.GeneratorConfig;
|
|
import com.xjrsoft.module.generator.entity.GeneratorConfig;
|
|
|
import com.xjrsoft.module.generator.entity.TableConfig;
|
|
import com.xjrsoft.module.generator.entity.TableConfig;
|
|
|
import com.xjrsoft.module.generator.service.IApiGeneratorService;
|
|
import com.xjrsoft.module.generator.service.IApiGeneratorService;
|
|
|
|
|
+import com.xjrsoft.module.generator.service.impl.ApiGeneratorServiceImpl;
|
|
|
import freemarker.template.Template;
|
|
import freemarker.template.Template;
|
|
|
import freemarker.template.TemplateException;
|
|
import freemarker.template.TemplateException;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
@@ -29,25 +31,21 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|
|
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
|
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
|
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
|
|
|
|
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
+import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDate;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
-@SpringBootTest(classes = XjrSoftApplication.class)
|
|
|
|
|
public class FreeMarkerGeneratorTest {
|
|
public class FreeMarkerGeneratorTest {
|
|
|
|
|
|
|
|
- private static final String ENTITY_TEMPLATE_NAME = "entity.java.ftl";
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private FreeMarkerConfigurer freeMarkerConfigurer;
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private IApiGeneratorService apiGeneratorService;
|
|
|
|
|
|
|
+ private static final DataSource ds = new SimpleDataSource("jdbc:mysql://8.142.26.206:3306/tl?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true",
|
|
|
|
|
+ "root" , "Zwr~-f6H,u6QE^]C-AD_" );
|
|
|
|
|
|
|
|
@Test
|
|
@Test
|
|
|
- public void generateCodesTest(){
|
|
|
|
|
|
|
+ public void generateCodesTest() throws IOException {
|
|
|
List<TableConfig> tableConfigs = new ArrayList<>();
|
|
List<TableConfig> tableConfigs = new ArrayList<>();
|
|
|
TableConfig mainTable = new TableConfig();
|
|
TableConfig mainTable = new TableConfig();
|
|
|
mainTable.setTableName("xjr_oa_news");
|
|
mainTable.setTableName("xjr_oa_news");
|
|
@@ -73,143 +71,19 @@ public class FreeMarkerGeneratorTest {
|
|
|
childTable2.setRelationTableField(GlobalConstant.DEFAULT_PK);
|
|
childTable2.setRelationTableField(GlobalConstant.DEFAULT_PK);
|
|
|
|
|
|
|
|
tableConfigs.add(mainTable);
|
|
tableConfigs.add(mainTable);
|
|
|
- tableConfigs.add(childTable1);
|
|
|
|
|
- tableConfigs.add(childTable2);
|
|
|
|
|
|
|
+// tableConfigs.add(childTable1);
|
|
|
|
|
+// tableConfigs.add(childTable2);
|
|
|
|
|
|
|
|
ApiGenerateCodesDto params = new ApiGenerateCodesDto();
|
|
ApiGenerateCodesDto params = new ApiGenerateCodesDto();
|
|
|
params.setAuthor("fanxp");
|
|
params.setAuthor("fanxp");
|
|
|
params.setPackageName("test");
|
|
params.setPackageName("test");
|
|
|
params.setTableConfigs(tableConfigs);
|
|
params.setTableConfigs(tableConfigs);
|
|
|
|
|
+ params.setPage(true);
|
|
|
|
|
+ params.setDs(ds);
|
|
|
|
|
|
|
|
- apiGeneratorService.generateCodes(params);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Test
|
|
|
|
|
- public void freeMarkerTest() throws IOException, TemplateException {
|
|
|
|
|
-
|
|
|
|
|
- List<TableConfig> tableConfigs = new ArrayList<>();
|
|
|
|
|
- TableConfig mainTable = new TableConfig();
|
|
|
|
|
- mainTable.setTableName("xjr_oa_news");
|
|
|
|
|
- mainTable.setIsMain(true);
|
|
|
|
|
- mainTable.setPkField(GlobalConstant.DEFAULT_PK);
|
|
|
|
|
- mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
|
|
|
|
|
-
|
|
|
|
|
- TableConfig childTable1 = new TableConfig();
|
|
|
|
|
- childTable1.setTableName("xjr_oa_news_relation");
|
|
|
|
|
- childTable1.setIsMain(false);
|
|
|
|
|
- childTable1.setPkField(GlobalConstant.DEFAULT_PK);
|
|
|
|
|
- childTable1.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
|
|
|
|
|
- childTable1.setRelationField("news_id");
|
|
|
|
|
- childTable1.setRelationTableField(GlobalConstant.DEFAULT_PK);
|
|
|
|
|
|
|
|
|
|
|
|
+ IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
|
|
|
|
|
|
|
|
- TableConfig childTable2 = new TableConfig();
|
|
|
|
|
- childTable2.setTableName("xjr_oa_news_relation_config");
|
|
|
|
|
- childTable2.setIsMain(false);
|
|
|
|
|
- childTable2.setPkField(GlobalConstant.DEFAULT_PK);
|
|
|
|
|
- childTable2.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
|
|
|
|
|
- childTable2.setRelationField("news_id");
|
|
|
|
|
- childTable2.setRelationTableField(GlobalConstant.DEFAULT_PK);
|
|
|
|
|
-
|
|
|
|
|
- tableConfigs.add(mainTable);
|
|
|
|
|
- tableConfigs.add(childTable1);
|
|
|
|
|
- tableConfigs.add(childTable2);
|
|
|
|
|
-
|
|
|
|
|
- GeneratorConfig generatorConfig = new GeneratorConfig();
|
|
|
|
|
- generatorConfig.setTableConfigs(tableConfigs);
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- List<Table> tableInfos = new ArrayList<>();
|
|
|
|
|
- for (TableConfig tableConfig : generatorConfig.getTableConfigs()) {
|
|
|
|
|
- //判断是否为默认数据源
|
|
|
|
|
- if (StrUtil.equalsIgnoreCase(generatorConfig.getDatabaseId(), GlobalConstant.DEFAULT_DATASOURCE_KEY)) {
|
|
|
|
|
- tableInfos.add(MetaUtil.getTableMeta(DatasourceUtil.getDatasourceMaster(), tableConfig.getTableName()));
|
|
|
|
|
- } else {
|
|
|
|
|
- tableInfos.add(MetaUtil.getTableMeta(DatasourceUtil.getDataSource(generatorConfig.getDatabaseId()), tableConfig.getTableName()));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- System.out.println(tableInfos);
|
|
|
|
|
-
|
|
|
|
|
- Map<String, String> entityCodeMap = new HashMap<>(tableInfos.size());
|
|
|
|
|
- // 遍历数据表配置 根据表明搜索表信息 然后生成代码
|
|
|
|
|
- for (Table tableInfo : tableInfos) {
|
|
|
|
|
- //获取表的所有字段
|
|
|
|
|
- Collection<Column> columns = tableInfo.getColumns();
|
|
|
|
|
- List<FieldConfig> fieldConfigList = new ArrayList<>();
|
|
|
|
|
-
|
|
|
|
|
- for (Column column : columns) {
|
|
|
|
|
- FieldConfig fieldConfig = new FieldConfig();
|
|
|
|
|
-
|
|
|
|
|
- if (GlobalConstant.AUTO_INSERT.contains(column.getName())) {
|
|
|
|
|
- fieldConfig.setAutoInsert(true);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (GlobalConstant.AUTO_UPDATE.contains(column.getName())) {
|
|
|
|
|
- fieldConfig.setAutoUpdate(true);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (StrUtil.equalsIgnoreCase(column.getName(), GlobalConstant.DELETE_MARK)) {
|
|
|
|
|
- fieldConfig.setDeleteMark(true);
|
|
|
|
|
- }
|
|
|
|
|
- fieldConfig.setFieldComment(column.getComment());
|
|
|
|
|
- fieldConfig.setPk(column.isPk());
|
|
|
|
|
- fieldConfig.setFieldType(JdbcToJavaUtil.getClassName(column.getTypeEnum()));
|
|
|
|
|
- fieldConfig.setFieldName(StrUtil.toCamelCase(column.getName()));
|
|
|
|
|
-
|
|
|
|
|
- fieldConfigList.add(fieldConfig);
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- //如果有子表 每多一个子表 多加1个长度
|
|
|
|
|
- Map<String, Object> entityMap = new HashMap<>(8);
|
|
|
|
|
- entityMap.put(EntityConstant.PACKAGE, GlobalConstant.GENERATOR_DEFAULT_PATH + StringPool.DOT + "test" + StringPool.DOT + "entity");
|
|
|
|
|
- entityMap.put(EntityConstant.AUTH_NAME, "fanxp");
|
|
|
|
|
- entityMap.put(EntityConstant.DATE, LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
|
|
|
|
- entityMap.put(EntityConstant.TABLE_NAME, tableInfo.getTableName());
|
|
|
|
|
- entityMap.put(EntityConstant.TABLE_COMMENT, tableInfo.getComment());
|
|
|
|
|
- //先将表明转换为驼峰命名 然后在转换为首字母大写
|
|
|
|
|
- entityMap.put(EntityConstant.ENTITY_CLASS_NAME, StrUtil.upperFirst(StrUtil.toCamelCase(tableInfo.getTableName())));
|
|
|
|
|
- entityMap.put(EntityConstant.TABLE_FIELDS, fieldConfigList);
|
|
|
|
|
-
|
|
|
|
|
- //如果是多表关联 则需要添加关联表的信息 判断数据表配置是否大于1 并且 是主表
|
|
|
|
|
- boolean isMainTable = generatorConfig.getTableConfigs().stream().anyMatch(x -> StrUtil.equals(x.getTableName(), tableInfo.getTableName()) && x.getIsMain());
|
|
|
|
|
- if (tableInfos.size() > 1 && isMainTable) {
|
|
|
|
|
- List<TableConfig> childTables = generatorConfig.getTableConfigs().stream()
|
|
|
|
|
- .filter(x -> !x.getIsMain())
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
-
|
|
|
|
|
- List<TableConfig> newChildTables = new ArrayList<>();
|
|
|
|
|
- for (TableConfig childTable : childTables) {
|
|
|
|
|
- TableConfig newTableConfig = BeanUtil.toBean(childTable, TableConfig.class);
|
|
|
|
|
-
|
|
|
|
|
- newTableConfig.setRelationField(StrUtil.toCamelCase(childTable.getRelationField()));
|
|
|
|
|
- newTableConfig.setRelationTableField(StrUtil.toCamelCase(childTable.getRelationTableField()));
|
|
|
|
|
- newTableConfig.setTableName(StrUtil.toCamelCase(childTable.getTableName()));
|
|
|
|
|
-
|
|
|
|
|
- newChildTables.add(newTableConfig);
|
|
|
|
|
- }
|
|
|
|
|
-// Optional<TableConfig> parentTable = generatorConfig.getTableConfigs().stream().filter(TableConfig::getIsMain).findFirst();
|
|
|
|
|
- entityMap.put(EntityConstant.CHILD_TABLES, newChildTables);
|
|
|
|
|
-// parentTable.ifPresent(x -> entityMap.put(EntityConstant.PARENT_RELATION_FIELD_KEY, x.getRelationTableField()));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- //生产目标代码
|
|
|
|
|
-// Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
|
|
|
|
|
-// configuration.setDefaultEncoding(StandardCharsets.UTF_8.name());
|
|
|
|
|
-// configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
|
|
|
|
|
-// Template template = configuration.getTemplate(ENTITY_TEMPLATE_NAME);
|
|
|
|
|
-
|
|
|
|
|
- Template template = freeMarkerConfigurer.getConfiguration().getTemplate(ENTITY_TEMPLATE_NAME);
|
|
|
|
|
- String entityContent = FreeMarkerTemplateUtils.processTemplateIntoString(template, entityMap);
|
|
|
|
|
-
|
|
|
|
|
- entityCodeMap.put(StrUtil.upperFirst(StrUtil.toCamelCase(tableInfo.getTableName())), entityContent);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- System.out.println(entityCodeMap);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ apiGeneratorService.generateCodes(params);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|