-
Notifications
You must be signed in to change notification settings - Fork 71
feat: implement model-driven data API with dynamic SQL operations #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9b6ddf3
fix: model data api
msslulu ee7219f
fix: model data api
msslulu 611f627
fix: update model data api RequestMapping
msslulu a659ab8
fix: update model data api service
msslulu c7f5dbf
fix: update model data api
msslulu 925a9dd
fix: update model data api
msslulu 21999e0
fix: update model data api
msslulu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
base/src/main/java/com/tinyengine/it/dynamic/controller/ModelDataController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package com.tinyengine.it.dynamic.controller; | ||
|
|
||
| import com.tinyengine.it.common.base.Result; | ||
| import com.tinyengine.it.common.log.SystemControllerLog; | ||
| import com.tinyengine.it.dynamic.dto.DynamicDelete; | ||
| import com.tinyengine.it.dynamic.dto.DynamicInsert; | ||
| import com.tinyengine.it.dynamic.dto.DynamicQuery; | ||
| import com.tinyengine.it.dynamic.dto.DynamicUpdate; | ||
| import com.tinyengine.it.dynamic.service.DynamicService; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.media.Content; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Validated | ||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/platform-center/api") | ||
| @Tag(name = "模型数据") | ||
| public class ModelDataController { | ||
| @Autowired | ||
| private DynamicService dynamicService; | ||
|
|
||
| /** | ||
| * 模型数据查询 | ||
| * | ||
| * @return 返回值 all | ||
| */ | ||
| @Operation(summary = "模型数据查询", description = "模型数据查询", responses = { | ||
| @ApiResponse(responseCode = "200", description = "返回信息", | ||
| content = @Content(mediaType = "application/json",schema = @Schema(implementation = Map.class))), | ||
| @ApiResponse(responseCode = "400", description = "请求失败") | ||
| }) | ||
| @SystemControllerLog(description = "模型数据查询") | ||
| @PostMapping("/model-data/queryApi") | ||
| public Result<Map<String, Object>> query(@RequestBody @Valid DynamicQuery dto) { | ||
| try { | ||
| return Result.success(dynamicService.queryWithPage(dto)); | ||
| } catch (Exception e) { | ||
| log.error("Query failed for table: {}", dto.getNameEn(), e); | ||
| return Result.failed("Query operation failed"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * 新增模型数据 | ||
| * | ||
| * @return 返回值 map | ||
| */ | ||
| @Operation(summary = "新增模型数据", description = "新增模型数据", responses = { | ||
| @ApiResponse(responseCode = "200", description = "返回信息", | ||
| content = @Content(mediaType = "application/json",schema = @Schema(implementation = Map.class))), | ||
| @ApiResponse(responseCode = "400", description = "请求失败") | ||
| }) | ||
| @SystemControllerLog(description = "新增模型数据") | ||
| @PostMapping("/model-data/insertApi") | ||
| public Result<Map<String, Object> > insert(@RequestBody @Valid DynamicInsert dto) { | ||
| try { | ||
| return Result.success(dynamicService.insert(dto)); | ||
| } catch (Exception e) { | ||
| log.error("insert failed for table: {}", dto.getNameEn(), e); | ||
| return Result.failed("insert operation failed"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * 更新模型数据 | ||
| * | ||
| * @return 返回值 map | ||
| */ | ||
| @Operation(summary = "更新模型数据", description = "更新模型数据", responses = { | ||
| @ApiResponse(responseCode = "200", description = "返回信息", | ||
| content = @Content(mediaType = "application/json",schema = @Schema(implementation = Map.class))), | ||
| @ApiResponse(responseCode = "400", description = "请求失败") | ||
| }) | ||
| @SystemControllerLog(description = "更新模型数据") | ||
| @PostMapping("/model-data/updateApi") | ||
| public Result<Map<String, Object> > update(@RequestBody @Valid DynamicUpdate dto) { | ||
| try { | ||
| return Result.success(dynamicService.update(dto)); | ||
| } catch (Exception e) { | ||
| log.error("updateApi failed for table: {}", dto.getNameEn(), e); | ||
| return Result.failed("update operation failed"); | ||
| } | ||
|
|
||
| } | ||
| /** | ||
| * 刪除模型数据 | ||
| * | ||
| * @return 返回值 map | ||
| */ | ||
| @Operation(summary = "刪除模型数据", description = "刪除模型数据", responses = { | ||
| @ApiResponse(responseCode = "200", description = "返回信息", | ||
| content = @Content(mediaType = "application/json",schema = @Schema(implementation = Map.class))), | ||
| @ApiResponse(responseCode = "400", description = "请求失败") | ||
| }) | ||
| @SystemControllerLog(description = "刪除模型数据") | ||
| @PostMapping("/model-data/deleteApi") | ||
| public Result<Map<String, Object> > delete(@RequestBody @Valid DynamicDelete dto) { | ||
| try { | ||
| return Result.success(dynamicService.delete(dto)); | ||
| } catch (Exception e) { | ||
| log.error("deleteApi failed for table: {}", dto.getNameEn(), e); | ||
| return Result.failed("delete operation failed"); | ||
| } | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
base/src/main/java/com/tinyengine/it/dynamic/dao/DynamicSqlProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package com.tinyengine.it.dynamic.dao; | ||
|
|
||
| import org.apache.ibatis.jdbc.SQL; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class DynamicSqlProvider { | ||
|
|
||
| public String select(Map<String, Object> params) { | ||
| String tableName = (String) params.get("tableName"); | ||
| List<String> fields = (List<String>) params.get("fields"); | ||
| Map<String, Object> conditions = (Map<String, Object>) params.get("conditions"); | ||
| Integer pageNum = (Integer) params.get("pageNum"); | ||
| Integer pageSize = (Integer) params.get("pageSize"); | ||
| String orderBy = (String) params.get("orderBy"); | ||
| String orderType = (String) params.get("orderType"); | ||
|
|
||
| SQL sql = new SQL(); | ||
|
|
||
| // 选择字段 | ||
| if (fields != null && !fields.isEmpty()) { | ||
| for (String field : fields) { | ||
| sql.SELECT(field); | ||
| } | ||
| } else { | ||
| sql.SELECT("*"); | ||
| } | ||
|
|
||
| sql.FROM(tableName); | ||
|
|
||
| // 条件 | ||
| if (conditions != null && !conditions.isEmpty()) { | ||
| for (Map.Entry<String, Object> entry : conditions.entrySet()) { | ||
| if (entry.getValue() != null) { | ||
| sql.WHERE(entry.getKey() + " = #{conditions." + entry.getKey() + "}"); | ||
| } | ||
| } | ||
| } | ||
| // 排序 | ||
| if (orderBy != null && !orderBy.isEmpty()) { | ||
| sql.ORDER_BY(orderBy + " " + orderType); | ||
| } | ||
|
|
||
| // 分页 | ||
| if (pageNum != null && pageSize != null) { | ||
| return sql.toString() + " LIMIT " + (pageNum - 1) * pageSize + ", " + pageSize; | ||
| } | ||
|
|
||
| return sql.toString(); | ||
| } | ||
msslulu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public String insert(Map<String, Object> params) { | ||
| String tableName = (String) params.get("tableName"); | ||
| Map<String, Object> data = (Map<String, Object>) params.get("data"); | ||
|
|
||
| SQL sql = new SQL(); | ||
| sql.INSERT_INTO(tableName); | ||
|
|
||
| if (data != null && !data.isEmpty()) { | ||
| for (Map.Entry<String, Object> entry : data.entrySet()) { | ||
| sql.VALUES(entry.getKey(), "#{data." + entry.getKey() + "}"); | ||
| } | ||
| } | ||
|
|
||
| return sql.toString(); | ||
| } | ||
|
|
||
| public String update(Map<String, Object> params) { | ||
| String tableName = (String) params.get("tableName"); | ||
| Map<String, Object> data = (Map<String, Object>) params.get("data"); | ||
| Map<String, Object> conditions = (Map<String, Object>) params.get("conditions"); | ||
|
|
||
| SQL sql = new SQL(); | ||
| sql.UPDATE(tableName); | ||
|
|
||
| if (data != null && !data.isEmpty()) { | ||
| for (Map.Entry<String, Object> entry : data.entrySet()) { | ||
| sql.SET(entry.getKey() + " = #{data." + entry.getKey() + "}"); | ||
| } | ||
| } | ||
|
|
||
| if (conditions != null && !conditions.isEmpty()) { | ||
| for (Map.Entry<String, Object> entry : conditions.entrySet()) { | ||
| sql.WHERE(entry.getKey() + " = #{conditions." + entry.getKey() + "}"); | ||
| } | ||
| } | ||
|
|
||
| return sql.toString(); | ||
| } | ||
msslulu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public String delete(Map<String, Object> params) { | ||
| String tableName = (String) params.get("tableName"); | ||
| Map<String, Object> conditions = (Map<String, Object>) params.get("conditions"); | ||
|
|
||
| SQL sql = new SQL(); | ||
| sql.DELETE_FROM(tableName); | ||
|
|
||
| if (conditions != null && !conditions.isEmpty()) { | ||
| for (Map.Entry<String, Object> entry : conditions.entrySet()) { | ||
| sql.WHERE(entry.getKey() + " = #{conditions." + entry.getKey() + "}"); | ||
| } | ||
| } | ||
|
|
||
| return sql.toString(); | ||
| } | ||
msslulu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
32 changes: 32 additions & 0 deletions
32
base/src/main/java/com/tinyengine/it/dynamic/dao/ModelDataDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.tinyengine.it.dynamic.dao; | ||
|
|
||
| import com.alibaba.fastjson.JSONObject; | ||
| import org.apache.ibatis.annotations.*; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Repository | ||
| @Mapper | ||
| public interface ModelDataDao { | ||
|
|
||
| @SelectProvider(type = DynamicSqlProvider.class, method = "select") | ||
| List<JSONObject> select(Map<String, Object> params); | ||
|
|
||
| @InsertProvider(type = DynamicSqlProvider.class, method = "insert") | ||
| @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") | ||
| Long insert(Map<String, Object> params); | ||
msslulu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @UpdateProvider(type = DynamicSqlProvider.class, method = "update") | ||
| Integer update(Map<String, Object> params); | ||
|
|
||
| @DeleteProvider(type = DynamicSqlProvider.class, method = "delete") | ||
| Integer delete(Map<String, Object> params); | ||
|
|
||
| @Select("SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT " + | ||
| "FROM INFORMATION_SCHEMA.COLUMNS " + | ||
| "WHERE TABLE_NAME = #{tableName} AND TABLE_SCHEMA = DATABASE()") | ||
| List<Map<String, Object>> getTableStructure(String tableName); | ||
| } | ||
|
|
||
11 changes: 11 additions & 0 deletions
11
base/src/main/java/com/tinyengine/it/dynamic/dto/DynamicDelete.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.tinyengine.it.dynamic.dto; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Data | ||
| public class DynamicDelete { | ||
| private String nameEn; | ||
| private Integer id; | ||
| } |
10 changes: 10 additions & 0 deletions
10
base/src/main/java/com/tinyengine/it/dynamic/dto/DynamicInsert.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.tinyengine.it.dynamic.dto; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| import java.util.Map; | ||
| @Data | ||
| public class DynamicInsert { | ||
| private String nameEn; | ||
| private Map<String, Object> params; // 插入/更新数据 | ||
| } |
19 changes: 19 additions & 0 deletions
19
base/src/main/java/com/tinyengine/it/dynamic/dto/DynamicQuery.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.tinyengine.it.dynamic.dto; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Data | ||
| public class DynamicQuery { | ||
|
|
||
| private String nameEn; // 表名 | ||
| private String nameCh; // 表中文名 | ||
| private List<String> fields; // 查询字段 | ||
| private Map<String, Object> params; // 查询条件 | ||
| private Integer currentPage = 1; // 页码 | ||
| private Integer pageSize = 10; // 每页大小 | ||
| private String orderBy; // 排序字段 | ||
| private String orderType = "ASC"; // 排序方式 | ||
| } |
11 changes: 11 additions & 0 deletions
11
base/src/main/java/com/tinyengine/it/dynamic/dto/DynamicUpdate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.tinyengine.it.dynamic.dto; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| import java.util.Map; | ||
| @Data | ||
| public class DynamicUpdate { | ||
| private String nameEn; | ||
| private Map<String, Object> data; | ||
| private Map<String, Object> params;// 查询条件 | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.