代码生成支持同步数据库

This commit is contained in:
RuoYi 2020-09-18 10:24:21 +08:00
parent 9ca28d6dbf
commit 9e38c7de2e
8 changed files with 96 additions and 5 deletions

View File

@ -165,12 +165,24 @@ public class GenController extends BaseController
@PreAuthorize("@ss.hasPermi('tool:gen:code')") @PreAuthorize("@ss.hasPermi('tool:gen:code')")
@Log(title = "代码生成", businessType = BusinessType.GENCODE) @Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/genCode/{tableName}") @GetMapping("/genCode/{tableName}")
public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String tableName) public AjaxResult genCode(@PathVariable("tableName") String tableName)
{ {
genTableService.generatorCode(tableName); genTableService.generatorCode(tableName);
return AjaxResult.success(); return AjaxResult.success();
} }
/**
* 同步数据库
*/
@PreAuthorize("@ss.hasPermi('tool:gen:edit')")
@Log(title = "代码生成", businessType = BusinessType.UPDATE)
@GetMapping("/synchDb/{tableName}")
public AjaxResult synchDb(@PathVariable("tableName") String tableName)
{
genTableService.synchDb(tableName);
return AjaxResult.success();
}
/** /**
* 批量生成代码 * 批量生成代码
*/ */

View File

@ -17,7 +17,7 @@ public interface GenTableColumnMapper
* @return 列信息 * @return 列信息
*/ */
public List<GenTableColumn> selectDbTableColumnsByName(String tableName); public List<GenTableColumn> selectDbTableColumnsByName(String tableName);
/** /**
* 查询业务字段列表 * 查询业务字段列表
* *
@ -42,6 +42,14 @@ public interface GenTableColumnMapper
*/ */
public int updateGenTableColumn(GenTableColumn genTableColumn); public int updateGenTableColumn(GenTableColumn genTableColumn);
/**
* 删除业务字段
*
* @param genTableColumns 列数据
* @return 结果
*/
public int deleteGenTableColumns(List<GenTableColumn> genTableColumns);
/** /**
* 批量删除业务字段 * 批量删除业务字段
* *

View File

@ -7,6 +7,7 @@ import java.io.StringWriter;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -224,7 +225,6 @@ public class GenTableServiceImpl implements IGenTableService
* 生成代码自定义路径 * 生成代码自定义路径
* *
* @param tableName 表名称 * @param tableName 表名称
* @return 数据
*/ */
@Override @Override
public void generatorCode(String tableName) public void generatorCode(String tableName)
@ -262,6 +262,37 @@ public class GenTableServiceImpl implements IGenTableService
} }
} }
/**
* 同步数据库
*
* @param tableName 表名称
*/
@Override
@Transactional
public void synchDb(String tableName)
{
GenTable table = genTableMapper.selectGenTableByName(tableName);
List<GenTableColumn> tableColumns = table.getColumns();
List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
dbTableColumns.forEach(column -> {
if (!tableColumnNames.contains(column.getColumnName()))
{
GenUtils.initColumnField(column, table);
genTableColumnMapper.insertGenTableColumn(column);
}
});
List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
if (StringUtils.isNotEmpty(delColumns))
{
genTableColumnMapper.deleteGenTableColumns(delColumns);
}
}
/** /**
* 批量生成代码下载方式 * 批量生成代码下载方式
* *

View File

@ -90,6 +90,13 @@ public interface IGenTableService
*/ */
public void generatorCode(String tableName); public void generatorCode(String tableName);
/**
* 同步数据库
*
* @param tableName 表名称
*/
public void synchDb(String tableName);
/** /**
* 批量生成代码下载方式 * 批量生成代码下载方式
* *

View File

@ -117,4 +117,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</foreach> </foreach>
</delete> </delete>
<delete id="deleteGenTableColumns">
delete from gen_table_column where column_id in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item.columnId}
</foreach>
</delete>
</mapper> </mapper>

View File

@ -186,4 +186,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</foreach> </foreach>
</delete> </delete>
</mapper> </mapper>

View File

@ -67,3 +67,10 @@ export function genCode(tableName) {
}) })
} }
// 同步数据库
export function synchDb(tableName) {
return request({
url: '/tool/gen/synchDb/' + tableName,
method: 'get'
})
}

View File

@ -132,6 +132,13 @@
@click="handleDelete(scope.row)" @click="handleDelete(scope.row)"
v-hasPermi="['tool:gen:remove']" v-hasPermi="['tool:gen:remove']"
>删除</el-button> >删除</el-button>
<el-button
type="text"
size="small"
icon="el-icon-refresh"
@click="handleSynchDb(scope.row)"
v-hasPermi="['tool:gen:edit']"
>同步</el-button>
<el-button <el-button
type="text" type="text"
size="small" size="small"
@ -167,7 +174,7 @@
</template> </template>
<script> <script>
import { listTable, previewTable, delTable, genCode } from "@/api/tool/gen"; import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
import importTable from "./importTable"; import importTable from "./importTable";
import { downLoadZip } from "@/utils/zipdownload"; import { downLoadZip } from "@/utils/zipdownload";
export default { export default {
@ -252,6 +259,19 @@ export default {
downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi"); downLoadZip("/tool/gen/batchGenCode?tables=" + tableNames, "ruoyi");
} }
}, },
/** 同步数据库操作 */
handleSynchDb(row) {
const tableName = row.tableName;
this.$confirm('确认要强制同步"' + tableName + '"表结构吗?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
return synchDb(tableName);
}).then(() => {
this.msgSuccess("同步成功");
}).catch(function() {});
},
/** 打开导入表弹窗 */ /** 打开导入表弹窗 */
openImportTable() { openImportTable() {
this.$refs.import.show(); this.$refs.import.show();