Commit 0d55ca747c9c7ebc1180c847883486a6f1ec1262

Authored by YunaiV
1 parent 63962d55

1. 同步数据表和字段定义

2. 删除数据库表
ruoyi-generator/src/main/java/com/ruoyi/generator/config/GenConfig.java
@@ -14,6 +14,7 @@ import org.springframework.stereotype.Component; @@ -14,6 +14,7 @@ import org.springframework.stereotype.Component;
14 @ConfigurationProperties(prefix = "gen") 14 @ConfigurationProperties(prefix = "gen")
15 @PropertySource(value = { "classpath:generator.yml" }) 15 @PropertySource(value = { "classpath:generator.yml" })
16 public class GenConfig { 16 public class GenConfig {
  17 +
17 /** 作者 */ 18 /** 作者 */
18 public static String author; 19 public static String author;
19 20
@@ -26,47 +27,4 @@ public class GenConfig { @@ -26,47 +27,4 @@ public class GenConfig {
26 /** 表前缀(类名不会包含表前缀) */ 27 /** 表前缀(类名不会包含表前缀) */
27 public static String tablePrefix; 28 public static String tablePrefix;
28 29
29 - public static String getAuthor()  
30 - {  
31 - return author;  
32 - }  
33 -  
34 - @Value("${author}")  
35 - public void setAuthor(String author)  
36 - {  
37 - GenConfig.author = author;  
38 - }  
39 -  
40 - public static String getPackageName()  
41 - {  
42 - return packageName;  
43 - }  
44 -  
45 - @Value("${packageName}")  
46 - public void setPackageName(String packageName)  
47 - {  
48 - GenConfig.packageName = packageName;  
49 - }  
50 -  
51 - public static boolean getAutoRemovePre()  
52 - {  
53 - return autoRemovePre;  
54 - }  
55 -  
56 - @Value("${autoRemovePre}")  
57 - public void setAutoRemovePre(boolean autoRemovePre)  
58 - {  
59 - GenConfig.autoRemovePre = autoRemovePre;  
60 - }  
61 -  
62 - public static String getTablePrefix()  
63 - {  
64 - return tablePrefix;  
65 - }  
66 -  
67 - @Value("${tablePrefix}")  
68 - public void setTablePrefix(String tablePrefix)  
69 - {  
70 - GenConfig.tablePrefix = tablePrefix;  
71 - }  
72 } 30 }
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java deleted
1 -package com.ruoyi.generator.controller;  
2 -  
3 -import java.io.IOException;  
4 -import java.util.HashMap;  
5 -import java.util.List;  
6 -import java.util.Map;  
7 -import javax.servlet.http.HttpServletResponse;  
8 -  
9 -import org.apache.commons.io.IOUtils;  
10 -import org.springframework.beans.factory.annotation.Autowired;  
11 -import org.springframework.security.access.prepost.PreAuthorize;  
12 -import org.springframework.validation.annotation.Validated;  
13 -import org.springframework.web.bind.annotation.DeleteMapping;  
14 -import org.springframework.web.bind.annotation.GetMapping;  
15 -import org.springframework.web.bind.annotation.PathVariable;  
16 -import org.springframework.web.bind.annotation.PostMapping;  
17 -import org.springframework.web.bind.annotation.PutMapping;  
18 -import org.springframework.web.bind.annotation.RequestBody;  
19 -import org.springframework.web.bind.annotation.RequestMapping;  
20 -import org.springframework.web.bind.annotation.RestController;  
21 -import com.ruoyi.common.annotation.Log;  
22 -import com.ruoyi.common.core.controller.BaseController;  
23 -import com.ruoyi.common.core.domain.AjaxResult;  
24 -import com.ruoyi.common.core.page.TableDataInfo;  
25 -import com.ruoyi.common.core.text.Convert;  
26 -import com.ruoyi.common.enums.BusinessType;  
27 -import com.ruoyi.generator.domain.GenTable;  
28 -import com.ruoyi.generator.domain.GenTableColumn;  
29 -import com.ruoyi.generator.service.IGenTableColumnService;  
30 -import com.ruoyi.generator.service.IGenTableService;  
31 -  
32 -/**  
33 - * 代码生成 操作处理  
34 - *  
35 - * @author ruoyi  
36 - */  
37 -@RestController  
38 -@RequestMapping("/tool/gen")  
39 -public class GenController extends BaseController {  
40 -  
41 - /**  
42 - * 查询数据表字段列表  
43 - */  
44 - @PreAuthorize("@ss.hasPermi('tool:gen:list')")  
45 - @GetMapping(value = "/column/{talbleId}")  
46 - public TableDataInfo columnList(Long tableId) {  
47 - TableDataInfo dataInfo = new TableDataInfo();  
48 - List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);  
49 - dataInfo.setRows(list);  
50 - dataInfo.setTotal(list.size());  
51 - return dataInfo;  
52 - }  
53 -  
54 - /**  
55 - * 导入表结构(保存)  
56 - */  
57 - @PreAuthorize("@ss.hasPermi('tool:gen:list')")  
58 - @Log(title = "代码生成", businessType = BusinessType.IMPORT)  
59 - @PostMapping("/importTable")  
60 - public AjaxResult importTableSave(String tables) {  
61 - String[] tableNames = Convert.toStrArray(tables);  
62 - // 查询表信息  
63 - List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);  
64 - genTableService.importGenTable(tableList);  
65 - return AjaxResult.success();  
66 - }  
67 -  
68 - /**  
69 - * 删除代码生成  
70 - */  
71 - @PreAuthorize("@ss.hasPermi('tool:gen:remove')")  
72 - @Log(title = "代码生成", businessType = BusinessType.DELETE)  
73 - @DeleteMapping("/{tableIds}")  
74 - public AjaxResult remove(@PathVariable Long[] tableIds) {  
75 - genTableService.deleteGenTableByIds(tableIds);  
76 - return AjaxResult.success();  
77 - }  
78 -  
79 - /**  
80 - * 生成代码(自定义路径)  
81 - */  
82 - @PreAuthorize("@ss.hasPermi('tool:gen:code')")  
83 - @Log(title = "代码生成", businessType = BusinessType.GENCODE)  
84 - @GetMapping("/genCode/{tableName}")  
85 - public AjaxResult genCode(@PathVariable("tableName") String tableName) {  
86 - genTableService.generatorCode(tableName);  
87 - return AjaxResult.success();  
88 - }  
89 -  
90 - /**  
91 - * 同步数据库  
92 - */  
93 - @PreAuthorize("@ss.hasPermi('tool:gen:edit')")  
94 - @Log(title = "代码生成", businessType = BusinessType.UPDATE)  
95 - @GetMapping("/synchDb/{tableName}")  
96 - public AjaxResult synchDb(@PathVariable("tableName") String tableName) {  
97 - genTableService.synchDb(tableName);  
98 - return AjaxResult.success();  
99 - }  
100 -  
101 - /**  
102 - * 生成zip文件  
103 - */  
104 - private void genCode(HttpServletResponse response, byte[] data) throws IOException {  
105 - response.reset();  
106 - response.addHeader("Access-Control-Allow-Origin", "*");  
107 - response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");  
108 - response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");  
109 - response.addHeader("Content-Length", "" + data.length);  
110 - response.setContentType("application/octet-stream; charset=UTF-8");  
111 - IOUtils.write(data, response.getOutputStream());  
112 - }  
113 -  
114 -}  
ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/GenTableMapper.java deleted
1 -package com.ruoyi.generator.mapper;  
2 -  
3 -import java.util.List;  
4 -  
5 -import com.ruoyi.generator.domain.GenTable;  
6 -  
7 -/**  
8 - * 业务 数据层  
9 - *  
10 - * @author ruoyi  
11 - */  
12 -public interface GenTableMapper {  
13 - /**  
14 - * 查询业务列表  
15 - *  
16 - * @param genTable 业务信息  
17 - * @return 业务集合  
18 - */  
19 - public List<GenTable> selectGenTableList(GenTable genTable);  
20 -  
21 - /**  
22 - * 查询据库列表  
23 - *  
24 - * @param genTable 业务信息  
25 - * @return 数据库表集合  
26 - */  
27 - public List<GenTable> selectDbTableList(GenTable genTable);  
28 -  
29 - /**  
30 - * 查询据库列表  
31 - *  
32 - * @param tableNames 表名称组  
33 - * @return 数据库表集合  
34 - */  
35 - public List<GenTable> selectDbTableListByNames(String[] tableNames);  
36 -  
37 - /**  
38 - * 查询表ID业务信息  
39 - *  
40 - * @param id 业务ID  
41 - * @return 业务信息  
42 - */  
43 - public GenTable selectGenTableById(Long id);  
44 -  
45 - /**  
46 - * 查询表名称业务信息  
47 - *  
48 - * @param tableName 表名称  
49 - * @return 业务信息  
50 - */  
51 - public GenTable selectGenTableByName(String tableName);  
52 -  
53 -}  
ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java deleted
1 -package com.ruoyi.generator.service;  
2 -  
3 -import java.io.ByteArrayOutputStream;  
4 -import java.io.File;  
5 -import java.io.IOException;  
6 -import java.io.StringWriter;  
7 -import java.util.LinkedHashMap;  
8 -import java.util.List;  
9 -import java.util.Map;  
10 -import java.util.stream.Collectors;  
11 -import java.util.zip.ZipEntry;  
12 -import java.util.zip.ZipOutputStream;  
13 -  
14 -import org.apache.commons.io.IOUtils;  
15 -import org.apache.velocity.Template;  
16 -import org.apache.velocity.VelocityContext;  
17 -import org.apache.velocity.app.Velocity;  
18 -import org.slf4j.Logger;  
19 -import org.slf4j.LoggerFactory;  
20 -import org.springframework.beans.factory.annotation.Autowired;  
21 -import org.springframework.stereotype.Service;  
22 -import org.springframework.transaction.annotation.Transactional;  
23 -import com.alibaba.fastjson.JSON;  
24 -import com.alibaba.fastjson.JSONObject;  
25 -import com.ruoyi.common.constant.Constants;  
26 -import com.ruoyi.common.constant.GenConstants;  
27 -import com.ruoyi.common.core.text.CharsetKit;  
28 -import com.ruoyi.common.exception.CustomException;  
29 -import com.ruoyi.common.utils.SecurityUtils;  
30 -import com.ruoyi.common.utils.StringUtils;  
31 -import com.ruoyi.common.utils.file.FileUtils;  
32 -import com.ruoyi.generator.domain.GenTable;  
33 -import com.ruoyi.generator.domain.GenTableColumn;  
34 -import com.ruoyi.generator.mapper.GenTableColumnMapper;  
35 -import com.ruoyi.generator.mapper.GenTableMapper;  
36 -import com.ruoyi.generator.util.GenUtils;  
37 -import com.ruoyi.generator.util.VelocityInitializer;  
38 -import com.ruoyi.generator.util.VelocityUtils;  
39 -  
40 -/**  
41 - * 业务 服务层实现  
42 - *  
43 - * @author ruoyi  
44 - */  
45 -@Service  
46 -public class GenTableServiceImpl implements IGenTableService {  
47 -  
48 - @Autowired  
49 - private GenTableMapper genTableMapper;  
50 -  
51 - @Autowired  
52 - private GenTableColumnMapper genTableColumnMapper;  
53 -  
54 - /**  
55 - * 查询业务信息  
56 - *  
57 - * @param id 业务ID  
58 - * @return 业务信息  
59 - */  
60 - @Override  
61 - public GenTable selectGenTableById(Long id) {  
62 - GenTable genTable = genTableMapper.selectGenTableById(id);  
63 - setTableFromOptions(genTable);  
64 - return genTable;  
65 - }  
66 -  
67 - /**  
68 - * 查询据库列表  
69 - *  
70 - * @param genTable 业务信息  
71 - * @return 数据库表集合  
72 - */  
73 - @Override  
74 - public List<GenTable> selectDbTableList(GenTable genTable) {  
75 - return genTableMapper.selectDbTableList(genTable);  
76 - }  
77 -  
78 - /**  
79 - * 查询据库列表  
80 - *  
81 - * @param tableNames 表名称组  
82 - * @return 数据库表集合  
83 - */  
84 - @Override  
85 - public List<GenTable> selectDbTableListByNames(String[] tableNames) {  
86 - return genTableMapper.selectDbTableListByNames(tableNames);  
87 - }  
88 -  
89 - /**  
90 - * 删除业务对象  
91 - *  
92 - * @param tableIds 需要删除的数据ID  
93 - * @return 结果  
94 - */  
95 - @Override  
96 - @Transactional  
97 - public void deleteGenTableByIds(Long[] tableIds) {  
98 - genTableMapper.deleteGenTableByIds(tableIds);  
99 - genTableColumnMapper.deleteGenTableColumnByIds(tableIds);  
100 - }  
101 -  
102 - /**  
103 - * 导入表结构  
104 - *  
105 - * @param tableList 导入表列表  
106 - */  
107 - @Override  
108 - @Transactional  
109 - public void importGenTable(List<GenTable> tableList) {  
110 - String operName = SecurityUtils.getUsername();  
111 - try {  
112 - for (GenTable table : tableList) {  
113 - String tableName = table.getTableName();  
114 - GenUtils.initTable(table, operName);  
115 - int row = genTableMapper.insertGenTable(table);  
116 - if (row > 0) {  
117 - // 保存列信息  
118 - List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);  
119 - for (GenTableColumn column : genTableColumns) {  
120 - GenUtils.initColumnField(column, table);  
121 - genTableColumnMapper.insertGenTableColumn(column);  
122 - }  
123 - }  
124 - }  
125 - } catch (Exception e) {  
126 - throw new CustomException("导入失败:" + e.getMessage());  
127 - }  
128 - }  
129 -  
130 -  
131 - /**  
132 - * 同步数据库  
133 - *  
134 - * @param tableName 表名称  
135 - */  
136 - @Override  
137 - @Transactional  
138 - public void synchDb(String tableName) {  
139 - GenTable table = genTableMapper.selectGenTableByName(tableName);  
140 - List<GenTableColumn> tableColumns = table.getColumns();  
141 - List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());  
142 -  
143 - List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);  
144 - List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());  
145 -  
146 - dbTableColumns.forEach(column -> {  
147 - if (!tableColumnNames.contains(column.getColumnName())) {  
148 - GenUtils.initColumnField(column, table);  
149 - genTableColumnMapper.insertGenTableColumn(column);  
150 - }  
151 - });  
152 -  
153 - List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());  
154 - if (StringUtils.isNotEmpty(delColumns)) {  
155 - genTableColumnMapper.deleteGenTableColumns(delColumns);  
156 - }  
157 - }  
158 -  
159 - /**  
160 - * 修改保存参数校验  
161 - *  
162 - * @param genTable 业务信息  
163 - */  
164 - @Override  
165 - public void validateEdit(GenTable genTable) {  
166 - if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {  
167 - String options = JSON.toJSONString(genTable.getParams());  
168 - JSONObject paramsObj = JSONObject.parseObject(options);  
169 - if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) {  
170 - throw new CustomException("树编码字段不能为空");  
171 - } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) {  
172 - throw new CustomException("树父编码字段不能为空");  
173 - } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) {  
174 - throw new CustomException("树名称字段不能为空");  
175 - }  
176 - }  
177 - }  
178 -  
179 -}  
ruoyi-generator/src/main/resources/mapper/generator/GenTableMapper.xml deleted
1 -<?xml version="1.0" encoding="UTF-8" ?>  
2 -<!DOCTYPE mapper  
3 -PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
4 -"http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
5 -<mapper namespace="com.ruoyi.generator.mapper.GenTableMapper">  
6 -  
7 - <select id="selectDbTableList" parameterType="GenTable" resultMap="GenTableResult">  
8 - select table_name, table_comment, create_time, update_time from information_schema.tables  
9 - where table_schema = (select database())  
10 - AND table_name NOT LIKE 'qrtz_%' AND table_name NOT LIKE 'gen_%'  
11 - AND table_name NOT IN (select table_name from gen_table)  
12 - <if test="tableName != null and tableName != ''">  
13 - AND lower(table_name) like lower(concat('%', #{tableName}, '%'))  
14 - </if>  
15 - <if test="tableComment != null and tableComment != ''">  
16 - AND lower(table_comment) like lower(concat('%', #{tableComment}, '%'))  
17 - </if>  
18 - <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->  
19 - AND date_format(create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')  
20 - </if>  
21 - <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->  
22 - AND date_format(create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')  
23 - </if>  
24 - </select>  
25 -  
26 - <select id="selectDbTableListByNames" resultMap="GenTableResult">  
27 - select table_name, table_comment, create_time, update_time from information_schema.tables  
28 - where table_name NOT LIKE 'qrtz_%' and table_name NOT LIKE 'gen_%' and table_schema = (select database())  
29 - and table_name in  
30 - <foreach collection="array" item="name" open="(" separator="," close=")">  
31 - #{name}  
32 - </foreach>  
33 - </select>  
34 -  
35 - <select id="selectTableByName" parameterType="String" resultMap="GenTableResult">  
36 - select table_name, table_comment, create_time, update_time from information_schema.tables  
37 - where table_comment <![CDATA[ <> ]]> '' and table_schema = (select database())  
38 - and table_name = #{tableName}  
39 - </select>  
40 -  
41 - <select id="selectGenTableById" parameterType="Long" resultMap="GenTableResult">  
42 - SELECT t.table_id, t.table_name, t.table_comment, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.gen_type, t.gen_path, t.options, t.remark,  
43 - c.column_id, c.column_name, c.column_comment, c.column_type, c.java_type, c.java_field, c.is_pk, c.is_increment, c.is_required, c.is_insert, c.is_edit, c.is_list, c.is_query, c.query_type, c.html_type, c.dict_type, c.sort  
44 - FROM gen_table t  
45 - LEFT JOIN gen_table_column c ON t.table_id = c.table_id  
46 - where t.table_id = #{tableId} order by c.sort  
47 - </select>  
48 -  
49 - <select id="selectGenTableByName" parameterType="String" resultMap="GenTableResult">  
50 - SELECT t.table_id, t.table_name, t.table_comment, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.gen_type, t.gen_path, t.options, t.remark,  
51 - c.column_id, c.column_name, c.column_comment, c.column_type, c.java_type, c.java_field, c.is_pk, c.is_increment, c.is_required, c.is_insert, c.is_edit, c.is_list, c.is_query, c.query_type, c.html_type, c.dict_type, c.sort  
52 - FROM gen_table t  
53 - LEFT JOIN gen_table_column c ON t.table_id = c.table_id  
54 - where t.table_name = #{tableName} order by c.sort  
55 - </select>  
56 -  
57 -</mapper>  
ruoyi-ui/src/api/tool/codegen.js
1 import request from '@/utils/request' 1 import request from '@/utils/request'
2 2
3 // 获得表定义分页 3 // 获得表定义分页
4 -export function getCodeGenTablePage(query) { 4 +export function getCodegenTablePage(query) {
5 return request({ 5 return request({
6 url: '/tool/codegen/table/page', 6 url: '/tool/codegen/table/page',
7 method: 'get', 7 method: 'get',
@@ -10,7 +10,7 @@ export function getCodeGenTablePage(query) { @@ -10,7 +10,7 @@ export function getCodeGenTablePage(query) {
10 } 10 }
11 11
12 // 获得表和字段的明细 12 // 获得表和字段的明细
13 -export function getCodeGenDetail(tableId) { 13 +export function getCodegenDetail(tableId) {
14 return request({ 14 return request({
15 url: '/tool/codegen/detail?tableId=' + tableId, 15 url: '/tool/codegen/detail?tableId=' + tableId,
16 method: 'get', 16 method: 'get',
@@ -26,6 +26,14 @@ export function updateCodegen(data) { @@ -26,6 +26,14 @@ export function updateCodegen(data) {
26 }) 26 })
27 } 27 }
28 28
  29 +// 基于数据库的表结构,同步数据库的表和字段定义
  30 +export function syncCodegen(tableId) {
  31 + return request({
  32 + url: '/tool/codegen/sync?tableId=' + tableId,
  33 + method: 'put'
  34 + })
  35 +}
  36 +
29 // 预览生成代码 37 // 预览生成代码
30 export function previewCodegen(tableId) { 38 export function previewCodegen(tableId) {
31 return request({ 39 return request({
@@ -42,3 +50,32 @@ export function downloadCodegen(tableId) { @@ -42,3 +50,32 @@ export function downloadCodegen(tableId) {
42 responseType: 'blob' 50 responseType: 'blob'
43 }) 51 })
44 } 52 }
  53 +
  54 +// 获得表定义分页
  55 +export function getSchemaTableList(query) {
  56 + return request({
  57 + url: '/tool/codegen/db/table/list',
  58 + method: 'get',
  59 + params: query
  60 + })
  61 +}
  62 +
  63 +// 基于数据库的表结构,创建代码生成器的表定义
  64 +export function createCodegenList(tableNames) {
  65 + return request({
  66 + url: '/tool/codegen/create-list',
  67 + method: 'post',
  68 + headers:{
  69 + 'Content-type': 'application/x-www-form-urlencoded'
  70 + },
  71 + data: 'tableNames=' + tableNames
  72 + })
  73 +}
  74 +
  75 +// 删除数据库的表和字段定义
  76 +export function deleteCodegen(tableId) {
  77 + return request({
  78 + url: '/tool/codegen/delete?tableId=' + tableId,
  79 + method: 'delete'
  80 + })
  81 +}
ruoyi-ui/src/api/tool/gen.js deleted
1 -import request from '@/utils/request'  
2 -  
3 -// 查询db数据库列表  
4 -export function listDbTable(query) {  
5 - return request({  
6 - url: '/tool/gen/db/list',  
7 - method: 'get',  
8 - params: query  
9 - })  
10 -}  
11 -  
12 -// 导入表  
13 -export function importTable(data) {  
14 - return request({  
15 - url: '/tool/gen/importTable',  
16 - method: 'post',  
17 - params: data  
18 - })  
19 -}  
20 -  
21 -// 删除表数据  
22 -export function delTable(tableId) {  
23 - return request({  
24 - url: '/tool/gen/' + tableId,  
25 - method: 'delete'  
26 - })  
27 -}  
28 -  
29 -// 生成代码(自定义路径)  
30 -export function genCode(tableName) {  
31 - return request({  
32 - url: '/tool/gen/genCode/' + tableName,  
33 - method: 'get'  
34 - })  
35 -}  
36 -  
37 -// 同步数据库  
38 -export function synchDb(tableName) {  
39 - return request({  
40 - url: '/tool/gen/synchDb/' + tableName,  
41 - method: 'get'  
42 - })  
43 -}  
ruoyi-ui/src/views/tool/gen/editTable.vue
@@ -126,7 +126,7 @@ @@ -126,7 +126,7 @@
126 </el-card> 126 </el-card>
127 </template> 127 </template>
128 <script> 128 <script>
129 -import { getCodeGenDetail, updateCodegen } from "@/api/tool/codegen"; 129 +import { getCodegenDetail, updateCodegen } from "@/api/tool/codegen";
130 import { listAllSimple as listAllSimpleDictType } from "@/api/system/dict/type"; 130 import { listAllSimple as listAllSimpleDictType } from "@/api/system/dict/type";
131 import { listMenu as getMenuTreeselect } from "@/api/system/menu"; 131 import { listMenu as getMenuTreeselect } from "@/api/system/menu";
132 import basicInfoForm from "./basicInfoForm"; 132 import basicInfoForm from "./basicInfoForm";
@@ -161,7 +161,7 @@ export default { @@ -161,7 +161,7 @@ export default {
161 const tableId = this.$route.params && this.$route.params.tableId; 161 const tableId = this.$route.params && this.$route.params.tableId;
162 if (tableId) { 162 if (tableId) {
163 // 获取表详细信息 163 // 获取表详细信息
164 - getCodeGenDetail(tableId).then(res => { 164 + getCodegenDetail(tableId).then(res => {
165 this.table = res.data.table; 165 this.table = res.data.table;
166 this.columns = res.data.columns; 166 this.columns = res.data.columns;
167 }); 167 });
ruoyi-ui/src/views/tool/gen/importTable.vue
@@ -28,18 +28,15 @@ @@ -28,18 +28,15 @@
28 <el-row> 28 <el-row>
29 <el-table @row-click="clickRow" ref="table" :data="dbTableList" @selection-change="handleSelectionChange" height="260px"> 29 <el-table @row-click="clickRow" ref="table" :data="dbTableList" @selection-change="handleSelectionChange" height="260px">
30 <el-table-column type="selection" width="55"></el-table-column> 30 <el-table-column type="selection" width="55"></el-table-column>
  31 + <el-table-column prop="tableSchema" label="数据库" :show-overflow-tooltip="true"></el-table-column>
31 <el-table-column prop="tableName" label="表名称" :show-overflow-tooltip="true"></el-table-column> 32 <el-table-column prop="tableName" label="表名称" :show-overflow-tooltip="true"></el-table-column>
32 <el-table-column prop="tableComment" label="表描述" :show-overflow-tooltip="true"></el-table-column> 33 <el-table-column prop="tableComment" label="表描述" :show-overflow-tooltip="true"></el-table-column>
33 - <el-table-column prop="createTime" label="创建时间"></el-table-column>  
34 - <el-table-column prop="updateTime" label="更新时间"></el-table-column> 34 + <el-table-column prop="createTime" label="创建时间">
  35 + <template slot-scope="scope">
  36 + <span>{{ parseTime(scope.row.createTime) }}</span>
  37 + </template>
  38 + </el-table-column>
35 </el-table> 39 </el-table>
36 - <pagination  
37 - v-show="total>0"  
38 - :total="total"  
39 - :page.sync="queryParams.pageNum"  
40 - :limit.sync="queryParams.pageSize"  
41 - @pagination="getList"  
42 - />  
43 </el-row> 40 </el-row>
44 <div slot="footer" class="dialog-footer"> 41 <div slot="footer" class="dialog-footer">
45 <el-button type="primary" @click="handleImportTable">确 定</el-button> 42 <el-button type="primary" @click="handleImportTable">确 定</el-button>
@@ -49,7 +46,7 @@ @@ -49,7 +46,7 @@
49 </template> 46 </template>
50 47
51 <script> 48 <script>
52 -import { listDbTable, importTable } from "@/api/tool/gen"; 49 +import { getSchemaTableList, createCodegenList } from "@/api/tool/codegen";
53 export default { 50 export default {
54 data() { 51 data() {
55 return { 52 return {
@@ -63,8 +60,6 @@ export default { @@ -63,8 +60,6 @@ export default {
63 dbTableList: [], 60 dbTableList: [],
64 // 查询参数 61 // 查询参数
65 queryParams: { 62 queryParams: {
66 - pageNum: 1,  
67 - pageSize: 10,  
68 tableName: undefined, 63 tableName: undefined,
69 tableComment: undefined 64 tableComment: undefined
70 } 65 }
@@ -85,16 +80,12 @@ export default { @@ -85,16 +80,12 @@ export default {
85 }, 80 },
86 // 查询表数据 81 // 查询表数据
87 getList() { 82 getList() {
88 - listDbTable(this.queryParams).then(res => {  
89 - if (res.code === 200) {  
90 - this.dbTableList = res.rows;  
91 - this.total = res.total;  
92 - } 83 + getSchemaTableList(this.queryParams).then(res => {
  84 + this.dbTableList = res.data;
93 }); 85 });
94 }, 86 },
95 /** 搜索按钮操作 */ 87 /** 搜索按钮操作 */
96 handleQuery() { 88 handleQuery() {
97 - this.queryParams.pageNum = 1;  
98 this.getList(); 89 this.getList();
99 }, 90 },
100 /** 重置按钮操作 */ 91 /** 重置按钮操作 */
@@ -104,12 +95,10 @@ export default { @@ -104,12 +95,10 @@ export default {
104 }, 95 },
105 /** 导入按钮操作 */ 96 /** 导入按钮操作 */
106 handleImportTable() { 97 handleImportTable() {
107 - importTable({ tables: this.tables.join(",") }).then(res => {  
108 - this.msgSuccess(res.msg);  
109 - if (res.code === 200) {  
110 - this.visible = false;  
111 - this.$emit("ok");  
112 - } 98 + createCodegenList(this.tables.join(",")).then(res => {
  99 + this.msgSuccess("导入成功");
  100 + this.visible = false;
  101 + this.$emit("ok");
113 }); 102 });
114 } 103 }
115 } 104 }
ruoyi-ui/src/views/tool/gen/index.vue
@@ -39,9 +39,6 @@ @@ -39,9 +39,6 @@
39 39
40 <el-row :gutter="10" class="mb8"> 40 <el-row :gutter="10" class="mb8">
41 <el-col :span="1.5"> 41 <el-col :span="1.5">
42 - <el-button type="primary" plain icon="el-icon-download" size="mini" @click="handleGenTable" v-hasPermi="['tool:gen:code']">生成</el-button>  
43 - </el-col>  
44 - <el-col :span="1.5">  
45 <el-button type="info" plain icon="el-icon-upload" size="mini" @click="openImportTable" v-hasPermi="['tool:gen:import']">导入</el-button> 42 <el-button type="info" plain icon="el-icon-upload" size="mini" @click="openImportTable" v-hasPermi="['tool:gen:import']">导入</el-button>
46 </el-col> 43 </el-col>
47 <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> 44 <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
@@ -85,15 +82,12 @@ @@ -85,15 +82,12 @@
85 </template> 82 </template>
86 83
87 <script> 84 <script>
88 -import { delTable, synchDb } from "@/api/tool/gen";  
89 -import { getCodeGenTablePage, previewCodegen, downloadCodegen } from "@/api/tool/codegen"; 85 +import { getCodegenTablePage, previewCodegen, downloadCodegen, deleteCodegen, syncCodegen } from "@/api/tool/codegen";
90 86
91 import importTable from "./importTable"; 87 import importTable from "./importTable";
92 // 代码高亮插件 88 // 代码高亮插件
93 import hljs from "highlight.js/lib/highlight"; 89 import hljs from "highlight.js/lib/highlight";
94 import "highlight.js/styles/github-gist.css"; 90 import "highlight.js/styles/github-gist.css";
95 -import {list} from "@/api/system/loginlog";  
96 -import {exportOperateLog} from "@/api/system/operatelog";  
97 hljs.registerLanguage("java", require("highlight.js/lib/languages/java")); 91 hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
98 hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml")); 92 hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml"));
99 hljs.registerLanguage("html", require("highlight.js/lib/languages/xml")); 93 hljs.registerLanguage("html", require("highlight.js/lib/languages/xml"));
@@ -150,7 +144,7 @@ export default { @@ -150,7 +144,7 @@ export default {
150 /** 查询表集合 */ 144 /** 查询表集合 */
151 getList() { 145 getList() {
152 this.loading = true; 146 this.loading = true;
153 - getCodeGenTablePage(this.addDateRange(this.queryParams, [ 147 + getCodegenTablePage(this.addDateRange(this.queryParams, [
154 this.dateRange[0] ? this.dateRange[0] + ' 00:00:00' : undefined, 148 this.dateRange[0] ? this.dateRange[0] + ' 00:00:00' : undefined,
155 this.dateRange[1] ? this.dateRange[1] + ' 23:59:59' : undefined, 149 this.dateRange[1] ? this.dateRange[1] + ' 23:59:59' : undefined,
156 ], 'CreateTime')).then(response => { 150 ], 'CreateTime')).then(response => {
@@ -179,7 +173,7 @@ export default { @@ -179,7 +173,7 @@ export default {
179 cancelButtonText: "取消", 173 cancelButtonText: "取消",
180 type: "warning" 174 type: "warning"
181 }).then(function() { 175 }).then(function() {
182 - return synchDb(tableName); 176 + return syncCodegen(row.id);
183 }).then(() => { 177 }).then(() => {
184 this.msgSuccess("同步成功"); 178 this.msgSuccess("同步成功");
185 }) 179 })
@@ -217,13 +211,13 @@ export default { @@ -217,13 +211,13 @@ export default {
217 }, 211 },
218 /** 删除按钮操作 */ 212 /** 删除按钮操作 */
219 handleDelete(row) { 213 handleDelete(row) {
220 - const tableIds = row.tableId || this.ids;  
221 - this.$confirm('是否确认删除表编号为"' + tableIds + '"的数据项?', "警告", { 214 + const tableIds = row.id;
  215 + this.$confirm('是否确认删除表名称为"' + row.tableName + '"的数据项?', "警告", {
222 confirmButtonText: "确定", 216 confirmButtonText: "确定",
223 cancelButtonText: "取消", 217 cancelButtonText: "取消",
224 type: "warning" 218 type: "warning"
225 }).then(function() { 219 }).then(function() {
226 - return delTable(tableIds); 220 + return deleteCodegen(tableIds);
227 }).then(() => { 221 }).then(() => {
228 this.getList(); 222 this.getList();
229 this.msgSuccess("删除成功"); 223 this.msgSuccess("删除成功");
src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java
@@ -15,6 +15,7 @@ import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColum @@ -15,6 +15,7 @@ import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColum
15 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; 15 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO;
16 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; 16 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO;
17 import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; 17 import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService;
  18 +import cn.iocoder.dashboard.util.collection.CollectionUtils;
18 import cn.iocoder.dashboard.util.servlet.ServletUtils; 19 import cn.iocoder.dashboard.util.servlet.ServletUtils;
19 import io.swagger.annotations.Api; 20 import io.swagger.annotations.Api;
20 import io.swagger.annotations.ApiImplicitParam; 21 import io.swagger.annotations.ApiImplicitParam;
@@ -31,6 +32,7 @@ import java.io.ByteArrayOutputStream; @@ -31,6 +32,7 @@ import java.io.ByteArrayOutputStream;
31 import java.io.IOException; 32 import java.io.IOException;
32 import java.util.List; 33 import java.util.List;
33 import java.util.Map; 34 import java.util.Map;
  35 +import java.util.Set;
34 36
35 import static cn.iocoder.dashboard.common.pojo.CommonResult.success; 37 import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
36 38
@@ -47,7 +49,7 @@ public class ToolCodegenController { @@ -47,7 +49,7 @@ public class ToolCodegenController {
47 @GetMapping("/db/table/list") 49 @GetMapping("/db/table/list")
48 @ApiImplicitParams({ 50 @ApiImplicitParams({
49 @ApiImplicitParam(name = "tableName", required = true, example = "yudao", dataTypeClass = String.class), 51 @ApiImplicitParam(name = "tableName", required = true, example = "yudao", dataTypeClass = String.class),
50 - @ApiImplicitParam(name = "tableComment", required = true, example = "芋道", dataTypeClass = Long.class) 52 + @ApiImplicitParam(name = "tableComment", required = true, example = "芋道", dataTypeClass = String.class)
51 }) 53 })
52 // @PreAuthorize("@ss.hasPermi('tool:gen:list')") TODO 权限 54 // @PreAuthorize("@ss.hasPermi('tool:gen:list')") TODO 权限
53 public CommonResult<List<ToolSchemaTableRespVO>> getSchemaTableList( 55 public CommonResult<List<ToolSchemaTableRespVO>> getSchemaTableList(
@@ -56,33 +58,35 @@ public class ToolCodegenController { @@ -56,33 +58,35 @@ public class ToolCodegenController {
56 // 获得数据库自带的表定义列表 58 // 获得数据库自带的表定义列表
57 List<ToolSchemaTableDO> schemaTables = codegenService.getSchemaTableList(tableName, tableComment); 59 List<ToolSchemaTableDO> schemaTables = codegenService.getSchemaTableList(tableName, tableComment);
58 // 移除在 Codegen 中,已经存在的 60 // 移除在 Codegen 中,已经存在的
59 -  
60 - return null; 61 + Set<String> existsTables = CollectionUtils.convertSet(codegenService.getCodeGenTableList(), ToolCodegenTableDO::getTableName);
  62 + schemaTables.removeIf(table -> existsTables.contains(table.getTableName()));
  63 + return success(ToolCodegenConvert.INSTANCE.convertList04(schemaTables));
61 } 64 }
62 65
63 @ApiOperation("获得表定义分页") 66 @ApiOperation("获得表定义分页")
64 @GetMapping("/table/page") 67 @GetMapping("/table/page")
65 // TODO 权限 @PreAuthorize("@ss.hasPermi('tool:gen:list')") 68 // TODO 权限 @PreAuthorize("@ss.hasPermi('tool:gen:list')")
66 public CommonResult<PageResult<ToolCodegenTableRespVO>> getCodeGenTablePage(@Valid ToolCodegenTablePageReqVO pageReqVO) { 69 public CommonResult<PageResult<ToolCodegenTableRespVO>> getCodeGenTablePage(@Valid ToolCodegenTablePageReqVO pageReqVO) {
67 - PageResult<ToolCodegenTableDO> pageResult = codegenService.getCodeGenTablePage(pageReqVO); 70 + PageResult<ToolCodegenTableDO> pageResult = codegenService.getCodegenTablePage(pageReqVO);
68 return success(ToolCodegenConvert.INSTANCE.convertPage(pageResult)); 71 return success(ToolCodegenConvert.INSTANCE.convertPage(pageResult));
69 } 72 }
70 73
71 @ApiOperation("获得表和字段的明细") 74 @ApiOperation("获得表和字段的明细")
72 @GetMapping("/detail") 75 @GetMapping("/detail")
  76 + @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class)
73 // todo @PreAuthorize("@ss.hasPermi('tool:gen:query')") 77 // todo @PreAuthorize("@ss.hasPermi('tool:gen:query')")
74 - public CommonResult<ToolCodegenDetailRespVO> getCodeGenDetail(@RequestParam("tableId") Long tableId) {  
75 - ToolCodegenTableDO table = codegenService.getCodeGenTablePage(tableId); 78 + public CommonResult<ToolCodegenDetailRespVO> getCodegenDetail(@RequestParam("tableId") Long tableId) {
  79 + ToolCodegenTableDO table = codegenService.getCodegenTablePage(tableId);
76 List<ToolCodegenColumnDO> columns = codegenService.getCodegenColumnListByTableId(tableId); 80 List<ToolCodegenColumnDO> columns = codegenService.getCodegenColumnListByTableId(tableId);
77 // 拼装返回 81 // 拼装返回
78 return success(ToolCodegenConvert.INSTANCE.convert(table, columns)); 82 return success(ToolCodegenConvert.INSTANCE.convert(table, columns));
79 } 83 }
80 84
81 - @ApiOperation("基于数据库的表结构,创建代码生成器的表定义")  
82 - @PostMapping("/create") 85 + @ApiOperation("基于数据库的表结构,创建代码生成器的表和字段定义")
  86 + @PostMapping("/create-list")
83 // TODO 权限 87 // TODO 权限
84 - public CommonResult<Long> createCodeGen(@RequestParam("tableName") String tableName) {  
85 - return success(codegenService.createCodegen(tableName)); 88 + public CommonResult<List<Long>> createCodegenList(@RequestParam("tableNames") List<String> tableNames) {
  89 + return success(codegenService.createCodeGenList(tableNames));
86 } 90 }
87 91
88 @ApiOperation("更新数据库的表和字段定义") 92 @ApiOperation("更新数据库的表和字段定义")
@@ -93,6 +97,24 @@ public class ToolCodegenController { @@ -93,6 +97,24 @@ public class ToolCodegenController {
93 return success(true); 97 return success(true);
94 } 98 }
95 99
  100 + @ApiOperation("基于数据库的表结构,同步数据库的表和字段定义")
  101 + @PutMapping("/sync")
  102 + @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class)
  103 +// @PreAuthorize("@ss.hasPermi('tool:gen:edit')") TODO 权限
  104 + public CommonResult<Boolean> syncCodegen(@RequestParam("tableId") Long tableId) {
  105 + codegenService.syncCodegen(tableId);
  106 + return success(true);
  107 + }
  108 +
  109 + @ApiOperation("删除数据库的表和字段定义")
  110 + @DeleteMapping("/delete")
  111 + @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class)
  112 +// @PreAuthorize("@ss.hasPermi('tool:gen:remove')") TODO 权限
  113 + public CommonResult<Boolean> deleteCodegen(@RequestParam("tableId") Long tableId) {
  114 + codegenService.deleteCodegen(tableId);
  115 + return success(true);
  116 + }
  117 +
96 @ApiOperation("预览生成代码") 118 @ApiOperation("预览生成代码")
97 @GetMapping("/preview") 119 @GetMapping("/preview")
98 @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class) 120 @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class)
@@ -119,4 +141,18 @@ public class ToolCodegenController { @@ -119,4 +141,18 @@ public class ToolCodegenController {
119 ServletUtils.writeAttachment(response, "codegen.zip", outputStream.toByteArray()); 141 ServletUtils.writeAttachment(response, "codegen.zip", outputStream.toByteArray());
120 } 142 }
121 143
  144 +// /**
  145 +// * 查询数据表字段列表
  146 +// */
  147 +// @PreAuthorize("@ss.hasPermi('tool:gen:list')")
  148 +// @GetMapping(value = "/column/{talbleId}")
  149 +// public TableDataInfo columnList(Long tableId) {
  150 +// TableDataInfo dataInfo = new TableDataInfo();
  151 +// List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
  152 +// dataInfo.setRows(list);
  153 +// dataInfo.setTotal(list.size());
  154 +// return dataInfo;
  155 +// }
  156 +//
  157 +
122 } 158 }
src/main/java/cn/iocoder/dashboard/modules/tool/convert/codegen/ToolCodegenConvert.java
@@ -6,6 +6,7 @@ import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenPrevie @@ -6,6 +6,7 @@ import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenPrevie
6 import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenUpdateReqVO; 6 import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenUpdateReqVO;
7 import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.column.ToolCodegenColumnRespVO; 7 import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.column.ToolCodegenColumnRespVO;
8 import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTableRespVO; 8 import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTableRespVO;
  9 +import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolSchemaTableRespVO;
9 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; 10 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO;
10 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; 11 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO;
11 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; 12 import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO;
@@ -46,6 +47,8 @@ public interface ToolCodegenConvert { @@ -46,6 +47,8 @@ public interface ToolCodegenConvert {
46 47
47 List<ToolCodegenColumnDO> convertList03(List<ToolCodegenUpdateReqVO.Column> columns); 48 List<ToolCodegenColumnDO> convertList03(List<ToolCodegenUpdateReqVO.Column> columns);
48 49
  50 + List<ToolSchemaTableRespVO> convertList04(List<ToolSchemaTableDO> list);
  51 +
49 // ========== 其它 ========== 52 // ========== 其它 ==========
50 53
51 default ToolCodegenDetailRespVO convert(ToolCodegenTableDO table, List<ToolCodegenColumnDO> columns) { 54 default ToolCodegenDetailRespVO convert(ToolCodegenTableDO table, List<ToolCodegenColumnDO> columns) {
src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolCodegenColumnMapper.java
@@ -11,9 +11,12 @@ import java.util.List; @@ -11,9 +11,12 @@ import java.util.List;
11 public interface ToolCodegenColumnMapper extends BaseMapperX<ToolCodegenColumnDO> { 11 public interface ToolCodegenColumnMapper extends BaseMapperX<ToolCodegenColumnDO> {
12 12
13 default List<ToolCodegenColumnDO> selectListByTableId(Long tableId) { 13 default List<ToolCodegenColumnDO> selectListByTableId(Long tableId) {
14 - return selectList(new QueryWrapper<ToolCodegenColumnDO>()  
15 - .eq("table_id", tableId) 14 + return selectList(new QueryWrapper<ToolCodegenColumnDO>().eq("table_id", tableId)
16 .orderByAsc("ordinal_position")); 15 .orderByAsc("ordinal_position"));
17 } 16 }
18 17
  18 + default void deleteListByTableId(Long tableId) {
  19 + delete(new QueryWrapper<ToolCodegenColumnDO>().eq("table_id", tableId));
  20 + }
  21 +
19 } 22 }
src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaTableMapper.java
@@ -14,8 +14,8 @@ public interface ToolSchemaTableMapper extends BaseMapperX&lt;ToolSchemaTableDO&gt; { @@ -14,8 +14,8 @@ public interface ToolSchemaTableMapper extends BaseMapperX&lt;ToolSchemaTableDO&gt; {
14 14
15 default List<ToolSchemaTableDO> selectList(Collection<String> tableSchemas, String tableName, String tableComment) { 15 default List<ToolSchemaTableDO> selectList(Collection<String> tableSchemas, String tableName, String tableComment) {
16 return selectList(new QueryWrapperX<ToolSchemaTableDO>().in("table_schema", tableSchemas) 16 return selectList(new QueryWrapperX<ToolSchemaTableDO>().in("table_schema", tableSchemas)
17 - .eqIfPresent("table_name", tableName)  
18 - .eqIfPresent("table_comment", tableComment)); 17 + .likeIfPresent("table_name", tableName)
  18 + .likeIfPresent("table_comment", tableComment));
19 } 19 }
20 20
21 default List<ToolSchemaTableDO> selectListByTableSchema(String tableSchema) { 21 default List<ToolSchemaTableDO> selectListByTableSchema(String tableSchema) {
src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/ToolCodegenService.java
@@ -21,11 +21,19 @@ public interface ToolCodegenService { @@ -21,11 +21,19 @@ public interface ToolCodegenService {
21 * 基于数据库的表结构,创建代码生成器的表定义 21 * 基于数据库的表结构,创建代码生成器的表定义
22 * 22 *
23 * @param tableName 表名称 23 * @param tableName 表名称
24 - * @return 表定义的编号 24 + * @return 创建的表定义的编号
25 */ 25 */
26 Long createCodegen(String tableName); 26 Long createCodegen(String tableName);
27 27
28 /** 28 /**
  29 + * 基于 {@link #createCodegen(String)} 的批量创建
  30 + *
  31 + * @param tableNames 表名称数组
  32 + * @return 创建的表定义的编号数组
  33 + */
  34 + List<Long> createCodeGenList(List<String> tableNames);
  35 +
  36 + /**
29 * 更新数据库的表和字段定义 37 * 更新数据库的表和字段定义
30 * 38 *
31 * @param updateReqVO 更新信息 39 * @param updateReqVO 更新信息
@@ -33,12 +41,26 @@ public interface ToolCodegenService { @@ -33,12 +41,26 @@ public interface ToolCodegenService {
33 void updateCodegen(ToolCodegenUpdateReqVO updateReqVO); 41 void updateCodegen(ToolCodegenUpdateReqVO updateReqVO);
34 42
35 /** 43 /**
  44 + * 基于数据库的表结构,同步数据库的表和字段定义
  45 + *
  46 + * @param tableId 表编号
  47 + */
  48 + void syncCodegen(Long tableId);
  49 +
  50 + /**
  51 + * 删除数据库的表和字段定义
  52 + *
  53 + * @param tableId 数据编号
  54 + */
  55 + void deleteCodegen(Long tableId);
  56 +
  57 + /**
36 * 获得表定义分页 58 * 获得表定义分页
37 * 59 *
38 * @param pageReqVO 分页条件 60 * @param pageReqVO 分页条件
39 * @return 表定义分页 61 * @return 表定义分页
40 */ 62 */
41 - PageResult<ToolCodegenTableDO> getCodeGenTablePage(ToolCodegenTablePageReqVO pageReqVO); 63 + PageResult<ToolCodegenTableDO> getCodegenTablePage(ToolCodegenTablePageReqVO pageReqVO);
42 64
43 /** 65 /**
44 * 获得表定义 66 * 获得表定义
@@ -46,7 +68,14 @@ public interface ToolCodegenService { @@ -46,7 +68,14 @@ public interface ToolCodegenService {
46 * @param id 表编号 68 * @param id 表编号
47 * @return 表定义 69 * @return 表定义
48 */ 70 */
49 - ToolCodegenTableDO getCodeGenTablePage(Long id); 71 + ToolCodegenTableDO getCodegenTablePage(Long id);
  72 +
  73 + /**
  74 + * 获得全部表定义
  75 + *
  76 + * @return 表定义数组
  77 + */
  78 + List<ToolCodegenTableDO> getCodeGenTableList();
50 79
51 /** 80 /**
52 * 获得指定表的字段定义数组 81 * 获得指定表的字段定义数组
src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java
@@ -15,12 +15,16 @@ import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper @@ -15,12 +15,16 @@ import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper
15 import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaColumnMapper; 15 import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaColumnMapper;
16 import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaTableMapper; 16 import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaTableMapper;
17 import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; 17 import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService;
  18 +import cn.iocoder.dashboard.util.collection.CollectionUtils;
18 import org.springframework.stereotype.Service; 19 import org.springframework.stereotype.Service;
19 import org.springframework.transaction.annotation.Transactional; 20 import org.springframework.transaction.annotation.Transactional;
20 21
21 import javax.annotation.Resource; 22 import javax.annotation.Resource;
  23 +import java.util.ArrayList;
22 import java.util.List; 24 import java.util.List;
23 import java.util.Map; 25 import java.util.Map;
  26 +import java.util.Set;
  27 +import java.util.stream.Collectors;
24 28
25 /** 29 /**
26 * 代码生成 Service 实现类 30 * 代码生成 Service 实现类
@@ -78,6 +82,15 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { @@ -78,6 +82,15 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
78 82
79 @Override 83 @Override
80 @Transactional 84 @Transactional
  85 + public List<Long> createCodeGenList(List<String> tableNames) {
  86 + List<Long> ids = new ArrayList<>(tableNames.size());
  87 + // 遍历添加。虽然效率会低一点,但是没必要做成完全批量,因为不会这么大量
  88 + tableNames.forEach(tableName -> ids.add(createCodegen(tableName)));
  89 + return ids;
  90 + }
  91 +
  92 + @Override
  93 + @Transactional
81 public void updateCodegen(ToolCodegenUpdateReqVO updateReqVO) { 94 public void updateCodegen(ToolCodegenUpdateReqVO updateReqVO) {
82 // 校验是否已经存在 95 // 校验是否已经存在
83 if (codegenTableMapper.selectById(updateReqVO.getTable().getId()) == null) { 96 if (codegenTableMapper.selectById(updateReqVO.getTable().getId()) == null) {
@@ -93,16 +106,71 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { @@ -93,16 +106,71 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
93 } 106 }
94 107
95 @Override 108 @Override
96 - public PageResult<ToolCodegenTableDO> getCodeGenTablePage(ToolCodegenTablePageReqVO pageReqVO) { 109 + public void syncCodegen(Long tableId) {
  110 + // 校验是否已经存在
  111 + ToolCodegenTableDO table = codegenTableMapper.selectById(tableId);
  112 + if (table == null) {
  113 + throw new RuntimeException(""); // TODO
  114 + }
  115 + // 从数据库中,获得数据库表结构
  116 + List<ToolSchemaColumnDO> schemaColumns = schemaColumnMapper.selectListByTableName(table.getTableName());
  117 + if (CollUtil.isEmpty(schemaColumns)) {
  118 + throw new RuntimeException(""); // TODO
  119 + }
  120 + Set<String> schemaColumnNames = CollectionUtils.convertSet(schemaColumns, ToolSchemaColumnDO::getColumnName);
  121 +
  122 + // 构建 ToolCodegenColumnDO 数组,只同步新增的字段
  123 + List<ToolCodegenColumnDO> codegenColumns = codegenColumnMapper.selectListByTableId(tableId);
  124 + Set<String> codegenColumnNames = CollectionUtils.convertSet(codegenColumns, ToolCodegenColumnDO::getColumnName);
  125 + // 移除已经存在的字段
  126 + schemaColumns.removeIf(column -> codegenColumnNames.contains(column.getColumnName()));
  127 + // 计算需要删除的字段
  128 + Set<Long> deleteColumnIds = codegenColumns.stream().filter(column -> !schemaColumnNames.contains(column.getColumnName()))
  129 + .map(ToolCodegenColumnDO::getId).collect(Collectors.toSet());
  130 + if (CollUtil.isEmpty(schemaColumns) && CollUtil.isEmpty(deleteColumnIds)) {
  131 + throw new RuntimeException(""); // TODO
  132 + }
  133 +
  134 + // 插入新增的字段
  135 + List<ToolCodegenColumnDO> columns = codegenBuilder.buildColumns(schemaColumns);
  136 + columns.forEach(column -> {
  137 + column.setTableId(table.getId());
  138 + codegenColumnMapper.insert(column); // TODO 批量插入
  139 + });
  140 + // 删除不存在的字段
  141 + codegenColumnMapper.deleteBatchIds(deleteColumnIds);
  142 + }
  143 +
  144 + @Override
  145 + @Transactional
  146 + public void deleteCodegen(Long tableId) {
  147 + // 校验是否已经存在
  148 + if (codegenTableMapper.selectById(tableId) == null) {
  149 + throw new RuntimeException(""); // TODO
  150 + }
  151 +
  152 + // 删除 table 表定义
  153 + codegenTableMapper.deleteById(tableId);
  154 + // 删除 column 字段定义
  155 + codegenColumnMapper.deleteListByTableId(tableId);
  156 + }
  157 +
  158 + @Override
  159 + public PageResult<ToolCodegenTableDO> getCodegenTablePage(ToolCodegenTablePageReqVO pageReqVO) {
97 return codegenTableMapper.selectPage(pageReqVO); 160 return codegenTableMapper.selectPage(pageReqVO);
98 } 161 }
99 162
100 @Override 163 @Override
101 - public ToolCodegenTableDO getCodeGenTablePage(Long id) { 164 + public ToolCodegenTableDO getCodegenTablePage(Long id) {
102 return codegenTableMapper.selectById(id); 165 return codegenTableMapper.selectById(id);
103 } 166 }
104 167
105 @Override 168 @Override
  169 + public List<ToolCodegenTableDO> getCodeGenTableList() {
  170 + return codegenTableMapper.selectList();
  171 + }
  172 +
  173 + @Override
106 public List<ToolCodegenColumnDO> getCodegenColumnListByTableId(Long tableId) { 174 public List<ToolCodegenColumnDO> getCodegenColumnListByTableId(Long tableId) {
107 return codegenColumnMapper.selectListByTableId(tableId); 175 return codegenColumnMapper.selectListByTableId(tableId);
108 } 176 }
@@ -128,4 +196,24 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { @@ -128,4 +196,24 @@ public class ToolCodegenServiceImpl implements ToolCodegenService {
128 return schemaTableMapper.selectList(codegenProperties.getDbSchemas(), tableName, tableComment); 196 return schemaTableMapper.selectList(codegenProperties.getDbSchemas(), tableName, tableComment);
129 } 197 }
130 198
  199 +// /**
  200 +// * 修改保存参数校验
  201 +// *
  202 +// * @param genTable 业务信息
  203 +// */
  204 +// @Override
  205 +// public void validateEdit(GenTable genTable) {
  206 +// if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {
  207 +// String options = JSON.toJSONString(genTable.getParams());
  208 +// JSONObject paramsObj = JSONObject.parseObject(options);
  209 +// if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) {
  210 +// throw new CustomException("树编码字段不能为空");
  211 +// } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) {
  212 +// throw new CustomException("树父编码字段不能为空");
  213 +// } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) {
  214 +// throw new CustomException("树名称字段不能为空");
  215 +// }
  216 +// }
  217 +// }
  218 +
131 } 219 }