🎨 代码生成时默认回填系统配置值
This commit is contained in:
@@ -1,43 +0,0 @@
|
||||
package com.hccake.ballcat.codegen.config;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* 生成配置
|
||||
*/
|
||||
@Data
|
||||
public abstract class AbstractGenConfig implements GenConfig{
|
||||
|
||||
|
||||
/**
|
||||
* 包名
|
||||
*/
|
||||
private String packageName;
|
||||
/**
|
||||
* 作者
|
||||
*/
|
||||
private String author;
|
||||
/**
|
||||
* 模块名称
|
||||
*/
|
||||
private String moduleName;
|
||||
/**
|
||||
* 表前缀
|
||||
*/
|
||||
private String tablePrefix;
|
||||
|
||||
/**
|
||||
* column to javaType 映射关系
|
||||
*/
|
||||
private Map<String, String> typeMapping;
|
||||
|
||||
/**
|
||||
* 需要隐藏的列
|
||||
*/
|
||||
private Set<String> hiddenColumns;
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package com.hccake.ballcat.codegen.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/9/12 15:47
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "gen")
|
||||
@PropertySource("generator.properties")
|
||||
public class DefaultGenConfig extends AbstractGenConfig {
|
||||
|
||||
|
||||
private GenConfig shield() {
|
||||
throw new RuntimeException("default genconfig no modification allowed");
|
||||
}
|
||||
|
||||
public String setMainPath() {
|
||||
shield();
|
||||
return null;
|
||||
}
|
||||
|
||||
public String setPackageName() {
|
||||
shield();
|
||||
return null;
|
||||
}
|
||||
|
||||
public String setAuthor() {
|
||||
shield();
|
||||
return null;
|
||||
}
|
||||
|
||||
public String setModuleName() {
|
||||
shield();
|
||||
return null;
|
||||
}
|
||||
|
||||
public String setTablePrefix() {
|
||||
shield();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, String> setTypeMapping() {
|
||||
shield();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Set<String> setHiddenColumns() {
|
||||
shield();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public GenConfig mergeConfig(GenConfig sourceConfig) {
|
||||
return shield();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +1,48 @@
|
||||
package com.hccake.ballcat.codegen.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/9/12 20:11
|
||||
* 生成配置
|
||||
*/
|
||||
public interface GenConfig {
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "gen")
|
||||
@PropertySource("generator.properties")
|
||||
public class GenConfig{
|
||||
|
||||
/**
|
||||
* 基础包名
|
||||
* @return
|
||||
* 包名
|
||||
*/
|
||||
String getPackageName();
|
||||
|
||||
private String packageName;
|
||||
/**
|
||||
* 作者
|
||||
* @return
|
||||
*/
|
||||
String getAuthor();
|
||||
|
||||
private String author;
|
||||
/**
|
||||
* 模块名
|
||||
* @return
|
||||
* 模块名称
|
||||
*/
|
||||
String getModuleName();
|
||||
|
||||
private String moduleName;
|
||||
/**
|
||||
* 表前缀
|
||||
* @return
|
||||
*/
|
||||
String getTablePrefix();
|
||||
|
||||
private String tablePrefix;
|
||||
|
||||
/**
|
||||
* 类型映射
|
||||
* @return
|
||||
* column to javaType 映射关系
|
||||
*/
|
||||
Map<String, String> getTypeMapping();
|
||||
private Map<String, String> typeMapping;
|
||||
|
||||
/**
|
||||
* swagger文档中隐藏的属性字段
|
||||
* @return
|
||||
* 需要隐藏的列
|
||||
*/
|
||||
Set<String> getHiddenColumns();
|
||||
private Set<String> hiddenColumns;
|
||||
|
||||
|
||||
/**
|
||||
* 合并配置项
|
||||
* @param sourceConfig
|
||||
* @return
|
||||
*/
|
||||
GenConfig mergeConfig(GenConfig sourceConfig);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.hccake.ballcat.codegen.config;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/9/12 15:48
|
||||
*/
|
||||
public class ReqGenConfig extends AbstractGenConfig {
|
||||
|
||||
|
||||
@Override
|
||||
public GenConfig mergeConfig(GenConfig sourceConfig) {
|
||||
|
||||
if(StrUtil.isBlank(this.getAuthor())){
|
||||
this.setAuthor(sourceConfig.getAuthor());
|
||||
}
|
||||
if(StrUtil.isBlank(this.getModuleName())){
|
||||
this.setModuleName(sourceConfig.getModuleName());
|
||||
}
|
||||
if(StrUtil.isBlank(this.getPackageName())){
|
||||
this.setPackageName(sourceConfig.getPackageName());
|
||||
}
|
||||
if(StrUtil.isBlank(this.getTablePrefix())){
|
||||
this.setTablePrefix(sourceConfig.getTablePrefix());
|
||||
}
|
||||
if(CollUtil.isEmpty(this.getTypeMapping())){
|
||||
this.setTypeMapping(sourceConfig.getTypeMapping());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,18 +3,16 @@ package com.hccake.ballcat.codegen.controller;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.baomidou.mybatisplus.extension.api.R;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.hccake.ballcat.codegen.config.DefaultGenConfig;
|
||||
import com.hccake.ballcat.codegen.config.GenConfig;
|
||||
import com.hccake.ballcat.codegen.config.ReqGenConfig;
|
||||
import com.hccake.ballcat.codegen.service.GeneratorService;
|
||||
import com.hccake.ballcat.codegen.vo.GeneratorVo;
|
||||
import com.hccake.ballcat.codegen.vo.GenConfigVO;
|
||||
import com.hccake.ballcat.codegen.vo.GeneratorVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 代码生成器
|
||||
@@ -27,37 +25,49 @@ import java.util.Optional;
|
||||
@RequestMapping("/generator")
|
||||
@RequiredArgsConstructor
|
||||
public class GeneratorController {
|
||||
private final GeneratorService generatorService;
|
||||
private final DefaultGenConfig defaultGenConfig;
|
||||
private final GeneratorService generatorService;
|
||||
private final GenConfig defaultGenConfig;
|
||||
|
||||
/**
|
||||
* 列表
|
||||
*
|
||||
* @param tableName 参数集
|
||||
* @return 数据库表
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public R getPage(Page page, String tableName) {
|
||||
return R.ok(generatorService.getPage(page, tableName).getRecords());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PostMapping("/code")
|
||||
public void generatorCode(@RequestBody GeneratorVo generatorVo, HttpServletResponse response) {
|
||||
/**
|
||||
* 默认配置
|
||||
*
|
||||
* @return 默认配置
|
||||
*/
|
||||
@GetMapping("/config")
|
||||
public R<GenConfig> getConfig() {
|
||||
return R.ok(defaultGenConfig);
|
||||
}
|
||||
|
||||
GenConfig genConfig = Optional.ofNullable(generatorVo.getGenConfig()).orElse(new ReqGenConfig());
|
||||
genConfig = genConfig.mergeConfig(defaultGenConfig);
|
||||
|
||||
String[] tableNames = generatorVo.getTableNames();
|
||||
byte[] data = generatorService.generatorCode(tableNames, genConfig);
|
||||
response.reset();
|
||||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"ballcat.zip\"");
|
||||
response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
/**
|
||||
* 列表
|
||||
*
|
||||
* @param tableName 参数集
|
||||
* @return 数据库表
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public R getPage(Page page, String tableName) {
|
||||
return R.ok(generatorService.getPage(page, tableName).getRecords());
|
||||
}
|
||||
|
||||
IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
|
||||
}
|
||||
/**
|
||||
* 生成代码
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PostMapping("/code")
|
||||
public void generatorCode(@RequestBody GeneratorVO generatorVo, HttpServletResponse response) {
|
||||
|
||||
GenConfigVO vo = generatorVo.getGenConfigVO();
|
||||
GenConfig genConfig = vo.transform(defaultGenConfig);
|
||||
|
||||
String[] tableNames = generatorVo.getTableNames();
|
||||
byte[] data = generatorService.generatorCode(tableNames, genConfig);
|
||||
response.reset();
|
||||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"ballcat.zip\"");
|
||||
response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
|
||||
IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,7 +224,8 @@ public class GenUtils {
|
||||
}
|
||||
|
||||
if (template.contains(MAPPER_XML_VM)) {
|
||||
return BACK_PROJECT_NAME + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + className + "Mapper.xml";
|
||||
return BACK_PROJECT_NAME + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "mapper"
|
||||
+ File.separator + moduleName + File.separator + className + "Mapper.xml";
|
||||
}
|
||||
|
||||
if (template.contains(MENU_SQL_VM)) {
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.hccake.ballcat.codegen.vo;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.hccake.ballcat.codegen.config.GenConfig;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* 生成配置
|
||||
*/
|
||||
@Data
|
||||
public class GenConfigVO {
|
||||
|
||||
/**
|
||||
* 包名
|
||||
*/
|
||||
private String packageName;
|
||||
/**
|
||||
* 作者
|
||||
*/
|
||||
private String author;
|
||||
/**
|
||||
* 模块名称
|
||||
*/
|
||||
private String moduleName;
|
||||
/**
|
||||
* 表前缀
|
||||
*/
|
||||
private String tablePrefix;
|
||||
|
||||
|
||||
public GenConfig transform(GenConfig defaultGenConfig) {
|
||||
GenConfig genConfig = new GenConfig();
|
||||
BeanUtil.copyProperties(defaultGenConfig, genConfig);
|
||||
|
||||
if(packageName != null){
|
||||
genConfig.setPackageName(packageName);
|
||||
}
|
||||
if(author != null){
|
||||
genConfig.setAuthor(author);
|
||||
}
|
||||
if(moduleName != null){
|
||||
genConfig.setModuleName(moduleName);
|
||||
}
|
||||
if(tablePrefix != null){
|
||||
genConfig.setTablePrefix(tablePrefix);
|
||||
}
|
||||
return genConfig;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.hccake.ballcat.codegen.vo;
|
||||
|
||||
import com.hccake.ballcat.codegen.config.ReqGenConfig;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
@@ -8,15 +7,16 @@ import lombok.Data;
|
||||
* 生成参数vo
|
||||
*/
|
||||
@Data
|
||||
public class GeneratorVo {
|
||||
public class GeneratorVO {
|
||||
|
||||
/**
|
||||
* 配置信息
|
||||
* 配置
|
||||
*/
|
||||
private ReqGenConfig genConfig;
|
||||
private GenConfigVO genConfigVO;
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
private String[] tableNames;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
#基础包名
|
||||
gen.packageName=com.hccake.ballcat.admin
|
||||
gen.packageName=com.hccake.ballcat.admin.modules
|
||||
#模块名
|
||||
gen.moduleName=sys
|
||||
#作者
|
||||
gen.author=ballcat code generator
|
||||
gen.author=hccake
|
||||
#表前缀(类名不会包含表前缀)
|
||||
gen.tablePrefix=
|
||||
gen.tablePrefix=tbl_
|
||||
|
||||
|
||||
#column To java 类型转换配置
|
||||
|
||||
@@ -23,10 +23,4 @@
|
||||
<input type="text" name="tablePrefix" required lay-verify="required" placeholder="请输入表前缀" autocomplete="off" class="layui-input">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
layui.use('form', function(){
|
||||
var form = layui.form;
|
||||
});
|
||||
</script>
|
||||
</form>
|
||||
@@ -32,16 +32,14 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="id" style="display: none">
|
||||
text
|
||||
</div>
|
||||
|
||||
<script>
|
||||
layui.use(['admin', 'table'], function(){
|
||||
|
||||
var table = layui.table
|
||||
,$ = layui.$
|
||||
, form = layui.form;
|
||||
, form = layui.form
|
||||
, admin = layui.admin;
|
||||
|
||||
var TABLE_ID = 'table-codegen';
|
||||
|
||||
@@ -59,6 +57,29 @@
|
||||
,height: 'full-250' //高度最大化减去差值
|
||||
});
|
||||
|
||||
// 默认先读取到弹窗的html,避免多次点击重复加载
|
||||
var formHtml;
|
||||
$.ajax({
|
||||
url: "./views/genConfigForm.html",
|
||||
async: false,
|
||||
type: "GET",
|
||||
dataType: "html",
|
||||
success: function(result){
|
||||
formHtml = result;
|
||||
}
|
||||
})
|
||||
|
||||
var defaultConfig;
|
||||
admin.req({
|
||||
type: "GET",
|
||||
url: "/generator/config",
|
||||
async: false,
|
||||
success: function (res) {
|
||||
defaultConfig = res.data;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
var active = {
|
||||
reload: function(){
|
||||
var tableName = $('#tableName');
|
||||
@@ -88,27 +109,23 @@
|
||||
tableNames.push(data[i].tableName);
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
url: "./views/genConfigForm.html",
|
||||
type: "GET",
|
||||
dataType: "html",
|
||||
success: function(result){
|
||||
layer.open({
|
||||
id: "genConfigLayer",
|
||||
type: 1,
|
||||
area: ['420px', '350px'],
|
||||
content: result,
|
||||
btn: ['生成', '取消'],
|
||||
yes: function(index, layero){
|
||||
debugger
|
||||
// 异步下载
|
||||
active.download(tableNames, form.val("gen-config-form"));
|
||||
// 关闭弹窗
|
||||
layer.close(index)
|
||||
}
|
||||
})
|
||||
layer.open({
|
||||
id: "genConfigLayer",
|
||||
type: 1,
|
||||
area: ['420px', '350px'],
|
||||
content: formHtml,
|
||||
btn: ['生成', '取消'],
|
||||
success: function() {
|
||||
form.val("gen-config-form", defaultConfig);
|
||||
},
|
||||
yes: function(index, layero){
|
||||
// 异步下载
|
||||
active.download(tableNames, form.val("gen-config-form"));
|
||||
// 关闭弹窗
|
||||
layer.close(index)
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
},
|
||||
download: function (tableNames, genConfig) {
|
||||
var url = 'generator/code';
|
||||
@@ -137,7 +154,7 @@
|
||||
}
|
||||
};
|
||||
// 发送ajax请求
|
||||
xhr.send(JSON.stringify({tableNames: tableNames, genConfig: genConfig}));
|
||||
xhr.send(JSON.stringify({tableNames: tableNames, genConfigVO: genConfig}));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.hccake.ballcat.common.core.result.ResultStatus;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user