mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-03-13 20:53:42 +08:00
feat: 更新代码生成功能-添加数据模型批量添加字段数据
This commit is contained in:
@@ -11,9 +11,12 @@ import org.ruoyi.enums.DataBaseType;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数据库助手
|
||||
@@ -102,4 +105,59 @@ public class DataBaseHelper {
|
||||
}
|
||||
return tableNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定表的字段信息
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 字段信息列表
|
||||
*/
|
||||
public static List<Map<String, Object>> getTableColumnInfo(String tableName) {
|
||||
DataSource dataSource = DS.determineDataSource();
|
||||
List<Map<String, Object>> columns = new ArrayList<>();
|
||||
|
||||
try (Connection conn = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
String catalog = conn.getCatalog();
|
||||
String schema = conn.getSchema();
|
||||
|
||||
// 获取表字段信息
|
||||
try (ResultSet resultSet = metaData.getColumns(catalog, schema, tableName, "%")) {
|
||||
while (resultSet.next()) {
|
||||
Map<String, Object> column = new HashMap<>();
|
||||
column.put("columnName", resultSet.getString("COLUMN_NAME"));
|
||||
column.put("columnComment", resultSet.getString("REMARKS"));
|
||||
column.put("dataType", resultSet.getString("TYPE_NAME"));
|
||||
column.put("columnSize", resultSet.getInt("COLUMN_SIZE"));
|
||||
column.put("isNullable", "YES".equals(resultSet.getString("IS_NULLABLE")));
|
||||
column.put("ordinalPosition", resultSet.getInt("ORDINAL_POSITION"));
|
||||
|
||||
// 设置默认值
|
||||
String defaultValue = resultSet.getString("COLUMN_DEF");
|
||||
column.put("columnDefault", defaultValue);
|
||||
|
||||
columns.add(column);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取主键信息
|
||||
try (ResultSet pkResultSet = metaData.getPrimaryKeys(catalog, schema, tableName)) {
|
||||
List<String> primaryKeys = new ArrayList<>();
|
||||
while (pkResultSet.next()) {
|
||||
primaryKeys.add(pkResultSet.getString("COLUMN_NAME"));
|
||||
}
|
||||
|
||||
// 标记主键字段
|
||||
for (Map<String, Object> column : columns) {
|
||||
String columnName = (String) column.get("columnName");
|
||||
column.put("isPrimaryKey", primaryKeys.contains(columnName));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new ServiceException("获取表字段信息失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,10 @@ import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.ruoyi.common.core.domain.R;
|
||||
import org.ruoyi.common.log.annotation.Log;
|
||||
import org.ruoyi.common.log.enums.BusinessType;
|
||||
import org.ruoyi.common.web.core.BaseController;
|
||||
import org.ruoyi.generator.service.IGenTableService;
|
||||
import org.ruoyi.helper.DataBaseHelper;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@@ -15,7 +15,7 @@ import org.ruoyi.core.page.PageQuery;
|
||||
import org.ruoyi.core.page.TableDataInfo;
|
||||
import org.ruoyi.generator.domain.bo.SchemaBo;
|
||||
import org.ruoyi.generator.domain.vo.SchemaVo;
|
||||
import org.ruoyi.generator.service.ISchemaService;
|
||||
import org.ruoyi.generator.service.SchemaService;
|
||||
import org.ruoyi.helper.DataBaseHelper;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
@@ -40,7 +40,7 @@ import java.util.List;
|
||||
@RequestMapping("/dev/schema")
|
||||
public class SchemaController extends BaseController {
|
||||
|
||||
private final ISchemaService schemaService;
|
||||
private final SchemaService schemaService;
|
||||
|
||||
/**
|
||||
* 查询数据模型列表
|
||||
|
||||
@@ -15,7 +15,7 @@ import org.ruoyi.core.page.PageQuery;
|
||||
import org.ruoyi.core.page.TableDataInfo;
|
||||
import org.ruoyi.generator.domain.bo.SchemaFieldBo;
|
||||
import org.ruoyi.generator.domain.vo.SchemaFieldVo;
|
||||
import org.ruoyi.generator.service.ISchemaFieldService;
|
||||
import org.ruoyi.generator.service.SchemaFieldService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -32,14 +32,15 @@ import java.util.List;
|
||||
* 数据模型字段
|
||||
*
|
||||
* @author ruoyi
|
||||
*/@Validated
|
||||
*/
|
||||
@Validated
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/dev/schemaField")
|
||||
public class SchemaFieldController extends BaseController {
|
||||
|
||||
private final ISchemaFieldService schemaFieldService;
|
||||
|
||||
private final SchemaFieldService schemaFieldService;
|
||||
|
||||
/**
|
||||
* 查询数据模型字段列表
|
||||
*/
|
||||
@@ -116,8 +117,7 @@ public class SchemaFieldController extends BaseController {
|
||||
*/
|
||||
@SaCheckPermission("dev:schemaField:list")
|
||||
@GetMapping("/listBySchemaId/{schemaId}")
|
||||
public R<List<SchemaFieldVo>> listBySchemaId(@NotNull(message = "模型ID不能为空")
|
||||
@PathVariable Long schemaId) {
|
||||
public R<List<SchemaFieldVo>> listBySchemaId(@NotNull(message = "模型ID不能为空") @PathVariable Long schemaId) {
|
||||
return R.ok(schemaFieldService.queryListBySchemaId(schemaId));
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import org.ruoyi.core.page.PageQuery;
|
||||
import org.ruoyi.core.page.TableDataInfo;
|
||||
import org.ruoyi.generator.domain.bo.SchemaGroupBo;
|
||||
import org.ruoyi.generator.domain.vo.SchemaGroupVo;
|
||||
import org.ruoyi.generator.service.ISchemaGroupService;
|
||||
import org.ruoyi.generator.service.SchemaGroupService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -39,7 +39,7 @@ import java.util.List;
|
||||
@RequestMapping("/dev/schemaGroup")
|
||||
public class SchemaGroupController extends BaseController {
|
||||
|
||||
private final ISchemaGroupService schemaGroupService;
|
||||
private final SchemaGroupService schemaGroupService;
|
||||
|
||||
/**
|
||||
* 查询数据模型分组列表
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.ruoyi.generator.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* 数据模型添加事件
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Getter
|
||||
public class SchemaAddedEvent extends ApplicationEvent {
|
||||
|
||||
private final Long schemaId;
|
||||
private final String tableName;
|
||||
|
||||
public SchemaAddedEvent(Object source, Long schemaId, String tableName) {
|
||||
super(source);
|
||||
this.schemaId = schemaId;
|
||||
this.tableName = tableName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.ruoyi.generator.event;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.generator.service.SchemaFieldService;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 数据模型事件监听器
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class SchemaEventListener {
|
||||
|
||||
private final SchemaFieldService schemaFieldService;
|
||||
|
||||
/**
|
||||
* 监听数据模型添加事件,自动插入字段数据
|
||||
*/
|
||||
@Async
|
||||
@EventListener
|
||||
public void handleSchemaAddedEvent(SchemaAddedEvent event) {
|
||||
try {
|
||||
Long schemaId = event.getSchemaId();
|
||||
String tableName = event.getTableName();
|
||||
log.info("开始为数据模型 {} 自动插入字段数据,表名: {}", schemaId, tableName);
|
||||
boolean success = schemaFieldService.batchInsertFieldsByTableName(schemaId, tableName);
|
||||
if (success) {
|
||||
log.info("数据模型 {} 字段数据插入成功", schemaId);
|
||||
} else {
|
||||
log.warn("数据模型 {} 字段数据插入失败", schemaId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("自动插入字段数据失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import java.util.List;
|
||||
* @author ruoyi
|
||||
* @date 2024-01-01
|
||||
*/
|
||||
public interface ISchemaFieldService {
|
||||
public interface SchemaFieldService {
|
||||
|
||||
/**
|
||||
* 查询数据模型字段
|
||||
@@ -56,4 +56,10 @@ public interface ISchemaFieldService {
|
||||
*/
|
||||
Object getMetaDataByTableName(String tableName);
|
||||
|
||||
/**
|
||||
* @param schemaId
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
boolean batchInsertFieldsByTableName(Long schemaId, String tableName);
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import java.util.List;
|
||||
* @author ruoyi
|
||||
* @date 2024-01-01
|
||||
*/
|
||||
public interface ISchemaGroupService {
|
||||
public interface SchemaGroupService {
|
||||
|
||||
/**
|
||||
* 查询数据模型分组
|
||||
@@ -15,7 +15,7 @@ import java.util.List;
|
||||
* @author ruoyi
|
||||
* @date 2024-01-01
|
||||
*/
|
||||
public interface ISchemaService {
|
||||
public interface SchemaService {
|
||||
|
||||
/**
|
||||
* 查询数据模型
|
||||
@@ -16,9 +16,10 @@ import org.ruoyi.generator.domain.vo.SchemaFieldVo;
|
||||
import org.ruoyi.generator.domain.vo.SchemaGroupVo;
|
||||
import org.ruoyi.generator.domain.vo.SchemaVo;
|
||||
import org.ruoyi.generator.mapper.SchemaFieldMapper;
|
||||
import org.ruoyi.generator.service.ISchemaFieldService;
|
||||
import org.ruoyi.generator.service.ISchemaGroupService;
|
||||
import org.ruoyi.generator.service.ISchemaService;
|
||||
import org.ruoyi.generator.service.SchemaFieldService;
|
||||
import org.ruoyi.generator.service.SchemaGroupService;
|
||||
import org.ruoyi.generator.service.SchemaService;
|
||||
import org.ruoyi.helper.DataBaseHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -35,11 +36,11 @@ import java.util.Objects;
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class SchemaFieldServiceImpl implements ISchemaFieldService {
|
||||
public class SchemaFieldServiceImpl implements SchemaFieldService {
|
||||
|
||||
private final SchemaFieldMapper baseMapper;
|
||||
private final ISchemaService schemaService;
|
||||
private final ISchemaGroupService schemaGroupService;
|
||||
private final SchemaService schemaService;
|
||||
private final SchemaGroupService schemaGroupService;
|
||||
|
||||
/**
|
||||
* 查询数据模型字段
|
||||
@@ -222,6 +223,75 @@ public class SchemaFieldServiceImpl implements ISchemaFieldService {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean batchInsertFieldsByTableName(Long schemaId, String tableName) {
|
||||
try {
|
||||
// 获取表的字段信息
|
||||
List<Map<String, Object>> columnInfos = DataBaseHelper.getTableColumnInfo(tableName);
|
||||
if (CollUtil.isEmpty(columnInfos)) {
|
||||
return false;
|
||||
}
|
||||
// 检查是否已存在字段数据
|
||||
List<SchemaFieldVo> existingFields = queryListBySchemaId(schemaId);
|
||||
if (CollUtil.isNotEmpty(existingFields)) {
|
||||
// 如果已存在字段,则不重复插入
|
||||
return true;
|
||||
}
|
||||
// 转换为 SchemaField 对象并批量插入
|
||||
List<SchemaField> fieldsToInsert = new ArrayList<>();
|
||||
int sort = 1;
|
||||
for (Map<String, Object> columnInfo : columnInfos) {
|
||||
SchemaField field = new SchemaField();
|
||||
field.setSchemaId(schemaId);
|
||||
field.setName((String) columnInfo.get("columnComment"));
|
||||
field.setCode((String) columnInfo.get("columnName"));
|
||||
field.setType((String) columnInfo.get("dataType"));
|
||||
field.setLength(Integer.valueOf(String.valueOf(columnInfo.get("columnSize"))));
|
||||
field.setIsPk((Boolean) columnInfo.get("isPrimaryKey") ? "1" : "0");
|
||||
field.setIsRequired(!(Boolean) columnInfo.get("isNullable") ? "1" : "0");
|
||||
field.setIsInsert("1");
|
||||
field.setIsEdit("1");
|
||||
field.setIsList("1");
|
||||
field.setIsQuery("1");
|
||||
field.setQueryType("EQ");
|
||||
field.setHtmlType(getDefaultHtmlType((String) columnInfo.get("dataType")));
|
||||
field.setSort(sort++);
|
||||
field.setStatus("0");
|
||||
// 如果字段名为空,使用字段代码作为名称
|
||||
if (StringUtils.isBlank(field.getName())) {
|
||||
field.setName(field.getCode());
|
||||
}
|
||||
fieldsToInsert.add(field);
|
||||
}
|
||||
// 批量插入
|
||||
fieldsToInsert.forEach(baseMapper::insert);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据库类型获取默认的HTML类型
|
||||
*/
|
||||
private String getDefaultHtmlType(String dbType) {
|
||||
if (StringUtils.isBlank(dbType)) {
|
||||
return "input";
|
||||
}
|
||||
|
||||
String type = dbType.toLowerCase();
|
||||
if (type.contains("text") || type.contains("longtext")) {
|
||||
return "textarea";
|
||||
} else if (type.contains("date") || type.contains("time")) {
|
||||
return "datetime";
|
||||
} else if (type.contains("bit") || type.contains("boolean")) {
|
||||
return "radio";
|
||||
} else {
|
||||
return "input";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为驼峰命名
|
||||
*/
|
||||
@@ -286,4 +356,6 @@ public class SchemaFieldServiceImpl implements ISchemaFieldService {
|
||||
default -> "input";
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import org.ruoyi.generator.domain.SchemaGroup;
|
||||
import org.ruoyi.generator.domain.bo.SchemaGroupBo;
|
||||
import org.ruoyi.generator.domain.vo.SchemaGroupVo;
|
||||
import org.ruoyi.generator.mapper.SchemaGroupMapper;
|
||||
import org.ruoyi.generator.service.ISchemaGroupService;
|
||||
import org.ruoyi.generator.service.SchemaGroupService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -25,7 +25,7 @@ import java.util.List;
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class SchemaGroupServiceImpl implements ISchemaGroupService {
|
||||
public class SchemaGroupServiceImpl implements SchemaGroupService {
|
||||
|
||||
private final SchemaGroupMapper baseMapper;
|
||||
|
||||
|
||||
@@ -11,8 +11,10 @@ import org.ruoyi.core.page.TableDataInfo;
|
||||
import org.ruoyi.generator.domain.Schema;
|
||||
import org.ruoyi.generator.domain.bo.SchemaBo;
|
||||
import org.ruoyi.generator.domain.vo.SchemaVo;
|
||||
import org.ruoyi.generator.event.SchemaAddedEvent;
|
||||
import org.ruoyi.generator.mapper.SchemaMapper;
|
||||
import org.ruoyi.generator.service.ISchemaService;
|
||||
import org.ruoyi.generator.service.SchemaService;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -25,9 +27,10 @@ import java.util.List;
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class SchemaServiceImpl implements ISchemaService {
|
||||
public class SchemaServiceImpl implements SchemaService {
|
||||
|
||||
private final SchemaMapper baseMapper;
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
|
||||
/**
|
||||
* 查询数据模型
|
||||
@@ -77,6 +80,11 @@ public class SchemaServiceImpl implements ISchemaService {
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
if (flag) {
|
||||
bo.setId(add.getId());
|
||||
|
||||
// 发布数据模型添加事件,由事件监听器处理字段插入
|
||||
if (StringUtils.isNotBlank(bo.getTableName())) {
|
||||
eventPublisher.publishEvent(new SchemaAddedEvent(this, add.getId(), bo.getTableName()));
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user