Browse Source

代码生成支持同步数据库

tags/v2.2
RuoYi 4 years ago
parent
commit
f4f89f9de6
  1. 14
      ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/controller/GenController.java
  2. 10
      ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/mapper/GenTableColumnMapper.java
  3. 33
      ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java
  4. 7
      ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/IGenTableService.java
  5. 7
      ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml
  6. 2
      ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableMapper.xml
  7. 2
      ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
  8. 8
      ruoyi-ui/src/api/tool/gen.js
  9. 22
      ruoyi-ui/src/views/tool/gen/index.vue

14
ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/controller/GenController.java

@ -164,13 +164,25 @@ public class GenController extends BaseController @@ -164,13 +164,25 @@ public class GenController extends BaseController
@PreAuthorize(hasPermi = "tool:gen:code")
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/genCode/{tableName}")
public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableName") String tableName)
public AjaxResult genCode(@PathVariable("tableName") String tableName)
{
genTableService.generatorCode(tableName);
return AjaxResult.success();
}
/**
* 同步数据库
*/
@PreAuthorize(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();
}
/**
* 批量生成代码
*/
@PreAuthorize(hasPermi = "tool:gen:code")

10
ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/mapper/GenTableColumnMapper.java

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

33
ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java

@ -7,6 +7,7 @@ import java.io.StringWriter; @@ -7,6 +7,7 @@ import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
@ -223,7 +224,6 @@ public class GenTableServiceImpl implements IGenTableService @@ -223,7 +224,6 @@ public class GenTableServiceImpl implements IGenTableService
* 生成代码自定义路径
*
* @param tableName 表名称
* @return 数据
*/
@Override
public void generatorCode(String tableName)
@ -262,6 +262,37 @@ public class GenTableServiceImpl implements IGenTableService @@ -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);
}
}
/**
* 批量生成代码下载方式
*
* @param tableNames 表数组

7
ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/IGenTableService.java

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

7
ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableColumnMapper.xml

@ -117,4 +117,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" @@ -117,4 +117,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</foreach>
</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>

2
ruoyi-modules/ruoyi-gen/src/main/resources/mapper/generator/GenTableMapper.xml

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

2
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml

@ -75,7 +75,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" @@ -75,7 +75,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
</if>
<if test="deptId != null and deptId != 0">
AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE FIND_IN_SET (#{deptId},ancestors) ))
AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
</if>
<!-- 数据范围过滤 -->
${params.dataScope}

8
ruoyi-ui/src/api/tool/gen.js

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

22
ruoyi-ui/src/views/tool/gen/index.vue

@ -132,6 +132,13 @@ @@ -132,6 +132,13 @@
@click="handleDelete(scope.row)"
v-hasPermi="['tool:gen:remove']"
>删除</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
type="text"
size="small"
@ -167,7 +174,7 @@ @@ -167,7 +174,7 @@
</template>
<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 { downLoadZip } from "@/utils/zipdownload";
export default {
@ -252,6 +259,19 @@ export default { @@ -252,6 +259,19 @@ export default {
downLoadZip("/code/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() {
this.$refs.import.show();

Loading…
Cancel
Save