fix():修复代码生成器

dev
ruyaxie 2024-09-17 22:03:08 +08:00
parent 318f2faf24
commit 5fb84afe65
20 changed files with 782 additions and 864 deletions

View File

@ -16,5 +16,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class CloudGenApplication {
public static void main (String[] args) {
SpringApplication.run(CloudGenApplication.class, args);
System.out.println("CloudGen 模块启动成功!");
}
}

View File

@ -1,22 +1,24 @@
package com.muyu.gen.controller;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.text.Convert;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.gen.domain.GenTable;
import com.muyu.gen.domain.GenTableColumn;
import com.muyu.gen.domain.GenTableResp;
import com.muyu.gen.service.IGenTableColumnService;
import com.muyu.gen.service.IGenTableService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@ -25,15 +27,19 @@ import java.util.Map;
/**
*
*
* @author muyu
* @author ruoyi
*/
@RequestMapping("/gen")
@RestController
public class GenController extends BaseController {
@Autowired
public class GenController extends BaseController
{
@Resource
private IGenTableService genTableService;
@Autowired
@Resource
private HttpServletResponse response;
@Resource
private IGenTableColumnService genTableColumnService;
/**
@ -41,7 +47,8 @@ public class GenController extends BaseController {
*/
@RequiresPermissions("tool:gen:list")
@GetMapping("/list")
public Result<TableDataInfo<GenTable>> genList (GenTable genTable) {
public Result genList(GenTable genTable)
{
startPage();
List<GenTable> list = genTableService.selectGenTableList(genTable);
return getDataTable(list);
@ -52,7 +59,8 @@ public class GenController extends BaseController {
*/
@RequiresPermissions("tool:gen:query")
@GetMapping(value = "/{tableId}")
public Result getInfo (@PathVariable("tableId") Long tableId) {
public Result getInfo(@PathVariable("tableId") Long tableId)
{
GenTable table = genTableService.selectGenTableById(tableId);
List<GenTable> tables = genTableService.selectGenTableAll();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
@ -63,12 +71,14 @@ public class GenController extends BaseController {
return success(map);
}
/**
*
*/
@RequiresPermissions("tool:gen:list")
@GetMapping("/db/list")
public Result<TableDataInfo<GenTable>> dataList (GenTable genTable) {
public Result dataList(GenTable genTable)
{
startPage();
List<GenTable> list = genTableService.selectDbTableList(genTable);
return getDataTable(list);
@ -78,26 +88,26 @@ public class GenController extends BaseController {
*
*/
@GetMapping(value = "/column/{tableId}")
public Result<TableDataInfo<GenTableColumn>> columnList (Long tableId) {
public Result columnList(@PathVariable("tableId") Long tableId)
{
TableDataInfo dataInfo = new TableDataInfo();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
return Result.success(
TableDataInfo.<GenTableColumn>builder()
.total(list.size())
.rows(list)
.build()
);
dataInfo.setRows(list);
dataInfo.setTotal(list.size());
return success(dataInfo);
}
/**
*
*/
@RequiresPermissions("tool:gen:import")
// @RequiresPermissions("tool:gen:import")
@Log(title = "代码生成", businessType = BusinessType.IMPORT)
@PostMapping("/importTable")
public Result importTableSave (String tables) {
public Result importTableSave(@RequestParam("tables") String tables, @RequestParam("dbName") String dbName)
{
String[] tableNames = Convert.toStrArray(tables);
// 查询表信息
List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames,dbName);
genTableService.importGenTable(tableList);
return success();
}
@ -108,7 +118,8 @@ public class GenController extends BaseController {
@RequiresPermissions("tool:gen:edit")
@Log(title = "代码生成", businessType = BusinessType.UPDATE)
@PutMapping
public Result editSave (@Validated @RequestBody GenTable genTable) {
public Result editSave(@Validated @RequestBody GenTable genTable)
{
genTableService.validateEdit(genTable);
genTableService.updateGenTable(genTable);
return success();
@ -120,7 +131,8 @@ public class GenController extends BaseController {
@RequiresPermissions("tool:gen:remove")
@Log(title = "代码生成", businessType = BusinessType.DELETE)
@DeleteMapping("/{tableIds}")
public Result remove (@PathVariable("tableIds") Long[] tableIds) {
public Result remove(@PathVariable("tableIds") Long[] tableIds)
{
genTableService.deleteGenTableByIds(tableIds);
return success();
}
@ -130,7 +142,8 @@ public class GenController extends BaseController {
*/
@RequiresPermissions("tool:gen:preview")
@GetMapping("/preview/{tableId}")
public Result preview (@PathVariable("tableId") Long tableId) throws IOException {
public Result preview(@PathVariable("tableId") Long tableId) throws IOException
{
Map<String, String> dataMap = genTableService.previewCode(tableId);
return success(dataMap);
}
@ -141,7 +154,8 @@ public class GenController extends BaseController {
@RequiresPermissions("tool:gen:code")
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/download/{tableName}")
public void download (HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
public void download(@PathVariable("tableName") String tableName) throws IOException
{
byte[] data = genTableService.downloadCode(tableName);
genCode(response, data);
}
@ -152,7 +166,8 @@ public class GenController extends BaseController {
@RequiresPermissions("tool:gen:code")
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/genCode/{tableName}")
public Result genCode (@PathVariable("tableName") String tableName) {
public Result genCode(@PathVariable("tableName") String tableName)
{
genTableService.generatorCode(tableName);
return success();
}
@ -162,9 +177,10 @@ public class GenController extends BaseController {
*/
@RequiresPermissions("tool:gen:edit")
@Log(title = "代码生成", businessType = BusinessType.UPDATE)
@GetMapping("/synchDb/{tableName}")
public Result synchDb (@PathVariable("tableName") String tableName) {
genTableService.synchDb(tableName);
@GetMapping("/synchDb/{tableName}/{dbName}")
public Result synchDb(@PathVariable("tableName") String tableName,@PathVariable("dbName") String dbName)
{
genTableService.synchDb(tableName,dbName);
return success();
}
@ -174,7 +190,8 @@ public class GenController extends BaseController {
@RequiresPermissions("tool:gen:code")
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/batchGenCode")
public void batchGenCode (HttpServletResponse response, String tables) throws IOException {
public void batchGenCode(@RequestParam("tables") String tables) throws IOException
{
String[] tableNames = Convert.toStrArray(tables);
byte[] data = genTableService.downloadCode(tableNames);
genCode(response, data);
@ -183,11 +200,37 @@ public class GenController extends BaseController {
/**
* zip
*/
private void genCode (HttpServletResponse response, byte[] data) throws IOException {
private void genCode(HttpServletResponse response, byte[] data) throws IOException
{
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"muyu.zip\"");
response.addHeader("Content-Length", String.valueOf(data.length));
response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
/**
*
*/
@GetMapping("/db/selDbNameAll")
public Result<List<String>> selDbNameAll(){
return Result.success(genTableService.selDbNameAll());
}
/**
* ,
*/
@GetMapping("/db/listAll")
public Result<List<GenTableResp>> genListAll() {
return success(genTableService.selectDbTableListAll());
}
/**
*
*/
@GetMapping("/selectDbTableColumnsByName")
public Result<List<GenTableColumn>> selTableAll(@RequestParam("dbName") String dbName,@RequestParam("table") String table){
List<GenTableColumn> genTableColumns = genTableColumnService.selectDbTableColumnsByName(table, dbName);
return Result.success(genTableColumns);
}
}

View File

@ -35,6 +35,8 @@ public class GenTable extends BaseEntity {
*/
private Long tableId;
private String dbName;
/**
*
*/

View File

@ -0,0 +1,43 @@
package com.muyu.gen.domain;
import com.muyu.common.core.constant.GenConstants;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.web.domain.BaseEntity;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.ArrayUtils;
import java.util.List;
/**
* gen_table
*
* @author muyu
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class GenTableResp extends BaseEntity {
private String dbName;
/**
*
*/
private String tableName;
/**
*
*/
private String tableComment;
}

View File

@ -1,67 +1,64 @@
package com.muyu.gen.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.gen.domain.GenTableColumn;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
*
*
* @author muyu
* @author ruoyi
*/
public interface GenTableColumnMapper extends BaseMapper<GenTableColumn> {
public interface GenTableColumnMapper
{
/**
*
*
* @param tableName
*
* @param dbName
* @return
*/
List<GenTableColumn> selectDbTableColumnsByName (String tableName);
public List<GenTableColumn> selectDbTableColumnsByName(@Param("tableName") String tableName, @Param("dbName") String dbName);
/**
*
*
* @param tableId
*
* @return
*/
List<GenTableColumn> selectGenTableColumnListByTableId (Long tableId);
public List<GenTableColumn> selectGenTableColumnListByTableId(@Param("tableId") Long tableId);
/**
*
*
* @param genTableColumn
*
* @return
*/
int insertGenTableColumn (GenTableColumn genTableColumn);
public int insertGenTableColumn(GenTableColumn genTableColumn);
/**
*
*
* @param genTableColumn
*
* @return
*/
int updateGenTableColumn (GenTableColumn genTableColumn);
public int updateGenTableColumn(GenTableColumn genTableColumn);
/**
*
*
* @param genTableColumns
*
* @return
*/
int deleteGenTableColumns (List<GenTableColumn> genTableColumns);
public int deleteGenTableColumns(@Param("genTableColumns") List<GenTableColumn> genTableColumns);
/**
*
*
* @param ids ID
*
* @return
*/
int deleteGenTableColumnByIds (Long[] ids);
public int deleteGenTableColumnByIds(@Param("ids") Long[] ids);
}

View File

@ -1,92 +1,91 @@
package com.muyu.gen.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.gen.domain.GenTable;
import com.muyu.gen.domain.GenTableResp;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
*
*
* @author muyu
* @author ruoyi
*/
public interface GenTableMapper extends BaseMapper<GenTable> {
public interface GenTableMapper
{
/**
*
*
* @param genTable
*
* @return
*/
List<GenTable> selectGenTableList (GenTable genTable);
public List<GenTable> selectGenTableList(GenTable genTable);
/**
*
*
* @param genTable
*
* @return
*/
List<GenTable> selectDbTableList (GenTable genTable);
public List<GenTable> selectDbTableList(GenTable genTable);
/**
*
*
* @param tableNames
*
* @param dbName
* @return
*/
List<GenTable> selectDbTableListByNames (String[] tableNames);
public List<GenTable> selectDbTableListByNames(@Param("tableNames") String[] tableNames, @Param("dbName") String dbName);
/**
*
*
* @return
*/
List<GenTable> selectGenTableAll ();
public List<GenTable> selectGenTableAll();
/**
* ID
*
* @param id ID
*
* @return
*/
GenTable selectGenTableById (Long id);
public GenTable selectGenTableById(@Param("id") Long id);
/**
*
*
* @param tableName
*
* @return
*/
GenTable selectGenTableByName (String tableName);
public GenTable selectGenTableByName(@Param("tableName") String tableName);
/**
*
*
* @param genTable
*
* @return
*/
int insertGenTable (GenTable genTable);
public int insertGenTable(GenTable genTable);
/**
*
*
* @param genTable
*
* @return
*/
int updateGenTable (GenTable genTable);
public int updateGenTable(GenTable genTable);
/**
*
*
* @param ids ID
*
* @return
*/
int deleteGenTableByIds (Long[] ids);
public int deleteGenTableByIds(@Param("ids") Long[] ids);
List<String> selDbNameAll();
List<GenTableResp> selectDbTableListAll();
}

View File

@ -3,7 +3,7 @@ package com.muyu.gen.service;
import com.muyu.common.core.text.Convert;
import com.muyu.gen.domain.GenTableColumn;
import com.muyu.gen.mapper.GenTableColumnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
@ -11,22 +11,23 @@ import java.util.List;
/**
*
*
* @author muyu
* @author ruoyi
*/
@Service
public class GenTableColumnServiceImpl implements IGenTableColumnService {
@Autowired
public class GenTableColumnServiceImpl implements IGenTableColumnService
{
@Resource
private GenTableColumnMapper genTableColumnMapper;
/**
*
*
* @param tableId
*
* @return
*/
@Override
public List<GenTableColumn> selectGenTableColumnListByTableId (Long tableId) {
public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId)
{
return genTableColumnMapper.selectGenTableColumnListByTableId(tableId);
}
@ -34,11 +35,11 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
*
*
* @param genTableColumn
*
* @return
*/
@Override
public int insertGenTableColumn (GenTableColumn genTableColumn) {
public int insertGenTableColumn(GenTableColumn genTableColumn)
{
return genTableColumnMapper.insertGenTableColumn(genTableColumn);
}
@ -46,11 +47,11 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
*
*
* @param genTableColumn
*
* @return
*/
@Override
public int updateGenTableColumn (GenTableColumn genTableColumn) {
public int updateGenTableColumn(GenTableColumn genTableColumn)
{
return genTableColumnMapper.updateGenTableColumn(genTableColumn);
}
@ -58,11 +59,16 @@ public class GenTableColumnServiceImpl implements IGenTableColumnService {
*
*
* @param ids ID
*
* @return
*/
@Override
public int deleteGenTableColumnByIds (String ids) {
public int deleteGenTableColumnByIds(String ids)
{
return genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids));
}
@Override
public List<GenTableColumn> selectDbTableColumnsByName(String table, String dbName) {
return genTableColumnMapper.selectDbTableColumnsByName(table,dbName);
}
}

View File

@ -10,6 +10,7 @@ import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.gen.domain.GenTable;
import com.muyu.gen.domain.GenTableColumn;
import com.muyu.gen.domain.GenTableResp;
import com.muyu.gen.mapper.GenTableColumnMapper;
import com.muyu.gen.mapper.GenTableMapper;
import com.muyu.gen.util.GenUtils;
@ -22,7 +23,7 @@ import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -41,43 +42,28 @@ import java.util.zip.ZipOutputStream;
/**
*
*
* @author muyu
* @author ruoyi
*/
@Service
public class GenTableServiceImpl implements IGenTableService {
public class GenTableServiceImpl implements IGenTableService
{
private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class);
@Autowired
@Resource
private GenTableMapper genTableMapper;
@Autowired
@Resource
private GenTableColumnMapper genTableColumnMapper;
/**
*
*
* @param table
* @param template
*
* @return
*/
public static String getGenPath (GenTable table, String template) {
String genPath = table.getGenPath();
if (StringUtils.equals(genPath, "/")) {
return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
}
return genPath + File.separator + VelocityUtils.getFileName(template, table);
}
/**
*
*
* @param id ID
*
* @return
*/
@Override
public GenTable selectGenTableById (Long id) {
public GenTable selectGenTableById(Long id)
{
GenTable genTable = genTableMapper.selectGenTableById(id);
setTableFromOptions(genTable);
return genTable;
@ -87,11 +73,11 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param genTable
*
* @return
*/
@Override
public List<GenTable> selectGenTableList (GenTable genTable) {
public List<GenTable> selectGenTableList(GenTable genTable)
{
return genTableMapper.selectGenTableList(genTable);
}
@ -99,11 +85,11 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param genTable
*
* @return
*/
@Override
public List<GenTable> selectDbTableList (GenTable genTable) {
public List<GenTable> selectDbTableList(GenTable genTable)
{
return genTableMapper.selectDbTableList(genTable);
}
@ -111,12 +97,13 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param tableNames
*
* @param dbName
* @return
*/
@Override
public List<GenTable> selectDbTableListByNames (String[] tableNames) {
return genTableMapper.selectDbTableListByNames(tableNames);
public List<GenTable> selectDbTableListByNames(String[] tableNames, String dbName)
{
return genTableMapper.selectDbTableListByNames(tableNames,dbName);
}
/**
@ -125,7 +112,8 @@ public class GenTableServiceImpl implements IGenTableService {
* @return
*/
@Override
public List<GenTable> selectGenTableAll () {
public List<GenTable> selectGenTableAll()
{
return genTableMapper.selectGenTableAll();
}
@ -133,17 +121,19 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param genTable
*
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void updateGenTable (GenTable genTable) {
public void updateGenTable(GenTable genTable)
{
String options = JSON.toJSONString(genTable.getParams());
genTable.setOptions(options);
int row = genTableMapper.updateGenTable(genTable);
if (row > 0) {
for (GenTableColumn cenTableColumn : genTable.getColumns()) {
if (row > 0)
{
for (GenTableColumn cenTableColumn : genTable.getColumns())
{
genTableColumnMapper.updateGenTableColumn(cenTableColumn);
}
}
@ -153,12 +143,12 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param tableIds ID
*
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteGenTableByIds (Long[] tableIds) {
public void deleteGenTableByIds(Long[] tableIds)
{
genTableMapper.deleteGenTableByIds(tableIds);
genTableColumnMapper.deleteGenTableColumnByIds(tableIds);
}
@ -170,23 +160,31 @@ public class GenTableServiceImpl implements IGenTableService {
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void importGenTable (List<GenTable> tableList) {
public void importGenTable(List<GenTable> tableList)
{
String operName = SecurityUtils.getUsername();
try {
for (GenTable table : tableList) {
try
{
for (GenTable table : tableList)
{
String dbName = table.getDbName();
String tableName = table.getTableName();
GenUtils.initTable(table, operName);
int row = genTableMapper.insertGenTable(table);
if (row > 0) {
if (row > 0)
{
// 保存列信息
List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
for (GenTableColumn column : genTableColumns) {
List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName, dbName);
for (GenTableColumn column : genTableColumns)
{
GenUtils.initColumnField(column, table);
genTableColumnMapper.insertGenTableColumn(column);
}
}
}
} catch (Exception e) {
}
catch (Exception e)
{
throw new ServiceException("导入失败:" + e.getMessage());
}
}
@ -195,11 +193,11 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param tableId
*
* @return
*/
@Override
public Map<String, String> previewCode (Long tableId) {
public Map<String, String> previewCode(Long tableId)
{
Map<String, String> dataMap = new LinkedHashMap<>();
// 查询表信息
GenTable table = genTableMapper.selectGenTableById(tableId);
@ -213,7 +211,8 @@ public class GenTableServiceImpl implements IGenTableService {
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates) {
for (String template : templates)
{
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
@ -227,11 +226,11 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param tableName
*
* @return
*/
@Override
public byte[] downloadCode (String tableName) {
public byte[] downloadCode(String tableName)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
generatorCode(tableName, zip);
@ -245,7 +244,8 @@ public class GenTableServiceImpl implements IGenTableService {
* @param tableName
*/
@Override
public void generatorCode (String tableName) {
public void generatorCode(String tableName)
{
// 查询表信息
GenTable table = genTableMapper.selectGenTableByName(tableName);
// 设置主子表信息
@ -259,16 +259,21 @@ public class GenTableServiceImpl implements IGenTableService {
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates) {
if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) {
for (String template : templates)
{
if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
{
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(context, sw);
try {
try
{
String path = getGenPath(table, template);
FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
} catch (IOException e) {
}
catch (IOException e)
{
throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
}
}
@ -279,45 +284,54 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param tableName
* @param dbName
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void synchDb (String tableName) {
public void synchDb(String tableName, String dbName)
{
GenTable table = genTableMapper.selectGenTableByName(tableName);
List<GenTableColumn> tableColumns = table.getColumns();
Map<String, GenTableColumn> tableColumnMap = tableColumns.stream().collect(Collectors.toMap(GenTableColumn::getColumnName, Function.identity()));
List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
if (StringUtils.isEmpty(dbTableColumns)) {
List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName, dbName);
if (StringUtils.isEmpty(dbTableColumns))
{
throw new ServiceException("同步数据失败,原表结构不存在");
}
List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
dbTableColumns.forEach(column -> {
GenUtils.initColumnField(column, table);
if (tableColumnMap.containsKey(column.getColumnName())) {
if (tableColumnMap.containsKey(column.getColumnName()))
{
GenTableColumn prevColumn = tableColumnMap.get(column.getColumnName());
column.setColumnId(prevColumn.getColumnId());
if (column.isList()) {
if (column.isList())
{
// 如果是列表,继续保留查询方式/字典类型选项
column.setDictType(prevColumn.getDictType());
column.setQueryType(prevColumn.getQueryType());
}
if (StringUtils.isNotEmpty(prevColumn.getIsRequired()) && !column.isPk()
&& (column.isInsert() || column.isEdit())
&& ((column.isUsableColumn()) || (!column.isSuperColumn()))) {
&& ((column.isUsableColumn()) || (!column.isSuperColumn())))
{
// 如果是(新增/修改&非主键/非忽略及父属性),继续保留必填/显示类型选项
column.setIsRequired(prevColumn.getIsRequired());
column.setHtmlType(prevColumn.getHtmlType());
}
genTableColumnMapper.updateGenTableColumn(column);
} else {
}
else
{
genTableColumnMapper.insertGenTableColumn(column);
}
});
List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
if (StringUtils.isNotEmpty(delColumns)) {
if (StringUtils.isNotEmpty(delColumns))
{
genTableColumnMapper.deleteGenTableColumns(delColumns);
}
}
@ -326,14 +340,15 @@ public class GenTableServiceImpl implements IGenTableService {
*
*
* @param tableNames
*
* @return
*/
@Override
public byte[] downloadCode (String[] tableNames) {
public byte[] downloadCode(String[] tableNames)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
for (String tableName : tableNames) {
for (String tableName : tableNames)
{
generatorCode(tableName, zip);
}
IOUtils.closeQuietly(zip);
@ -343,7 +358,8 @@ public class GenTableServiceImpl implements IGenTableService {
/**
*
*/
private void generatorCode (String tableName, ZipOutputStream zip) {
private void generatorCode(String tableName, ZipOutputStream zip)
{
// 查询表信息
GenTable table = genTableMapper.selectGenTableByName(tableName);
// 设置主子表信息
@ -357,19 +373,23 @@ public class GenTableServiceImpl implements IGenTableService {
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates) {
for (String template : templates)
{
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(context, sw);
try {
try
{
// 添加到zip
zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
IOUtils.write(sw.toString(), zip, Constants.UTF8);
IOUtils.closeQuietly(sw);
zip.flush();
zip.closeEntry();
} catch (IOException e) {
}
catch (IOException e)
{
log.error("渲染模板失败,表名:" + table.getTableName(), e);
}
}
@ -381,49 +401,80 @@ public class GenTableServiceImpl implements IGenTableService {
* @param genTable
*/
@Override
public void validateEdit (GenTable genTable) {
if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {
public void validateEdit(GenTable genTable)
{
if (GenConstants.TPL_TREE.equals(genTable.getTplCategory()))
{
String options = JSON.toJSONString(genTable.getParams());
JSONObject paramsObj = JSON.parseObject(options);
if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) {
if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE)))
{
throw new ServiceException("树编码字段不能为空");
} else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) {
}
else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE)))
{
throw new ServiceException("树父编码字段不能为空");
} else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) {
}
else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME)))
{
throw new ServiceException("树名称字段不能为空");
} else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) {
if (StringUtils.isEmpty(genTable.getSubTableName())) {
}
else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory()))
{
if (StringUtils.isEmpty(genTable.getSubTableName()))
{
throw new ServiceException("关联子表的表名不能为空");
} else if (StringUtils.isEmpty(genTable.getSubTableFkName())) {
}
else if (StringUtils.isEmpty(genTable.getSubTableFkName()))
{
throw new ServiceException("子表关联的外键名不能为空");
}
}
}
}
@Override
public List<String> selDbNameAll() {
return genTableMapper.selDbNameAll();
}
@Override
public List<GenTableResp> selectDbTableListAll() {
return genTableMapper.selectDbTableListAll();
}
/**
*
*
* @param table
*/
public void setPkColumn (GenTable table) {
for (GenTableColumn column : table.getColumns()) {
if (column.isPk()) {
public void setPkColumn(GenTable table)
{
for (GenTableColumn column : table.getColumns())
{
if (column.isPk())
{
table.setPkColumn(column);
break;
}
}
if (StringUtils.isNull(table.getPkColumn())) {
if (StringUtils.isNull(table.getPkColumn()))
{
table.setPkColumn(table.getColumns().get(0));
}
if (GenConstants.TPL_SUB.equals(table.getTplCategory())) {
for (GenTableColumn column : table.getSubTable().getColumns()) {
if (column.isPk()) {
if (GenConstants.TPL_SUB.equals(table.getTplCategory()))
{
for (GenTableColumn column : table.getSubTable().getColumns())
{
if (column.isPk())
{
table.getSubTable().setPkColumn(column);
break;
}
}
if (StringUtils.isNull(table.getSubTable().getPkColumn())) {
if (StringUtils.isNull(table.getSubTable().getPkColumn()))
{
table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0));
}
}
@ -434,9 +485,11 @@ public class GenTableServiceImpl implements IGenTableService {
*
* @param table
*/
public void setSubTable (GenTable table) {
public void setSubTable(GenTable table)
{
String subTableName = table.getSubTableName();
if (StringUtils.isNotEmpty(subTableName)) {
if (StringUtils.isNotEmpty(subTableName))
{
table.setSubTable(genTableMapper.selectGenTableByName(subTableName));
}
}
@ -446,9 +499,11 @@ public class GenTableServiceImpl implements IGenTableService {
*
* @param genTable
*/
public void setTableFromOptions (GenTable genTable) {
public void setTableFromOptions(GenTable genTable)
{
JSONObject paramsObj = JSON.parseObject(genTable.getOptions());
if (StringUtils.isNotNull(paramsObj)) {
if (StringUtils.isNotNull(paramsObj))
{
String treeCode = paramsObj.getString(GenConstants.TREE_CODE);
String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE);
String treeName = paramsObj.getString(GenConstants.TREE_NAME);
@ -462,4 +517,21 @@ public class GenTableServiceImpl implements IGenTableService {
genTable.setParentMenuName(parentMenuName);
}
}
/**
*
*
* @param table
* @param template
* @return
*/
public static String getGenPath(GenTable table, String template)
{
String genPath = table.getGenPath();
if (StringUtils.equals(genPath, "/"))
{
return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
}
return genPath + File.separator + VelocityUtils.getFileName(template, table);
}
}

View File

@ -7,42 +7,41 @@ import java.util.List;
/**
*
*
* @author muyu
* @author ruoyi
*/
public interface IGenTableColumnService {
public interface IGenTableColumnService
{
/**
*
*
* @param tableId
*
* @return
*/
List<GenTableColumn> selectGenTableColumnListByTableId (Long tableId);
public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId);
/**
*
*
* @param genTableColumn
*
* @return
*/
int insertGenTableColumn (GenTableColumn genTableColumn);
public int insertGenTableColumn(GenTableColumn genTableColumn);
/**
*
*
* @param genTableColumn
*
* @return
*/
int updateGenTableColumn (GenTableColumn genTableColumn);
public int updateGenTableColumn(GenTableColumn genTableColumn);
/**
*
*
* @param ids ID
*
* @return
*/
int deleteGenTableColumnByIds (String ids);
public int deleteGenTableColumnByIds(String ids);
List<GenTableColumn> selectDbTableColumnsByName(String table, String dbName);
}

View File

@ -1,6 +1,7 @@
package com.muyu.gen.service;
import com.muyu.gen.domain.GenTable;
import com.muyu.gen.domain.GenTableResp;
import java.util.List;
import java.util.Map;
@ -8,124 +9,121 @@ import java.util.Map;
/**
*
*
* @author muyu
* @author ruoyi
*/
public interface IGenTableService {
public interface IGenTableService
{
/**
*
*
* @param genTable
*
* @return
*/
List<GenTable> selectGenTableList (GenTable genTable);
public List<GenTable> selectGenTableList(GenTable genTable);
/**
*
*
* @param genTable
*
* @return
*/
List<GenTable> selectDbTableList (GenTable genTable);
public List<GenTable> selectDbTableList(GenTable genTable);
/**
*
*
* @param tableNames
*
* @param dbName
* @return
*/
List<GenTable> selectDbTableListByNames (String[] tableNames);
public List<GenTable> selectDbTableListByNames(String[] tableNames, String dbName);
/**
*
*
* @return
*/
List<GenTable> selectGenTableAll ();
public List<GenTable> selectGenTableAll();
/**
*
*
* @param id ID
*
* @return
*/
GenTable selectGenTableById (Long id);
public GenTable selectGenTableById(Long id);
/**
*
*
* @param genTable
*
* @return
*/
void updateGenTable (GenTable genTable);
public void updateGenTable(GenTable genTable);
/**
*
*
* @param tableIds ID
*
* @return
*/
void deleteGenTableByIds (Long[] tableIds);
public void deleteGenTableByIds(Long[] tableIds);
/**
*
*
* @param tableList
*/
void importGenTable (List<GenTable> tableList);
public void importGenTable(List<GenTable> tableList);
/**
*
*
* @param tableId
*
* @return
*/
Map<String, String> previewCode (Long tableId);
public Map<String, String> previewCode(Long tableId);
/**
*
*
* @param tableName
*
* @return
*/
byte[] downloadCode (String tableName);
public byte[] downloadCode(String tableName);
/**
*
*
* @param tableName
*
* @return
*/
void generatorCode (String tableName);
public void generatorCode(String tableName);
/**
*
*
* @param tableName
* @param dbName
*/
void synchDb (String tableName);
public void synchDb(String tableName, String dbName);
/**
*
*
* @param tableNames
*
* @return
*/
byte[] downloadCode (String[] tableNames);
public byte[] downloadCode(String[] tableNames);
/**
*
*
* @param genTable
*/
void validateEdit (GenTable genTable);
public void validateEdit(GenTable genTable);
List<String> selDbNameAll();
List<GenTableResp> selectDbTableListAll();
}

View File

@ -1,16 +1,20 @@
# Tomcat
server:
port: 9202
port: 9709
# nacos线上地址
nacos:
addr: 47.116.173.119:8848
user-name: nacos
password: nacos
namespace: five
namespace: four
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
# Spring
spring:
amqp:
deserialization:
trust:
all: true
main:
allow-bean-definition-overriding: true
application:
@ -49,3 +53,8 @@ spring:
- application-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# xxl-job 配置文件
- application-xxl-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# rabbit 配置文件
- application-rabbit-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
logging:
level:
com.muyu.system.mapper: DEBUG

View File

@ -30,29 +30,7 @@
</resultMap>
<sql id="selectGenTableColumnVo">
select column_id,
table_id,
column_name,
column_comment,
column_type,
java_type,
java_field,
is_pk,
is_increment,
is_required,
is_insert,
is_edit,
is_list,
is_query,
query_type,
html_type,
dict_type,
sort,
create_by,
create_time,
update_by,
update_time
from gen_table_column
select column_id, table_id, column_name, column_comment, column_type, java_type, java_field, is_pk, is_increment, is_required, is_insert, is_edit, is_list, is_query, query_type, html_type, dict_type, sort, create_by, create_time, update_by, update_time from gen_table_column
</sql>
<select id="selectGenTableColumnListByTableId" parameterType="com.muyu.gen.domain.GenTableColumn" resultMap="GenTableColumnResult">
@ -62,15 +40,9 @@
</select>
<select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult">
select column_name,
(case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else null end) as is_required,
(case when column_key = 'PRI' then '1' else '0' end) as is_pk,
ordinal_position as sort,
column_comment,
(case when extra = 'auto_increment' then '1' else '0' end) as is_increment,
column_type
from information_schema.columns
where table_schema = (select database())
select column_name, (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else null end) as is_required, (case when column_key = 'PRI' then '1' else '0' end) as is_pk, ordinal_position as sort, column_comment, (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type
from information_schema.columns where
<include refid="com.muyu.gen.mapper.GenTableMapper.select_dbName"/>
and table_name = (#{tableName})
order by ordinal_position
</select>
@ -141,17 +113,20 @@
</update>
<delete id="deleteGenTableColumnByIds" parameterType="Long">
delete from gen_table_column where table_id in
<foreach collection="array" item="tableId" open="(" separator="," close=")">
delete from gen_table_column where table_id in (
<foreach collection="ids" item="tableId" separator="," >
#{tableId}
</foreach>
)
</delete>
<delete id="deleteGenTableColumns">
delete from gen_table_column where column_id in
<foreach collection="list" item="item" open="(" separator="," close=")">
delete from gen_table_column where column_id in (
<foreach collection="genTableColumns" item="item" separator="," >
#{item.columnId}
</foreach>
)
</delete>
</mapper>

View File

@ -6,6 +6,7 @@
<resultMap type="com.muyu.gen.domain.GenTable" id="GenTableResult">
<id property="tableId" column="table_id" />
<result property="dbName" column="db_name" />
<result property="tableName" column="table_name" />
<result property="tableComment" column="table_comment" />
<result property="subTableName" column="sub_table_name" />
@ -54,27 +55,18 @@
</resultMap>
<sql id="selectGenTableVo">
select table_id,
table_name,
table_comment,
sub_table_name,
sub_table_fk_name,
class_name,
tpl_category,
package_name,
module_name,
business_name,
function_name,
function_author,
gen_type,
gen_path,
options,
create_by,
create_time,
update_by,
update_time,
remark
from gen_table
select table_id, db_name, table_name, table_comment, sub_table_name, sub_table_fk_name, class_name, tpl_category, package_name, module_name, business_name, function_name, function_author, gen_type, gen_path, options, create_by, create_time, update_by, update_time, remark from gen_table
</sql>
<sql id="select_dbName">
<choose>
<when test="dbName!=null and dbName!=''">
table_schema in (#{dbName})
</when>
<otherwise>
table_schema = (select database())
</otherwise>
</choose>
</sql>
<select id="selectGenTableList" parameterType="com.muyu.gen.domain.GenTable" resultMap="GenTableResult">
@ -97,7 +89,8 @@
<select id="selectDbTableList" parameterType="com.muyu.gen.domain.GenTable" resultMap="GenTableResult">
select table_name, table_comment, create_time, update_time from information_schema.tables
where table_schema = (select database())
where
<include refid="select_dbName"/>
AND table_name NOT LIKE 'qrtz_%' AND table_name NOT LIKE 'gen_%'
AND table_name NOT IN (select table_name from gen_table)
<if test="tableName != null and tableName != ''">
@ -116,141 +109,63 @@
</select>
<select id="selectDbTableListByNames" resultMap="GenTableResult">
select table_name, table_comment, create_time, update_time from information_schema.tables
where table_name NOT LIKE 'qrtz_%' and table_name NOT LIKE 'gen_%' and table_schema = (select database())
select table_name, table_schema db_name, table_comment, create_time, update_time from information_schema.tables
where table_name NOT LIKE 'qrtz_%' and table_name NOT LIKE 'gen_%'
and <include refid="select_dbName"/>
and table_name in
<foreach collection="array" item="name" open="(" separator="," close=")">
<foreach collection="tableNames" item="name" open="(" separator="," close=")">
#{name}
</foreach>
</select>
<select id="selectTableByName" parameterType="String" resultMap="GenTableResult">
select table_name, table_comment, create_time, update_time
from information_schema.tables
where table_comment <![CDATA[ <> ]]> ''
and table_schema = (select database())
select table_name, table_comment, create_time, update_time from information_schema.tables
where table_comment <![CDATA[ <> ]]> '' and table_schema = (select database())
and table_name = #{tableName}
</select>
<select id="selectGenTableById" parameterType="Long" resultMap="GenTableResult">
SELECT t.table_id,
t.table_name,
t.table_comment,
t.sub_table_name,
t.sub_table_fk_name,
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,
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
SELECT t.table_id, t.table_name, t.table_comment, t.sub_table_name, t.sub_table_fk_name, 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,
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
FROM gen_table t
LEFT JOIN gen_table_column c ON t.table_id = c.table_id
where t.table_id = #{tableId}
order by c.sort
where t.table_id = #{id} order by c.sort
</select>
<select id="selectGenTableByName" parameterType="String" resultMap="GenTableResult">
SELECT t.table_id,
t.table_name,
t.table_comment,
t.sub_table_name,
t.sub_table_fk_name,
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,
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
SELECT t.table_id, t.table_name, t.table_comment, t.sub_table_name, t.sub_table_fk_name, 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,
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
FROM gen_table t
LEFT JOIN gen_table_column c ON t.table_id = c.table_id
where t.table_name = #{tableName}
order by c.sort
where t.table_name = #{tableName} order by c.sort
</select>
<select id="selectGenTableAll" parameterType="String" resultMap="GenTableResult">
SELECT t.table_id,
t.table_name,
t.table_comment,
t.sub_table_name,
t.sub_table_fk_name,
t.class_name,
t.tpl_category,
t.package_name,
t.module_name,
t.business_name,
t.function_name,
t.function_author,
t.options,
t.remark,
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
SELECT t.table_id, t.table_name, t.table_comment, t.sub_table_name, t.sub_table_fk_name, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.options, t.remark,
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
FROM gen_table t
LEFT JOIN gen_table_column c ON t.table_id = c.table_id
order by c.sort
</select>
<select id="selDbNameAll" resultType="java.lang.String">
select table_schema from information_schema.tables
WHERE table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
GROUP BY table_schema
</select>
<select id="selFilterableByDbNameAndTableName" resultType="java.lang.String">
</select>
<select id="selectDbTableListAll" resultType="com.muyu.gen.domain.GenTableResp">
select table_name,table_comment,table_schema db_name from information_schema.tables
where table_name NOT LIKE 'qrtz_%' and table_name NOT LIKE 'gen_%'
and table_schema in (select table_schema from information_schema.tables
WHERE table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
GROUP BY table_schema)
</select>
<insert id="insertGenTable" parameterType="com.muyu.gen.domain.GenTable" useGeneratedKeys="true" keyProperty="tableId">
insert into gen_table (
<if test="dbName != null">db_name,</if>
<if test="tableName != null">table_name,</if>
<if test="tableComment != null and tableComment != ''">table_comment,</if>
<if test="className != null and className != ''">class_name,</if>
@ -266,6 +181,7 @@
<if test="createBy != null and createBy != ''">create_by,</if>
create_time
)values(
<if test="dbName != null">#{dbName},</if>
<if test="tableName != null">#{tableName},</if>
<if test="tableComment != null and tableComment != ''">#{tableComment},</if>
<if test="className != null and className != ''">#{className},</if>
@ -286,6 +202,7 @@
<update id="updateGenTable" parameterType="com.muyu.gen.domain.GenTable">
update gen_table
<set>
<if test="dbName != null">db_name = #{dbName},</if>
<if test="tableName != null">table_name = #{tableName},</if>
<if test="tableComment != null and tableComment != ''">table_comment = #{tableComment},</if>
<if test="subTableName != null">sub_table_name = #{subTableName},</if>
@ -294,8 +211,7 @@
<if test="functionAuthor != null and functionAuthor != ''">function_author = #{functionAuthor},</if>
<if test="genType != null and genType != ''">gen_type = #{genType},</if>
<if test="genPath != null and genPath != ''">gen_path = #{genPath},</if>
<if test="tplCategory != null and tplCategory != ''">tpl_category = #{tplCategory},</if>
<if test="packageName != null and packageName != ''">package_name = #{packageName},</if>
<if test="tplCategory != null and tplCategory != ''">tpl_category = #{tplCategory},</if> <if test="packageName != null and packageName != ''">package_name = #{packageName},</if>
<if test="moduleName != null and moduleName != ''">module_name = #{moduleName},</if>
<if test="businessName != null and businessName != ''">business_name = #{businessName},</if>
<if test="functionName != null and functionName != ''">function_name = #{functionName},</if>
@ -308,10 +224,11 @@
</update>
<delete id="deleteGenTableByIds" parameterType="Long">
delete from gen_table where table_id in
<foreach collection="array" item="tableId" open="(" separator="," close=")">
delete from gen_table where table_id in (
<foreach collection="ids" item="tableId" separator="," >
#{tableId}
</foreach>
)
</delete>
</mapper>

View File

@ -1,9 +1,9 @@
package ${packageName}.controller;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
@ -12,14 +12,14 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.I${ClassName}Service;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
#if($table.crud || $table.sub)
import com.muyu.common.core.web.page.TableDataInfo;
#elseif($table.tree)
@ -35,7 +35,7 @@ import com.muyu.common.core.web.page.TableDataInfo;
@RequestMapping("/${businessName}")
public class ${ClassName}Controller extends BaseController
{
@Autowired
@Resource
private I${ClassName}Service ${className}Service;
/**
@ -44,14 +44,14 @@ public class ${ClassName}Controller extends BaseController
@RequiresPermissions("${permissionPrefix}:list")
@GetMapping("/list")
#if($table.crud || $table.sub)
public Result<TableDataInfo> list(${ClassName} ${className})
public Result<TableDataInfo<${ClassName}>> list(${ClassName} ${className})
{
startPage();
List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
return getDataTable(list);
}
#elseif($table.tree)
public Result list(${ClassName} ${className})
public Result<${ClassName}> list(${ClassName} ${className})
{
List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
return success(list);
@ -62,7 +62,6 @@ public class ${ClassName}Controller extends BaseController
* 导出${functionName}列表
*/
@RequiresPermissions("${permissionPrefix}:export")
@Log(title = "${functionName}", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, ${ClassName} ${className})
{
@ -76,7 +75,7 @@ public class ${ClassName}Controller extends BaseController
*/
@RequiresPermissions("${permissionPrefix}:query")
@GetMapping(value = "/{${pkColumn.javaField}}")
public Result getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField})
public Result<List<${ClassName}>> getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField})
{
return success(${className}Service.select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaField}));
}
@ -85,32 +84,40 @@ public class ${ClassName}Controller extends BaseController
* 新增${functionName}
*/
@RequiresPermissions("${permissionPrefix}:add")
@Log(title = "${functionName}", businessType = BusinessType.INSERT)
@PostMapping
public Result add(@RequestBody ${ClassName} ${className})
public Result<Integer> add(
@Validated @RequestBody ${ClassName} ${className})
{
return toAjax(${className}Service.insert${ClassName}(${className}));
if (${className}Service.checkIdUnique(${className})) {
return error("新增 ${functionName} '" + ${className} + "'失败,${functionName}已存在");
}
${className}.setCreateBy(SecurityUtils.getUsername());
return toAjax(${className}Service.save(${className}));
}
/**
* 修改${functionName}
*/
@RequiresPermissions("${permissionPrefix}:edit")
@Log(title = "${functionName}", businessType = BusinessType.UPDATE)
@PutMapping
public Result edit(@RequestBody ${ClassName} ${className})
public Result<Integer> edit(
@Validated @RequestBody ${ClassName} ${className})
{
return toAjax(${className}Service.update${ClassName}(${className}));
if (!${className}Service.checkIdUnique(${className})) {
return error("修改 ${functionName} '" + ${className} + "'失败,${functionName}不存在");
}
${className}.setUpdateBy(SecurityUtils.getUsername());
return toAjax(${className}Service.updateById(${className}));
}
/**
* 删除${functionName}
*/
@RequiresPermissions("${permissionPrefix}:remove")
@Log(title = "${functionName}", businessType = BusinessType.DELETE)
@DeleteMapping("/{${pkColumn.javaField}s}")
public Result remove(@PathVariable("${pkColumn.javaField}s") ${pkColumn.javaType}[] ${pkColumn.javaField}s)
public Result<Integer> remove(@PathVariable("${pkColumn.javaField}s") ${pkColumn.javaType}[] ${pkColumn.javaField}s)
{
return toAjax(${className}Service.delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaField}s));
${className}Service.removeBatchByIds(Arrays.asList(${pkColumn.javaField}s));
return success();
}
}

View File

@ -9,6 +9,13 @@ import com.muyu.common.core.web.domain.BaseEntity;
#elseif($table.tree)
import com.muyu.common.core.web.domain.TreeEntity;
#end
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
/**
* ${functionName}对象 ${tableName}
@ -16,13 +23,20 @@ import com.muyu.common.core.web.domain.TreeEntity;
* @author ${author}
* @date ${datetime}
*/
#if($table.crud || $table.sub)
#set($Entity="BaseEntity")
#elseif($table.tree)
#set($Entity="TreeEntity")
#end
public class ${ClassName} extends ${Entity}
{
@Data
@Setter
@Getter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@TableName("${tableName}")
public class ${ClassName} extends ${Entity}{
private static final long serialVersionUID = 1L;
#foreach ($column in $columns)
@ -43,34 +57,20 @@ public class ${ClassName} extends ${Entity}
#else
@Excel(name = "${comment}")
#end
#end
#if($column.javaField == $pkColumn.javaField)
@TableId( type = IdType.AUTO)
#end
private $column.javaType $column.javaField;
#end
#end
#if($table.sub)
/** $table.subTable.functionName信息 */
private List<${subClassName}> ${subclassName}List;
#end
#foreach ($column in $columns)
#if(!$table.isSuperColumn($column.javaField))
#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]"))
#set($AttrName=$column.javaField)
#else
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#end
public void set${AttrName}($column.javaType $column.javaField)
{
this.$column.javaField = $column.javaField;
}
public $column.javaType get${AttrName}()
{
return $column.javaField;
}
#end
#end
#if($table.sub)
public List<${subClassName}> get${subClassName}List()

View File

@ -5,6 +5,8 @@ import ${packageName}.domain.${ClassName};
#if($table.sub)
import ${packageName}.domain.${subClassName};
#end
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* ${functionName}Mapper接口
@ -12,80 +14,7 @@ import ${packageName}.domain.${subClassName};
* @author ${author}
* @date ${datetime}
*/
public interface ${ClassName}Mapper
{
/**
* 查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
*/
public ${ClassName} select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField});
@Mapper
public interface ${ClassName}Mapper extends BaseMapper<${ClassName}>{
/**
* 查询${functionName}列表
*
* @param ${className} ${functionName}
* @return ${functionName}集合
*/
public List<${ClassName}> select${ClassName}List(${ClassName} ${className});
/**
* 新增${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int insert${ClassName}(${ClassName} ${className});
/**
* 修改${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int update${ClassName}(${ClassName} ${className});
/**
* 删除${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
public int delete${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField});
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的数据主键集合
* @return 结果
*/
public int delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaType}[] ${pkColumn.javaField}s);
#if($table.sub)
/**
* 批量删除${subTable.functionName}
*
* @param ${pkColumn.javaField}s 需要删除的数据主键集合
* @return 结果
*/
public int delete${subClassName}By${subTableFkClassName}s(${pkColumn.javaType}[] ${pkColumn.javaField}s);
/**
* 批量新增${subTable.functionName}
*
* @param ${subclassName}List ${subTable.functionName}列表
* @return 结果
*/
public int batch${subClassName}(List<${subClassName}> ${subclassName}List);
/**
* 通过${functionName}主键删除${subTable.functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}ID
* @return 结果
*/
public int delete${subClassName}By${subTableFkClassName}(${pkColumn.javaType} ${pkColumn.javaField});
#end
}

View File

@ -2,6 +2,7 @@ package ${packageName}.service;
import java.util.List;
import ${packageName}.domain.${ClassName};
import com.baomidou.mybatisplus.extension.service.IService;
/**
* ${functionName}Service接口
@ -9,10 +10,9 @@ import ${packageName}.domain.${ClassName};
* @author ${author}
* @date ${datetime}
*/
public interface I${ClassName}Service
{
public interface I${ClassName}Service extends IService<${ClassName}> {
/**
* 查询${functionName}
* 精确查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
@ -28,34 +28,10 @@ public interface I${ClassName}Service
public List<${ClassName}> select${ClassName}List(${ClassName} ${className});
/**
* 新增${functionName}
*
* 判断 ${functionName} id是否唯一
* @param ${className} ${functionName}
* @return 结果
*/
public int insert${ClassName}(${ClassName} ${className});
Boolean checkIdUnique(${ClassName} ${className});
/**
* 修改${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int update${ClassName}(${ClassName} ${className});
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的${functionName}主键集合
* @return 结果
*/
public int delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaType}[] ${pkColumn.javaField}s);
/**
* 删除${functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
public int delete${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField});
}

View File

@ -7,7 +7,6 @@ import com.muyu.common.core.utils.DateUtils;
#break
#end
#end
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#if($table.sub)
import java.util.ArrayList;
@ -18,6 +17,10 @@ import ${packageName}.domain.${subClassName};
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.I${ClassName}Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import org.springframework.util.Assert;
/**
* ${functionName}Service业务层处理
@ -26,13 +29,12 @@ import ${packageName}.service.I${ClassName}Service;
* @date ${datetime}
*/
@Service
public class ${ClassName}ServiceImpl implements I${ClassName}Service
{
@Autowired
private ${ClassName}Mapper ${className}Mapper;
public class ${ClassName}ServiceImpl
extends ServiceImpl<${ClassName}Mapper, ${ClassName}>
implements I${ClassName}Service {
/**
* 查询${functionName}
* 精确查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
@ -40,9 +42,13 @@ public class ${ClassName}ServiceImpl implements I${ClassName}Service
@Override
public ${ClassName} select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField})
{
return ${className}Mapper.select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaField});
LambdaQueryWrapper<${ClassName}> queryWrapper = new LambdaQueryWrapper<>();
Assert.notNull(${pkColumn.javaField}, "${pkColumn.javaField}不可为空");
queryWrapper.eq(${ClassName}::get${pkColumn.capJavaField}, ${pkColumn.javaField});
return this.getOne(queryWrapper);
}
/**
* 查询${functionName}列表
*
@ -52,118 +58,58 @@ public class ${ClassName}ServiceImpl implements I${ClassName}Service
@Override
public List<${ClassName}> select${ClassName}List(${ClassName} ${className})
{
return ${className}Mapper.select${ClassName}List(${className});
}
/**
* 新增${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int insert${ClassName}(${ClassName} ${className})
{
LambdaQueryWrapper<${ClassName}> queryWrapper = new LambdaQueryWrapper<>();
#foreach($column in $columns)
#if($column.javaField == 'createTime')
${className}.setCreateTime(DateUtils.getNowDate());
#set($queryType=$column.queryType)
#set($javaField=$column.javaField)
#set($javaType=$column.javaType)
#set($columnName=$column.columnName)
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#if($column.query)
#if($column.queryType == "EQ")
if (StringUtils.isNotEmpty(${className}.get${AttrName}())){
queryWrapper.eq(${ClassName}::get${AttrName}, ${className}.get${AttrName}());
}
#elseif($queryType == "NE")
if (StringUtils.isNotEmpty(${className}.get${AttrName}())){
queryWrapper.ne(${ClassName}::get${AttrName}, ${className}.get${AttrName}());
}
#elseif($queryType == "GT")
if (StringUtils.isNotEmpty(${className}.get${AttrName}())){
queryWrapper.gt(${ClassName}::get${AttrName}, ${className}.get${AttrName}());
}
#elseif($queryType == "GTE")
if (StringUtils.isNotEmpty(${className}.get${AttrName}())){
queryWrapper.ge(${ClassName}::get${AttrName}, ${className}.get${AttrName}());
}
#elseif($queryType == "LT")
if (StringUtils.isNotEmpty(${className}.get${AttrName}())){
queryWrapper.lt(${ClassName}::get${AttrName}, ${className}.get${AttrName}());
}
#elseif($queryType == "LTE")
if (StringUtils.isNotEmpty(${className}.get${AttrName}())){
queryWrapper.le(${ClassName}::get${AttrName}, ${className}.get${AttrName}());
}
#elseif($queryType == "LIKE")
if (StringUtils.isNotEmpty(${className}.get${AttrName}())){
queryWrapper.like(${ClassName}::get${AttrName}, ${className}.get${AttrName}());
}
#end
#end
#if($table.sub)
int rows = ${className}Mapper.insert${ClassName}(${className});
insert${subClassName}(${className});
return rows;
#else
return ${className}Mapper.insert${ClassName}(${className});
#end
return this.list(queryWrapper);
}
/**
* 修改${functionName}
*
* 唯一 判断
* @param ${className} ${functionName}
* @return 结果
* @return ${functionName}
*/
#if($table.sub)
@Transactional
#end
@Override
public int update${ClassName}(${ClassName} ${className})
{
#foreach ($column in $columns)
#if($column.javaField == 'updateTime')
${className}.setUpdateTime(DateUtils.getNowDate());
#end
#end
#if($table.sub)
${className}Mapper.delete${subClassName}By${subTableFkClassName}(${className}.get${pkColumn.capJavaField}());
insert${subClassName}(${className});
#end
return ${className}Mapper.update${ClassName}(${className});
public Boolean checkIdUnique(${ClassName} ${className}) {
LambdaQueryWrapper<${ClassName}> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(${ClassName}::get${pkColumn.capJavaField}, ${className}.get${pkColumn.capJavaField}());
return this.count(queryWrapper) > 0;
}
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的${functionName}主键
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaType}[] ${pkColumn.javaField}s)
{
#if($table.sub)
${className}Mapper.delete${subClassName}By${subTableFkClassName}s(${pkColumn.javaField}s);
#end
return ${className}Mapper.delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaField}s);
}
/**
* 删除${functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int delete${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField})
{
#if($table.sub)
${className}Mapper.delete${subClassName}By${subTableFkClassName}(${pkColumn.javaField});
#end
return ${className}Mapper.delete${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaField});
}
#if($table.sub)
/**
* 新增${subTable.functionName}信息
*
* @param ${className} ${functionName}对象
*/
public void insert${subClassName}(${ClassName} ${className})
{
List<${subClassName}> ${subclassName}List = ${className}.get${subClassName}List();
${pkColumn.javaType} ${pkColumn.javaField} = ${className}.get${pkColumn.capJavaField}();
if (StringUtils.isNotNull(${subclassName}List))
{
List<${subClassName}> list = new ArrayList<${subClassName}>();
for (${subClassName} ${subclassName} : ${subclassName}List)
{
${subclassName}.set${subTableFkClassName}(${pkColumn.javaField});
list.add(${subclassName});
}
if (list.size() > 0)
{
${className}Mapper.batch${subClassName}(list);
}
}
}
#end
}

View File

@ -17,8 +17,7 @@ import com.muyu.common.core.web.domain.BaseEntity;
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class ${subClassName} extends BaseEntity
{
public class ${subClassName} extends BaseEntity {
private static final long serialVersionUID = 1L;
#foreach ($column in $subTable.columns)

View File

@ -353,7 +353,7 @@
</template>
<script>
import { list${BusinessName}, get${BusinessName}, del${BusinessName}, add${BusinessName}, update${BusinessName} } from "@/api/${moduleName}/${businessName}";
import { list${BusinessName}, get${BusinessName}, del${BusinessName}, add${BusinessName}, update${BusinessName} } from "/src/api/${moduleName}/${businessName}";
export default {
name: "${BusinessName}",
@ -448,8 +448,8 @@ export default {
#end
#end
list${BusinessName}(this.queryParams).then(response => {
this.${businessName}List = response.rows;
this.total = response.total;
this.${businessName}List = response.data.rows;
this.total = response.data.total;
this.loading = false;
});
},