refactor: 生成代码修改
This commit is contained in:
parent
3d4d256c9a
commit
b8ce1f3454
|
@ -1,27 +0,0 @@
|
|||
package cn.bunny.config;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@Component
|
||||
public class DatabaseMetadataHolder {
|
||||
@Getter
|
||||
private DatabaseMetaData metaData;
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws SQLException {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
this.metaData = connection.getMetaData();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package cn.bunny.config;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.apache.velocity.app.Velocity;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
public class VmsHolder {
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
Properties prop = new Properties();
|
||||
prop.put("file.resource.loader.class" , "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
|
||||
Velocity.init(prop);
|
||||
}
|
||||
}
|
|
@ -1,51 +1,13 @@
|
|||
package cn.bunny.controller;
|
||||
|
||||
import cn.bunny.dao.entity.ColumnMetaData;
|
||||
import cn.bunny.dao.vo.TableInfoVo;
|
||||
import cn.bunny.service.TableService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
public class IndexController {
|
||||
|
||||
private final TableService tableService;
|
||||
|
||||
public IndexController(TableService tableService) {
|
||||
this.tableService = tableService;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String index(Model model) {
|
||||
List<TableInfoVo> list = tableService.getAllTableMetaData();
|
||||
model.addAttribute("list" , list);
|
||||
|
||||
public String index() {
|
||||
return "index";
|
||||
}
|
||||
|
||||
@GetMapping("/preview/{tableName}" )
|
||||
public String preview(Model model, @PathVariable String tableName) {
|
||||
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
|
||||
List<ColumnMetaData> columnInfo = tableService.getColumnInfo(tableName);
|
||||
|
||||
model.addAttribute("tableMetaData" , tableMetaData);
|
||||
model.addAttribute("columnInfo" , columnInfo);
|
||||
|
||||
return "preview";
|
||||
}
|
||||
|
||||
@GetMapping("/generator/{tableName}" )
|
||||
public String generator(Model model, @PathVariable String tableName) {
|
||||
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
|
||||
List<ColumnMetaData> columnInfo = tableService.getColumnInfo(tableName);
|
||||
|
||||
model.addAttribute("tableMetaData" , tableMetaData);
|
||||
model.addAttribute("columnInfo" , columnInfo);
|
||||
|
||||
return "generator";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cn.bunny.controller;
|
||||
|
||||
import cn.bunny.dao.entity.ColumnMetaData;
|
||||
import cn.bunny.dao.result.Result;
|
||||
import cn.bunny.dao.vo.TableInfoVo;
|
||||
import cn.bunny.service.TableService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
|
@ -11,9 +12,9 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "表控制器" , description = "代码生成器接口" )
|
||||
@Tag(name = "数据库表控制器" , description = "数据库表信息接口" )
|
||||
@RestController
|
||||
@RequestMapping("/api" )
|
||||
@RequestMapping("/api/table" )
|
||||
public class TableController {
|
||||
|
||||
private final TableService tableService;
|
||||
|
@ -24,19 +25,22 @@ public class TableController {
|
|||
|
||||
@Operation(summary = "获取所有表" , description = "获取所有表" )
|
||||
@GetMapping("getAllTableMetaData" )
|
||||
public List<TableInfoVo> getAllTableMetaData() {
|
||||
return tableService.getAllTableMetaData();
|
||||
public Result<List<TableInfoVo>> getAllTableMetaData() {
|
||||
List<TableInfoVo> list = tableService.getAllTableMetaData();
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取表属性" , description = "获取表属性" )
|
||||
@GetMapping("getTableMetaData" )
|
||||
public TableInfoVo getTableMetaData(String tableName) {
|
||||
return tableService.getTableMetaData(tableName);
|
||||
public Result<TableInfoVo> getTableMetaData(String tableName) {
|
||||
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
|
||||
return Result.success(tableMetaData);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取列属性" , description = "获取列属性" )
|
||||
@GetMapping("getColumnInfo" )
|
||||
public List<ColumnMetaData> getColumnInfo(String tableName) {
|
||||
return tableService.getColumnInfo(tableName);
|
||||
public Result<List<ColumnMetaData>> getColumnInfo(String tableName) {
|
||||
List<ColumnMetaData> columnInfo = tableService.getColumnInfo(tableName);
|
||||
return Result.success(columnInfo);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package cn.bunny.controller;
|
||||
|
||||
import cn.bunny.dao.dto.VmsArgumentDto;
|
||||
import cn.bunny.dao.result.Result;
|
||||
import cn.bunny.dao.vo.GeneratorVo;
|
||||
import cn.bunny.service.VmsService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "生成器", description = "代码生成器接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/vms")
|
||||
public class VmsController {
|
||||
|
||||
private final VmsService vmsService;
|
||||
|
||||
public VmsController(VmsService vmsService) {
|
||||
this.vmsService = vmsService;
|
||||
}
|
||||
|
||||
@Operation(summary = "生成控制器", description = "生成控制器代码")
|
||||
@PostMapping("generator")
|
||||
public Result<List<GeneratorVo>> generator(@Valid @RequestBody VmsArgumentDto dto) {
|
||||
List<GeneratorVo> list = vmsService.generator(dto);
|
||||
return Result.success(list);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package cn.bunny.dao.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class VmsArgumentDto {
|
||||
|
||||
/* 作者名称 */
|
||||
String author = "Bunny";
|
||||
|
||||
/* 包名称 */
|
||||
String packageName = "cn.bunny.services";
|
||||
|
||||
/* requestMapping 名称 */
|
||||
String requestMapping = "/api";
|
||||
|
||||
/* 类名称,格式为:xxx xxx_xxx */
|
||||
@NotBlank(message = "类名称不能为空")
|
||||
@NotNull(message = "类名称不能为空")
|
||||
@Pattern(regexp = "^(?:[a-z][a-z0-9_]*|[_a-z][a-z0-9_]*)$", message = "类名称不合法")
|
||||
private String className;
|
||||
|
||||
/* 表名称 */
|
||||
@NotBlank(message = "表名称不能为空")
|
||||
@NotNull(message = "表名称不能为空")
|
||||
@Pattern(regexp = "^(?:[a-z][a-z0-9_]*|[_a-z][a-z0-9_]*)$", message = "表名称不合法")
|
||||
private String tableName;
|
||||
|
||||
/* 时间格式 */
|
||||
private String simpleDateFormat = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
/* 去除表前缀 */
|
||||
private String tablePrefixes = "t_,sys_,qrtz_,log_";
|
||||
|
||||
/* 路径 */
|
||||
private List<String> path;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package cn.bunny.dao.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class GeneratorVo {
|
||||
|
||||
/* 生成的代码 */
|
||||
private String code;
|
||||
|
||||
/* 表名 */
|
||||
private String tableName;
|
||||
|
||||
/* 注释内容 */
|
||||
private String comment;
|
||||
|
||||
/* 生成类型路径 */
|
||||
private String path;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package cn.bunny.service;
|
||||
|
||||
import cn.bunny.dao.dto.VmsArgumentDto;
|
||||
import cn.bunny.dao.vo.GeneratorVo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface VmsService {
|
||||
/**
|
||||
* 生成服务端代码
|
||||
*
|
||||
* @param dto VmsArgumentDto
|
||||
* @return 生成内容
|
||||
*/
|
||||
List<GeneratorVo> generator(VmsArgumentDto dto);
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package cn.bunny.service.impl;
|
||||
|
||||
import cn.bunny.dao.dto.VmsArgumentDto;
|
||||
import cn.bunny.dao.entity.ColumnMetaData;
|
||||
import cn.bunny.dao.vo.GeneratorVo;
|
||||
import cn.bunny.dao.vo.TableInfoVo;
|
||||
import cn.bunny.service.TableService;
|
||||
import cn.bunny.service.VmsService;
|
||||
import cn.bunny.utils.VmsUtil;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
|
||||
public class VmsServiceImpl implements VmsService {
|
||||
private final TableService tableService;
|
||||
|
||||
public VmsServiceImpl(TableService tableService) {
|
||||
this.tableService = tableService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成服务端代码
|
||||
*
|
||||
* @param dto VmsArgumentDto
|
||||
* @return 生成内容
|
||||
*/
|
||||
@Override
|
||||
public List<GeneratorVo> generator(VmsArgumentDto dto) {
|
||||
return dto.getPath().stream().map(path -> {
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
String vmsPath = "vms/" + path + ".vm";
|
||||
String tableName = dto.getTableName();
|
||||
|
||||
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
|
||||
List<ColumnMetaData> columnInfo = tableService.getColumnInfo(tableName);
|
||||
|
||||
|
||||
VelocityContext context = new VelocityContext();
|
||||
context.put("tableName", tableMetaData.getComment());
|
||||
context.put("package", dto.getPackageName());
|
||||
context.put("columnInfo", columnInfo);
|
||||
|
||||
// VmsUtil.commonVms(writer, context, "vms/server/controller.vm", dto);
|
||||
VmsUtil.commonVms(writer, context, vmsPath, dto);
|
||||
String code = writer.toString();
|
||||
|
||||
return GeneratorVo.builder()
|
||||
.code(code)
|
||||
.comment(tableMetaData.getComment())
|
||||
.tableName(tableMetaData.getTableName())
|
||||
.path(vmsPath)
|
||||
.build();
|
||||
}).toList();
|
||||
}
|
||||
}
|
|
@ -38,9 +38,7 @@ public class ConvertUtil {
|
|||
* @param firstLetterCapital 首字母是否大写
|
||||
*/
|
||||
public static String convertToCamelCase(String name, boolean firstLetterCapital) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
return name;
|
||||
}
|
||||
if (name == null || name.isEmpty()) return name;
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
String[] parts = name.split("_" );
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package cn.bunny.utils;
|
||||
|
||||
import cn.bunny.config.DatabaseMetadataHolder;
|
||||
import cn.bunny.dao.entity.ColumnMetaData;
|
||||
import cn.bunny.dao.entity.TableMetaData;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
@ -16,8 +17,9 @@ import java.util.Set;
|
|||
|
||||
@Component
|
||||
public class DbInfoUtil {
|
||||
|
||||
@Autowired
|
||||
private DatabaseMetadataHolder metadataHolder;
|
||||
private DataSource dataSource;
|
||||
|
||||
/**
|
||||
* 获取表的所有主键列名
|
||||
|
@ -27,19 +29,22 @@ public class DbInfoUtil {
|
|||
*/
|
||||
public Set<String> getPrimaryKeyColumns(String tableName) throws SQLException {
|
||||
Set<String> primaryKeys = new HashSet<>();
|
||||
DatabaseMetaData metaData = metadataHolder.getMetaData();
|
||||
|
||||
try (ResultSet pkResultSet = metaData.getPrimaryKeys(null, null, tableName)) {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
ResultSet pkResultSet = metaData.getPrimaryKeys(null, null, tableName);
|
||||
|
||||
while (pkResultSet.next()) {
|
||||
primaryKeys.add(pkResultSet.getString("COLUMN_NAME" ).toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
return primaryKeys;
|
||||
}
|
||||
}
|
||||
|
||||
public List<TableMetaData> getAllTableInfo() throws SQLException {
|
||||
DatabaseMetaData metaData = metadataHolder.getMetaData();
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
ResultSet tables = metaData.getTables(null, null, "%" , new String[]{"TABLE"});
|
||||
|
||||
List<TableMetaData> list = new ArrayList<>();
|
||||
|
@ -52,6 +57,7 @@ public class DbInfoUtil {
|
|||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表注释信息
|
||||
|
@ -62,7 +68,9 @@ public class DbInfoUtil {
|
|||
*/
|
||||
public TableMetaData tableInfo(String tableName) throws SQLException {
|
||||
TableMetaData tableMetaData = null;
|
||||
DatabaseMetaData metaData = metadataHolder.getMetaData();
|
||||
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
ResultSet tables = metaData.getTables(null, null, tableName, new String[]{"TABLE"});
|
||||
|
||||
// 获取表的注释信息
|
||||
|
@ -89,10 +97,13 @@ public class DbInfoUtil {
|
|||
.selfReferencingColName(selfReferencingColName)
|
||||
.refGeneration(refGeneration)
|
||||
.build();
|
||||
} else {
|
||||
throw new RuntimeException("数据表不存在" );
|
||||
}
|
||||
|
||||
return tableMetaData;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库列信息
|
||||
|
@ -102,7 +113,8 @@ public class DbInfoUtil {
|
|||
* @throws SQLException SQLException
|
||||
*/
|
||||
public List<ColumnMetaData> columnInfo(String tableName) throws SQLException {
|
||||
DatabaseMetaData metaData = metadataHolder.getMetaData();
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
DatabaseMetaData metaData = connection.getMetaData();
|
||||
List<ColumnMetaData> columns = new ArrayList<>();
|
||||
|
||||
Set<String> primaryKeyColumns = getPrimaryKeyColumns(tableName);
|
||||
|
@ -117,7 +129,11 @@ public class DbInfoUtil {
|
|||
column.setJdbcType(columnsRs.getString("TYPE_NAME" ));
|
||||
column.setJavaType(ConvertUtil.convertToJavaType(column.getJdbcType()));
|
||||
column.setComment(columnsRs.getString("REMARKS" ));
|
||||
|
||||
// 确保 primaryKeyColumns 不为空
|
||||
if (!primaryKeyColumns.isEmpty()) {
|
||||
column.setIsPrimaryKey(primaryKeyColumns.contains(columnName));
|
||||
}
|
||||
|
||||
columns.add(column);
|
||||
}
|
||||
|
@ -127,6 +143,7 @@ public class DbInfoUtil {
|
|||
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据库所有的信息
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package cn.bunny.utils;
|
||||
|
||||
import cn.bunny.dao.dto.VmsArgumentDto;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.Velocity;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class VmsUtil {
|
||||
|
||||
/**
|
||||
* @param writer 写入字符串内容
|
||||
* @param context Velocity上下文
|
||||
* @param templateName 模板名称
|
||||
* @param dto 类名称可以自定义,格式为 xxx_xxx
|
||||
*/
|
||||
public static void commonVms(StringWriter writer, VelocityContext context, String templateName, VmsArgumentDto dto) {
|
||||
String className = dto.getClassName();
|
||||
String tablePrefixes = dto.getTablePrefixes();
|
||||
String author = dto.getAuthor();
|
||||
String requestMapping = dto.getRequestMapping();
|
||||
|
||||
AtomicReference<String> replaceTableName = new AtomicReference<>(className);
|
||||
for (String prefix : tablePrefixes.split("[,,]" )) {
|
||||
replaceTableName.set(className.replace(prefix, "" ));
|
||||
}
|
||||
|
||||
String date = new SimpleDateFormat(dto.getSimpleDateFormat()).format(new Date());
|
||||
context.put("date" , date);
|
||||
context.put("author" , author);
|
||||
context.put("requestMapping" , requestMapping);
|
||||
context.put("classLowercaseName" , ConvertUtil.convertToCamelCase(replaceTableName.get()));
|
||||
context.put("classUppercaseName" , ConvertUtil.convertToCamelCase(replaceTableName.get(), true));
|
||||
|
||||
Template servicePathTemplate = Velocity.getTemplate(templateName, "UTF-8" );
|
||||
servicePathTemplate.merge(context, writer);
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,34 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" th:href="@{/css/semantic-ui.css}">
|
||||
<script th:src="@{/js/semantic.min.js}"></script>
|
||||
<script th:src="@{/js/jquery.min.js}"></script>
|
||||
<title>代码生成页面</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="ui container" style="margin-top: 20px;">
|
||||
<div class="ui cards">
|
||||
<div class="card" style="width: 100%;">
|
||||
<div class="content">
|
||||
<div class="header" th:text="'表名:'+${tableMetaData.tableName}"></div>
|
||||
<div class="meta" th:text="'表注释:'+${tableMetaData.comment}">Friend</div>
|
||||
<div class="description"
|
||||
th:text="'数据库名:'+${tableMetaData.tableCat}+';类型:'+${tableMetaData.tableType}">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="ui secondary menu">
|
||||
<a class="item" th:href="@{'/preview/'+${tableMetaData.tableName}}">
|
||||
预览
|
||||
</a>
|
||||
<a class="item active" th:href="@{'/generator/'+${tableMetaData.tableName}}">
|
||||
生成页面
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,39 +1,16 @@
|
|||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" th:href="@{/css/semantic-ui.css}">
|
||||
<script th:src="@{/js/semantic.min.js}"></script>
|
||||
<script th:src="@{/js/jquery.min.js}"></script>
|
||||
<title>主页</title>
|
||||
<meta charset="UTF-8"/>
|
||||
<link href="/favicon.ico" rel="icon" type="image/svg+xml"/>
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
|
||||
<title>代码生成器</title>
|
||||
<script type="module" crossorigin src="/static/js/index-CzX1Gtq9.js"></script>
|
||||
<link rel="modulepreload" crossorigin href="/static/js/vendor-B_wa1CoW.js">
|
||||
<link rel="stylesheet" crossorigin href="/static/css/vendor-D-laMJ-n.css">
|
||||
<link rel="stylesheet" crossorigin href="/static/css/index-BbMOaAOF.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="ui container" style="margin-top: 20px;">
|
||||
<h1 style="text-align: center">代码生成</h1>
|
||||
|
||||
<table class="ui celled padded table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>数据库名称</th>
|
||||
<th>表类型</th>
|
||||
<th class="single line"> 表名</th>
|
||||
<th>注释内容</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<tr th:each="item : ${list}">
|
||||
<td class="ui center aligned" th:text="${item.tableCat}"></td>
|
||||
<td class="ui center aligned" th:text="${item.tableType}"></td>
|
||||
<td class="ui center aligned">
|
||||
<a class="ui blue label" th:href="@{'/preview/'+${item.tableName}}" th:text="${item.tableName}"></a>
|
||||
</td>
|
||||
<td class="single line center aligned ">
|
||||
<a class="ui grey label" th:href="@{'/preview/'+${item.tableName}}" th:text="${item.comment}"></a>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div id="app"></div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,62 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" th:href="@{/css/semantic-ui.css}">
|
||||
<script th:src="@{/js/semantic.min.js}"></script>
|
||||
<script th:src="@{/js/jquery.min.js}"></script>
|
||||
<title>代码生成页面</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="ui container" style="margin-top: 20px;">
|
||||
<div class="ui cards">
|
||||
<div class="card" style="width: 100%;">
|
||||
<div class="content">
|
||||
<div class="header" th:text="'表名:'+${tableMetaData.tableName}"></div>
|
||||
<div class="meta" th:text="'表注释:'+${tableMetaData.comment}">Friend</div>
|
||||
<div class="description"
|
||||
th:text="'数据库名:'+${tableMetaData.tableCat}+';类型:'+${tableMetaData.tableType}">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="ui secondary menu">
|
||||
<a class="item active" th:href="@{'/preview/'+${tableMetaData.tableName}}">
|
||||
预览
|
||||
</a>
|
||||
<a class="item" th:href="@{'/generator/'+${tableMetaData.tableName}}">
|
||||
生成页面
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<table class="ui celled padded table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>列名称</th>
|
||||
<th>字段名称</th>
|
||||
<th class="single line"> 数据库字段类型</th>
|
||||
<th>Java类型</th>
|
||||
<th>是否为主键</th>
|
||||
<th>字段注释</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<tr th:each="item : ${columnInfo}">
|
||||
<td class="ui center aligned" th:text="${item.columnName}"></td>
|
||||
<td class="ui center aligned" th:text="${item.fieldName}"></td>
|
||||
<td class="ui center aligned">
|
||||
<a class="ui blue label" th:text="${item.jdbcType}"></a>
|
||||
</td>
|
||||
<td class="single line center aligned ">
|
||||
<a class="ui grey label" th:text="${item.javaType}"></a>
|
||||
</td>
|
||||
<td class="ui center aligned" th:text="${item.isPrimaryKey?'是':'否'}"></td>
|
||||
<td class="ui center aligned" th:text="${item.comment}"></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,4 +1,4 @@
|
|||
package cn.bunny.services.controller;
|
||||
package ${package}.controller;
|
||||
|
||||
import cn.bunny.dao.pojo.result.Result;
|
||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||
|
@ -15,51 +15,51 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* <p>
|
||||
* ${classTitle}表 前端控制器
|
||||
* ${tableName}表 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author Bunny
|
||||
* @author ${author}
|
||||
* @since ${date}
|
||||
*/
|
||||
@Tag(name = "${classTitle}" , description = "${classTitle}相关接口" )
|
||||
@Tag(name = "${tableName}" , description = "${tableName}相关接口" )
|
||||
@RestController
|
||||
@RequestMapping("admin/${lowercaseName}" )
|
||||
public class ${originalName}Controller {
|
||||
@RequestMapping("${requestMapping}/${classLowercaseName}" )
|
||||
public class ${classUppercaseName}Controller {
|
||||
|
||||
@Autowired
|
||||
private ${originalName}Service ${lowercaseName}Service;
|
||||
private ${classUppercaseName}Service ${classLowercaseName}Service;
|
||||
|
||||
@Operation(summary = "分页查询${classTitle}" , description = "分页查询${classTitle}" )
|
||||
@GetMapping("get${originalName}List/{page}/{limit}" )
|
||||
public Mono<Result<PageResult<${originalName}Vo>>> get${originalName}List(
|
||||
@Operation(summary = "分页查询${tableName}" , description = "分页查询${tableName}" )
|
||||
@GetMapping("get${classUppercaseName}List/{page}/{limit}" )
|
||||
public Result<PageResult<${classUppercaseName}Vo>> get${classUppercaseName}List(
|
||||
@Parameter(name = "page" , description = "当前页" , required = true)
|
||||
@PathVariable("page" ) Integer page,
|
||||
@Parameter(name = "limit" , description = "每页记录数" , required = true)
|
||||
@PathVariable("limit" ) Integer limit,
|
||||
${originalName}Dto dto) {
|
||||
Page<${originalName}> pageParams = new Page<>(page, limit);
|
||||
PageResult<${originalName}Vo> pageResult = ${lowercaseName}Service.get${originalName}List(pageParams, dto);
|
||||
${classUppercaseName}Dto dto) {
|
||||
Page<${classUppercaseName}> pageParams = new Page<>(page, limit);
|
||||
PageResult<${classUppercaseName}Vo> pageResult = ${classLowercaseName}Service.get${classUppercaseName}List(pageParams, dto);
|
||||
return Mono.just(Result.success(pageResult));
|
||||
}
|
||||
|
||||
@Operation(summary = "添加${classTitle}" , description = "添加${classTitle}" )
|
||||
@PostMapping("add${originalName}" )
|
||||
public Mono<Result<String>> add${originalName}(@Valid @RequestBody ${originalName}AddDto dto) {
|
||||
${lowercaseName}Service.add${originalName}(dto);
|
||||
@Operation(summary = "添加${tableName}" , description = "添加${tableName}" )
|
||||
@PostMapping("add${classUppercaseName}" )
|
||||
public Result<String> add${classUppercaseName}(@Valid @RequestBody ${classUppercaseName}AddDto dto) {
|
||||
${classLowercaseName}Service.add${classUppercaseName}(dto);
|
||||
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
|
||||
}
|
||||
|
||||
@Operation(summary = "更新${classTitle}" , description = "更新${classTitle}" )
|
||||
@PutMapping("update${originalName}" )
|
||||
public Mono<Result<String>> update${originalName}(@Valid @RequestBody ${originalName}UpdateDto dto) {
|
||||
${lowercaseName}Service.update${originalName}(dto);
|
||||
@Operation(summary = "更新${tableName}" , description = "更新${tableName}" )
|
||||
@PutMapping("update${classUppercaseName}" )
|
||||
public Result<String> update${classUppercaseName}(@Valid @RequestBody ${classUppercaseName}UpdateDto dto) {
|
||||
${classLowercaseName}Service.update${classUppercaseName}(dto);
|
||||
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
|
||||
}
|
||||
|
||||
@Operation(summary = "删除${classTitle}" , description = "删除${classTitle}" )
|
||||
@DeleteMapping("delete${originalName}" )
|
||||
public Mono<Result<String>> delete${originalName}(@RequestBody List<Long> ids) {
|
||||
${lowercaseName}Service.delete${originalName}(ids);
|
||||
@Operation(summary = "删除${tableName}" , description = "删除${tableName}" )
|
||||
@DeleteMapping("delete${classUppercaseName}" )
|
||||
public Result<String> delete${classUppercaseName}(@RequestBody List<Long> ids) {
|
||||
${classLowercaseName}Service.delete${classUppercaseName}(ids);
|
||||
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS));
|
||||
}
|
||||
}
|
|
@ -1,33 +1,34 @@
|
|||
package cn.bunny.services.mapper;
|
||||
package ${package}.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ${classTitle} Mapper 接口
|
||||
* ${tableName} Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author Bunny
|
||||
* @author ${author}
|
||||
* @since ${date}
|
||||
*/
|
||||
@Mapper
|
||||
public interface ${originalName}Mapper extends BaseMapper<${originalName}> {
|
||||
public interface ${classUppercaseName}Mapper extends BaseMapper<${classUppercaseName}> {
|
||||
|
||||
/**
|
||||
* * 分页查询${classTitle}内容
|
||||
* * 分页查询${tableName}内容
|
||||
*
|
||||
* @param pageParams ${classTitle}分页参数
|
||||
* @param dto ${classTitle}查询表单
|
||||
* @return ${classTitle}分页结果
|
||||
* @param pageParams ${tableName}分页参数
|
||||
* @param dto ${tableName}查询表单
|
||||
* @return ${tableName}分页结果
|
||||
*/
|
||||
IPage<${originalName}Vo> selectListByPage(@Param("page") Page<${originalName}> pageParams, @Param("dto") ${originalName}Dto dto);
|
||||
IPage<${classUppercaseName}Vo> selectListByPage(@Param("page") Page<${classUppercaseName}> pageParams, @Param("dto") ${classUppercaseName}Dto dto);
|
||||
|
||||
/**
|
||||
* 物理删除${classTitle}
|
||||
* 物理删除${tableName}
|
||||
*
|
||||
* @param ids 删除 id 列表
|
||||
*/
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package cn.bunny;
|
||||
|
||||
|
||||
import cn.bunny.config.DatabaseMetadataHolder;
|
||||
import cn.bunny.dao.entity.ColumnMetaData;
|
||||
import cn.bunny.dao.entity.TableMetaData;
|
||||
import cn.bunny.utils.ConvertUtil;
|
||||
|
@ -10,6 +9,8 @@ import org.junit.jupiter.api.Test;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
@ -22,11 +23,13 @@ public class JDBCTest {
|
|||
DatabaseMetaData metaData;
|
||||
|
||||
@Autowired
|
||||
private DatabaseMetadataHolder metadataHolder;
|
||||
private DataSource dataSource;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
metaData = metadataHolder.getMetaData();
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
metaData = connection.getMetaData();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue