✨ Introducing new features. 代码生成器支持创建表

This commit is contained in:
lbw committed 2024-02-20 13:42:30 +08:00
1 parent d2585f2890
commit 26d4bad0ff
12 files changed
+707 -60

No files matched your search

+17
View File
@@ -18,6 +18,7 @@
<magic.version>2.0.2</magic.version>
<screw.version>0.0.3</screw.version>
<db2.version>11.5.0.0</db2.version>
<anyline.version>8.7.1-jdk17-20232101</anyline.version>
<configuration.version>1.10</configuration.version>
</properties>
@@ -87,6 +88,22 @@
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
</dependency>
<!--anyline-->
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-data-jdbc-mysql</artifactId>
<version>${anyline.version}</version>
</dependency>
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-data-jdbc-postgresql</artifactId>
<version>${anyline.version}</version>
</dependency>
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-data-jdbc-dm</artifactId>
<version>${anyline.version}</version>
</dependency>
<!--common-->
<dependency>
<groupId>com.pig4cloud</groupId>
@@ -0,0 +1,141 @@
package com.pig4cloud.pigx.codegen.controller;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.codegen.entity.GenCreateTable;
import com.pig4cloud.pigx.codegen.service.GenCreateTableService;
import com.pig4cloud.pigx.codegen.util.vo.GenCreateTableVO;
import com.pig4cloud.pigx.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 自动创建表管理
*
* @author luolin
* @date 2022-09-23 21:56:11
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/create-table")
@Tag(description = "create-table", name = "自动创建表管理管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GenCreateTableController {
private final GenCreateTableService createTableService;
/**
* 分页查询
* @param page 分页对象
* @param createTable 自动创建表管理
* @return
*/
@Operation(summary = "分页查询", description = "分页查询")
@GetMapping("/page")
public R getPage(Page page, @ModelAttribute GenCreateTable createTable,
@RequestParam(required = false) String[] queryTime) {
QueryWrapper<GenCreateTable> query = Wrappers.query(createTable);
return R.ok(createTableService.page(page, query));
}
/**
* 通过id查询自动创建表管理
* @param id id
* @return R
*/
@Operation(summary = "通过id查询", description = "通过id查询")
@GetMapping("/{id}")
public R getById(@PathVariable("id") Long id) {
return R.ok(createTableService.getById(id));
}
/**
* 新增自动创建表管理
* @param createTableVO 自动创建表管理
* @return R
*/
@Operation(summary = "新增自动创建表管理", description = "新增自动创建表管理")
@PostMapping
@PreAuthorize("@pms.hasPermission('codegen_table_add')")
public R save(@RequestBody GenCreateTableVO createTableVO) {
// 切换目标数据源
DynamicDataSourceContextHolder.push(createTableVO.getDsName());
createTableService.createTable(createTableVO);
GenCreateTable createTable = new GenCreateTable();
BeanUtil.copyProperties(createTableVO, createTable);
DynamicDataSourceContextHolder.clear();
return R.ok(createTableService.saveOrUpdate(createTable));
}
/**
* 修改自动创建表管理
* @param createTable 自动创建表管理
* @return R
*/
@Operation(summary = "修改自动创建表管理", description = "修改自动创建表管理")
@PutMapping
@PreAuthorize("@pms.hasPermission('codegen_table_add')")
public R updateById(@RequestBody GenCreateTable createTable) {
return R.ok(createTableService.updateById(createTable));
}
/**
* 通过id删除自动创建表管理
* @param id id
* @return R
*/
@Operation(summary = "通过id删除自动创建表管理", description = "通过id删除自动创建表管理")
@DeleteMapping("/{id}")
@PreAuthorize("@pms.hasPermission('codegen_table_add')")
public R removeById(@PathVariable Long id) {
return R.ok(createTableService.removeById(id));
}
/**
* 查询租户所有自动创建表管理
* @return R
*/
@GetMapping("/list")
public R list() {
return R.ok(createTableService.list());
}
/**
* 通过ids删除
* @param ids ids
* @return R
*/
@Operation(summary = "通过ids删除", description = "通过ids删除")
@DeleteMapping
@PreAuthorize("@pms.hasPermission('codegen_table_add')")
public R removeById(@RequestBody List<Long> ids) {
return R.ok(createTableService.removeByIds(ids));
}
}
@@ -0,0 +1,104 @@
package com.pig4cloud.pigx.codegen.entity;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pigx.common.core.util.TenantTable;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
/**
* 自动创建表管理
*
* @author luolin
* @date 2022-09-23 21:56:11
*/
@Data
@TenantTable
@TableName("gen_create_table")
@EqualsAndHashCode(callSuper = true)
@Schema(description = "自动创建表管理")
public class GenCreateTable extends Model<GenCreateTable> {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@TableId(type = IdType.ASSIGN_ID)
@Schema(description = "主键ID")
private Long id;
/**
* 表名称
*/
@Schema(description = "表名称")
private String tableName;
/**
* 表注释
*/
@Schema(description = "表注释")
private String comments;
/**
* 字段信息
*/
@Schema(description = "字段信息")
private String columnInfo;
/**
* 创建人
*/
@TableField(fill = FieldFill.INSERT)
@Schema(description = "创建人")
private String createBy;
/**
* 修改人
*/
@TableField(fill = FieldFill.UPDATE)
@Schema(description = "修改人")
private String updateBy;
/**
* 创建时间
*/
@Schema(description = "创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 修改时间
*/
@Schema(description = "修改时间")
@TableField(fill = FieldFill.UPDATE)
private LocalDateTime updateTime;
/**
* 删除标识(0-正常,1-删除)
*/
@TableLogic
@TableField(fill = FieldFill.INSERT)
@Schema(description = "删除标记,1:已删除,0:正常")
private String delFlag;
}
@@ -39,73 +39,73 @@ import java.time.LocalDateTime;
@Schema(description = "模板")
public class GenTemplateEntity extends Model<GenTemplateEntity> {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId(type = IdType.ASSIGN_ID)
@Schema(description = "主键")
private Long id;
/**
* 主键
*/
@TableId(type = IdType.ASSIGN_ID)
@Schema(description = "主键")
private Long id;
/**
* 模板名称
*/
@Schema(description = "模板名称")
private String templateName;
/**
* 模板名称
*/
@Schema(description = "模板名称")
private String templateName;
/**
* 模板路径
*/
@Schema(description = "模板路径")
private String generatorPath;
/**
* 模板路径
*/
@Schema(description = "模板路径")
private String generatorPath;
/**
* 模板描述
*/
@Schema(description = "模板描述")
private String templateDesc;
/**
* 模板描述
*/
@Schema(description = "模板描述")
private String templateDesc;
/**
* 模板代码
*/
@Schema(description = "模板代码")
private String templateCode;
/**
* 模板代码
*/
@Schema(description = "模板代码")
private String templateCode;
/**
* 创建人
*/
@TableField(fill = FieldFill.INSERT)
@Schema(description = "创建人")
private String createBy;
/**
* 创建人
*/
@TableField(fill = FieldFill.INSERT)
@Schema(description = "创建人")
private String createBy;
/**
* 修改人
*/
@TableField(fill = FieldFill.UPDATE)
@Schema(description = "修改人")
private String updateBy;
/**
* 修改人
*/
@TableField(fill = FieldFill.UPDATE)
@Schema(description = "修改人")
private String updateBy;
/**
* 创建时间
*/
@Schema(description = "创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 创建时间
*/
@Schema(description = "创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 修改时间
*/
@Schema(description = "修改时间")
@TableField(fill = FieldFill.UPDATE)
private LocalDateTime updateTime;
/**
* 修改时间
*/
@Schema(description = "修改时间")
@TableField(fill = FieldFill.UPDATE)
private LocalDateTime updateTime;
/**
* 删除标识(0-正常,1-删除)
*/
@TableLogic
@TableField(fill = FieldFill.INSERT)
@Schema(description = "删除标记,1:已删除,0:正常")
private String delFlag;
/**
* 删除标识(0-正常,1-删除)
*/
@TableLogic
@TableField(fill = FieldFill.INSERT)
@Schema(description = "删除标记,1:已删除,0:正常")
private String delFlag;
}
@@ -0,0 +1,32 @@
package com.pig4cloud.pigx.codegen.mapper;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
import com.pig4cloud.pigx.codegen.entity.GenCreateTable;
import com.pig4cloud.pigx.common.data.datascope.PigxBaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* 自动创建表管理
*
* @author luolin
* @date 2022-09-23 21:56:11
*/
@Mapper
public interface GenCreateTableMapper extends PigxBaseMapper<GenCreateTable> {
}
@@ -0,0 +1,38 @@
package com.pig4cloud.pigx.codegen.service;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.codegen.entity.GenCreateTable;
import com.pig4cloud.pigx.codegen.util.vo.GenCreateTableVO;
/**
* 自动创建表管理
*
* @author luolin
* @date 2022-09-23 21:56:11
*/
public interface GenCreateTableService extends IService<GenCreateTable> {
/**
* 自动创建表
* @param createTableVO 表信息
*/
Boolean createTable(GenCreateTableVO createTableVO);
}
@@ -0,0 +1,66 @@
package com.pig4cloud.pigx.codegen.service.impl;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.codegen.entity.GenCreateTable;
import com.pig4cloud.pigx.codegen.mapper.GenCreateTableMapper;
import com.pig4cloud.pigx.codegen.service.GenCreateTableService;
import com.pig4cloud.pigx.codegen.util.table.CreateTableHandler;
import com.pig4cloud.pigx.codegen.util.table.model.TableInfo;
import com.pig4cloud.pigx.codegen.util.vo.GenCreateTableVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.anyline.metadata.Column;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.LinkedHashMap;
/**
* 自动创建表管理
*
* @author luolin
* @date 2022-09-23 21:56:11
*/
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CreateTableServiceImpl extends ServiceImpl<GenCreateTableMapper, GenCreateTable>
implements GenCreateTableService {
private final CreateTableHandler createTableHandler;
@Override
public Boolean createTable(GenCreateTableVO createTableVO) {
String columnsInfo = createTableVO.getColumnsInfo();
TableInfo tableInfo = new TableInfo();
tableInfo.setName(createTableVO.getTableName());
LinkedHashMap<String, Column> columns = new LinkedHashMap<>();
LinkedHashMap<String, JSONObject> linkedHashMap = JSONUtil.toBean(columnsInfo, LinkedHashMap.class);
linkedHashMap.forEach((key, val) -> {
columns.put(key, JSONUtil.toBean(val, Column.class));
});
tableInfo.setColumns(columns);
tableInfo.setComment(createTableVO.getComments());
return createTableHandler.createTable(tableInfo);
}
}
@@ -309,8 +309,7 @@ public class GeneratorServiceImpl implements GeneratorService {
// 按字段类型分组,使用 Map 存储不同类型的字段列表
Map<Boolean, List<GenTableColumnEntity>> typeMap = table.getFieldList()
.stream()
.collect(Collectors
.partitioningBy(columnEntity -> BooleanUtil.toBoolean(columnEntity.getPrimaryPk().toString())));
.collect(Collectors.partitioningBy(columnEntity -> BooleanUtil.toBoolean(columnEntity.getPrimaryPk())));
// 从分组后的 Map 中获取不同类型的字段列表
List<GenTableColumnEntity> primaryList = typeMap.get(true);
@@ -0,0 +1,71 @@
package com.pig4cloud.pigx.codegen.util.table;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
import com.pig4cloud.pigx.codegen.util.table.enums.TableModelEnum;
import com.pig4cloud.pigx.codegen.util.table.model.TableInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.anyline.metadata.Table;
import org.anyline.proxy.ServiceProxy;
import org.anyline.service.AnylineService;
import org.springframework.stereotype.Component;
/**
* 创建表核心逻辑类
*
* @author luolin
* @date 2022-09-18 21:56:11
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class CreateTableHandler {
/**
* 运行表单主键ID
*/
private final static String RUN_FORM_COLUMN_NAME = "run_form_id";
/**
* 自动创建表
* @param tableInfo 表信息
*/
public Boolean createTable(TableInfo tableInfo) {
if (tableInfo.getColumns() == null || tableInfo.getColumns().isEmpty()) {
throw new RuntimeException("自动创建表[" + tableInfo.getName() + "]中至少需要一个字段");
}
if (!TableModelEnum.CREATE.name().toLowerCase().equals(tableInfo.getModel())) {
throw new RuntimeException("自动创建表[" + tableInfo.getModel() + "]模式的处理未找到,请检查");
}
try {
AnylineService service = ServiceProxy.service();
// 如果已存在,删除重键
Table table = service.metadata().table(tableInfo.getName(), false);
if (null != table)
service.ddl().drop(table);
// 执行建表SQL
service.ddl().create(tableInfo);
log.info("自动创建表处理完成!");
}
catch (Exception e) {
throw new RuntimeException("自动创建表异常", e);
}
return Boolean.TRUE;
}
}
@@ -0,0 +1,40 @@
package com.pig4cloud.pigx.codegen.util.table.enums;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
/**
* 表生成模式
*
* @author luolin
* @date 2022-09-18 21:56:11
*/
public enum TableModelEnum {
/**
* 创建模式: 将删除所有已存在的表,重新创建,会清除所有数据
*/
CREATE,
/**
* 更新模式: 保留已存在的表以及字段,不针对字段或其结构进行修改,不影响数据
*/
UPDATE,
/**
* 默认模式: 不做任何操作
*/
NONE;
}
@@ -0,0 +1,44 @@
package com.pig4cloud.pigx.codegen.util.table.model;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
import com.pig4cloud.pigx.codegen.util.table.enums.TableModelEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.anyline.metadata.Table;
/**
* 表信息
*
* @author luolin
* @date 2022-09-18 21:56:11
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class TableInfo extends Table<TableInfo> {
/**
* 建表模式
*/
private String model = TableModelEnum.CREATE.name().toLowerCase();
/**
* 表CRUD模式
*/
private String crudModel;
}
@@ -0,0 +1,95 @@
package com.pig4cloud.pigx.codegen.util.vo;
/*
* Copyright (c) 2018-2025, luolin 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: luolin (766488893@qq.com)
*/
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 自动创建表管理
*
* @author luolin
* @date 2022-09-23 21:56:11
*/
@Data
@Schema(description = "自动创建表管理")
public class GenCreateTableVO {
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@Schema(description = "主键ID")
private Long id;
/**
* 表名称
*/
@NotBlank(message = "表名称不能为空")
@Schema(description = "表名称")
private String tableName;
/**
* 表注释
*/
@NotBlank(message = "表注释不能为空")
@Schema(description = "表注释")
private String comments;
/**
* 数据源名称
*/
@NotBlank(message = "数据源名称不能为空")
@Schema(description = "数据源名称")
private String dsName;
/**
* 主键策略
*/
@NotBlank(message = "主键策略不能为空")
@Schema(description = "主键策略")
private String pkPolicy;
/**
* 创建人
*/
@Schema(description = "创建人")
private Long createUser;
/**
* 创建时间
*/
@Schema(description = "创建时间")
private LocalDateTime createTime;
/**
* 表字段信息
*/
@Schema(description = "表字段信息")
private String columnsInfo;
/**
* 字段信息
*/
@Schema(description = "字段信息")
private String columnInfo;
}