代码生成支持同步数据库

pull/197/MERGE
RuoYi 2020-08-18 11:41:00 +08:00
parent 9ab3a297bd
commit dc82712597
6 changed files with 80 additions and 6 deletions

View File

@ -206,12 +206,25 @@ public class GenController extends BaseController
@Log(title = "代码生成", businessType = BusinessType.GENCODE) @Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/genCode/{tableName}") @GetMapping("/genCode/{tableName}")
@ResponseBody @ResponseBody
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();
} }
/**
*
*/
@RequiresPermissions("tool:gen:edit")
@Log(title = "代码生成", businessType = BusinessType.UPDATE)
@GetMapping("/synchDb/{tableName}")
@ResponseBody
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);
/** /**
* *
* *
@ -49,4 +57,4 @@ public interface GenTableColumnMapper
* @return * @return
*/ */
public int deleteGenTableColumnByIds(Long[] ids); public int deleteGenTableColumnByIds(Long[] ids);
} }

View File

@ -94,9 +94,15 @@ public interface IGenTableService
* *
* *
* @param tableName * @param tableName
* @return
*/ */
public void generatorCode(String tableName); public void generatorCode(String tableName);
/**
*
*
* @param tableName
*/
public void synchDb(String tableName);
/** /**
* *

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;
@ -237,7 +238,6 @@ public class GenTableServiceImpl implements IGenTableService
* *
* *
* @param tableName * @param tableName
* @return
*/ */
@Override @Override
public void generatorCode(String tableName) public void generatorCode(String tableName)
@ -276,6 +276,38 @@ 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

@ -116,5 +116,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{tableId} #{tableId}
</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

@ -125,6 +125,7 @@
actions.push('<a class="btn btn-info btn-xs ' + previewFlag + '" href="javascript:void(0)" onclick="preview(\'' + row.tableId + '\')"><i class="fa fa-search"></i>预览</a> '); actions.push('<a class="btn btn-info btn-xs ' + previewFlag + '" href="javascript:void(0)" onclick="preview(\'' + row.tableId + '\')"><i class="fa fa-search"></i>预览</a> ');
actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.tableId + '\')"><i class="fa fa-edit"></i>编辑</a> '); actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.tableId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.tableId + '\')"><i class="fa fa-remove"></i>删除</a> '); actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.tableId + '\')"><i class="fa fa-remove"></i>删除</a> ');
actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="synchDb(\'' + row.tableName + '\')"><i class="fa fa-refresh"></i>同步</a> ');
actions.push('<a class="btn btn-primary btn-xs ' + codeFlag + '" href="javascript:void(0)" onclick="genCode(\'' + row.tableName + '\',\'' + row.genType + '\')"><i class="fa fa-bug"></i>生成代码</a> '); actions.push('<a class="btn btn-primary btn-xs ' + codeFlag + '" href="javascript:void(0)" onclick="genCode(\'' + row.tableName + '\',\'' + row.genType + '\')"><i class="fa fa-bug"></i>生成代码</a> ');
return actions.join(''); return actions.join('');
} }
@ -173,6 +174,13 @@
} }
}) })
} }
// 同步数据库
function synchDb(tableName){
$.modal.confirm("确认要强制同步" + tableName + "表结构吗?", function() {
$.operate.get(prefix + "/synchDb/" + tableName);
})
}
// 批量生成代码 // 批量生成代码
function batchGenCode() { function batchGenCode() {