1.添加tinyint类型转换(感谢@lixiliang&@liujiansgit的Suggestion) 2.添加一键复制功能(感谢@gaohanghang的Suggestion) 3.Mybatis的insert增加keyProperty="id"用于返回自增id(感谢@88888888888888888888的Suggestion) 4.优化date类型的支持(感谢@SteveLsf的反馈) 5.其他一些优化.

This commit is contained in:
MOSHOW.K.ZHENG
2019-11-14 23:55:04 +08:00
parent c5fadb38c9
commit c34fccc6f1
12 changed files with 122 additions and 124 deletions

View File

@@ -2,8 +2,8 @@ package com.softdev.system.generator.controller;
import com.softdev.system.generator.entity.ClassInfo;
import com.softdev.system.generator.entity.ReturnT;
import com.softdev.system.generator.util.CodeGeneratorTool;
import com.softdev.system.generator.util.FreemarkerTool;
import com.softdev.system.generator.util.TableParseUtil;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -40,8 +40,9 @@ public class IndexController {
@RequestParam(required = false, defaultValue = "大狼狗") String authorName,
@RequestParam(required = false, defaultValue = "com.softdev.system")String packageName,
@RequestParam(required = false, defaultValue = "ApiReturnUtil")String returnUtil,
@RequestParam(required = false, defaultValue = "true")boolean isUnderLineToCamelCase
) {
@RequestParam(required = false, defaultValue = "true")boolean isUnderLineToCamelCase,
@RequestParam(required = false, defaultValue = "boolean")String tinyintTransType
) {
try {
@@ -51,17 +52,17 @@ public class IndexController {
}
// parse table
ClassInfo classInfo = CodeGeneratorTool.processTableIntoClassInfo(tableSql, isUnderLineToCamelCase);
ClassInfo classInfo = TableParseUtil.processTableIntoClassInfo(tableSql, isUnderLineToCamelCase, tinyintTransType);
// code genarete
Map<String, Object> params = new HashMap<String, Object>();
Map<String, Object> params = new HashMap<String, Object>(8);
params.put("classInfo", classInfo);
params.put("authorName", authorName);
params.put("packageName", packageName);
params.put("returnUtil", returnUtil);
// result
Map<String, String> result = new HashMap<String, String>();
Map<String, String> result = new HashMap<String, String>(32);
//UI
result.put("swagger-ui", freemarkerTool.processString("code-generator/ui/swagger-ui.ftl", params));

View File

@@ -1,26 +0,0 @@
package com.softdev.system.generator.util;
import com.softdev.system.generator.entity.ClassInfo;
import java.io.IOException;
/**
* code generate tool
*
* @author xuxueli 2018-04-25 16:29:58
*/
public class CodeGeneratorTool {
/**
* process Table Into ClassInfo
*
* @param tableSql
* @return
*/
public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnderLineToCamelCase) throws IOException {
return TableParseUtil.processTableIntoClassInfo(tableSql, isUnderLineToCamelCase);
}
}

View File

@@ -4,8 +4,7 @@ import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
@@ -18,8 +17,8 @@ import java.util.Map;
*
* @author xuxueli 2018-05-02 19:56:00
*/
@Slf4j
public class FreemarkerUtil {
private static final Logger logger = LoggerFactory.getLogger(CodeGeneratorTool.class);
/**
* freemarker config
@@ -40,7 +39,7 @@ public class FreemarkerUtil {
freemarkerConfig.setLocale(Locale.CHINA);
freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
} catch (IOException e) {
logger.error(e.getMessage(), e);
log.error(e.getMessage(), e);
}
}

View File

@@ -24,7 +24,8 @@ public class TableParseUtil {
* @param tableSql
* @return
*/
public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnderLineToCamelCase) throws IOException {
public static ClassInfo processTableIntoClassInfo(String tableSql, boolean isUnderLineToCamelCase,String tinyintTransType)
throws IOException {
if (tableSql==null || tableSql.trim().length()==0) {
throw new CodeGenerateException("Table structure can not be empty.");
}
@@ -41,7 +42,9 @@ public class TableParseUtil {
}
//新增处理create table if not exists members情况
if (tableName.contains("if not exists")) tableName=tableName.replaceAll("if not exists","");
if (tableName.contains("if not exists")) {
tableName=tableName.replaceAll("if not exists","");
}
if (tableName.contains("`")) {
tableName = tableName.substring(tableName.indexOf("`")+1, tableName.lastIndexOf("`"));
@@ -101,20 +104,22 @@ public class TableParseUtil {
String fieldListTmp = tableSql.substring(tableSql.indexOf("(")+1, tableSql.lastIndexOf(")"));
// 匹配 comment替换备注里的小逗号, 防止不小心被当成切割符号切割
Matcher matcher = Pattern.compile("comment `(.*?)\\`").matcher(fieldListTmp); // "\\{(.*?)\\}"
while(matcher.find()){
String commentPattenStr1="comment `(.*?)\\`";
Matcher matcher1 = Pattern.compile(commentPattenStr1).matcher(fieldListTmp);
while(matcher1.find()){
String commentTmp = matcher.group();
String commentTmp = matcher1.group();
//2018-9-27 zhengk 不替换只处理支持COMMENT评论里面多种注释
//commentTmp = commentTmp.replaceAll("\\ comment `|\\`", " "); // "\\{|\\}"
if (commentTmp.contains(",")) {
String commentTmpFinal = commentTmp.replaceAll(",", "");
fieldListTmp = fieldListTmp.replace(matcher.group(), commentTmpFinal);
fieldListTmp = fieldListTmp.replace(matcher1.group(), commentTmpFinal);
}
}
//2018-10-18 zhengkai 新增支持double(10, 2)等类型中有英文逗号的特殊情况
Matcher matcher2 = Pattern.compile("\\`(.*?)\\`").matcher(fieldListTmp); // "\\{(.*?)\\}"
String commentPattenStr2="\\`(.*?)\\`";
Matcher matcher2 = Pattern.compile(commentPattenStr2).matcher(fieldListTmp);
while(matcher2.find()){
String commentTmp2 = matcher2.group();
if (commentTmp2.contains(",")) {
@@ -123,7 +128,8 @@ public class TableParseUtil {
}
}
//2018-10-18 zhengkai 新增支持double(10, 2)等类型中有英文逗号的特殊情况
Matcher matcher3 = Pattern.compile("\\((.*?)\\)").matcher(fieldListTmp); // "\\{(.*?)\\}"
String commentPattenStr3="\\((.*?)\\)";
Matcher matcher3 = Pattern.compile(commentPattenStr3).matcher(fieldListTmp);
while(matcher3.find()){
String commentTmp3 = matcher3.group();
if (commentTmp3.contains(",")) {
@@ -148,7 +154,6 @@ public class TableParseUtil {
&&!columnLine.contains("pctincrease")
&&!columnLine.contains("buffer_pool")&&!columnLine.contains("tablespace")
&&!(columnLine.contains("primary")&&i>3));
if (specialFlag){
//如果是oracle的number(x,x),可能出现最后分割残留的,x),这里做排除处理
if(columnLine.length()<5) {continue;}
@@ -157,9 +162,8 @@ public class TableParseUtil {
columnLine=columnLine.replaceAll("`"," ").replaceAll("\""," ").replaceAll("'","").replaceAll(" "," ").trim();
//如果遇到username varchar(65) default '' not null,这种情况,判断第一个空格是否比第一个引号前
columnName = columnLine.substring(0, columnLine.indexOf(" "));
// field Name
// 2019-09-08 yj 添加是否下划线转换为驼峰的判断
// 2019-09-08 yj 添加是否下划线转换为驼峰的判断
String fieldName;
if(isUnderLineToCamelCase){
fieldName = StringUtils.lowerCaseFirst(StringUtils.underlineToCamelCase(columnName));
@@ -184,7 +188,7 @@ public class TableParseUtil {
fieldClass = Float.class.getSimpleName();
} else if (columnLine.contains("double")) {
fieldClass = Double.class.getSimpleName();
} else if (columnLine.contains("datetime") || columnLine.contains("timestamp")) {
} else if (columnLine.contains("time") || columnLine.contains("date") || columnLine.contains("datetime") || columnLine.contains("timestamp")) {
fieldClass = Date.class.getSimpleName();
} else if (columnLine.contains("varchar") || columnLine.contains(" text")|| columnLine.contains("char")
|| columnLine.contains("clob")||columnLine.contains("blob")||columnLine.contains("json")) {
@@ -221,9 +225,12 @@ public class TableParseUtil {
}else{
fieldClass = BigDecimal.class.getSimpleName();
}
} else if (columnLine.contains("boolean")|| columnLine.contains("tinyint") ) {
} else if (columnLine.contains("boolean")) {
//20190910 MOSHOW.K.ZHENG 新增对boolean的处理感谢@violinxsc的反馈以及修复tinyint类型字段无法生成boolean类型问题感谢@hahaYhui的反馈
fieldClass = Boolean.class.getSimpleName();
} else if (columnLine.contains("tinyint") ) {
//20191115 MOSHOW.K.ZHENG 支持对tinyint的特殊处理
fieldClass=tinyintTransType;
} else {
fieldClass = String.class.getSimpleName();
}

View File

@@ -19,7 +19,7 @@
</#if>
</sql>
<insert id="insert" useGeneratedKeys="true" keyColumn="id" parameterType="${packageName}.entity.${classInfo.className}Entity">
<insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="${packageName}.entity.${classInfo.className}Entity">
INSERT INTO ${classInfo.tableName}
<trim prefix="(" suffix=")" suffixOverrides=",">
<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>

View File

@@ -8,13 +8,13 @@
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- Bootstrap 4 -->
<link href="//cdn.staticfile.org/twitter-bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
<link href="//cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome -->
<link href="//cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link href="//cdn.staticfile.org/font-awesome/5.11.2/css/fontawesome.min.css" rel="stylesheet">
<!-- Ionicons -->
<link href="//cdn.staticfile.org/ionicons/4.1.2/css/ionicons.min.css" rel="stylesheet">
<link href="//cdn.staticfile.org/ionicons/4.5.6/css/ionicons.min.css" rel="stylesheet">
<link href="//cdn.staticfile.org/codemirror/5.42.0/codemirror.min.css" rel="stylesheet">
<link href="//cdn.staticfile.org/codemirror/5.48.4/codemirror.min.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
@@ -28,33 +28,43 @@
</#macro>
<#macro commonScript>
<!-- jQuery -->
<script src="//cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.staticfile.org/jquery/3.4.1/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="//cdn.staticfile.org/twitter-bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<!-- FastClick -->
<script src="//cdn.staticfile.org/fastclick/1.0.6/fastclick.min.js"></script>
<script src="//cdn.staticfile.org/jQuery-slimScroll/1.3.8/jquery.slimscroll.min.js"></script>
<script src="//cdn.staticfile.org/layer/2.3/layer.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.42.0/codemirror.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.42.0/addon/display/placeholder.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.42.0/mode/clike/clike.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.42.0/mode/sql/sql.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.42.0/mode/xml/xml.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.48.4/codemirror.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.48.4/addon/display/placeholder.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.48.4/mode/clike/clike.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.48.4/mode/sql/sql.min.js"></script>
<script src="//cdn.staticfile.org/codemirror/5.48.4/mode/xml/xml.min.js"></script>
</#macro>
<#macro commonFooter >
<footer class="main-footer">
<div class="container">
Powered by <b>Spring Boot Code Generator</b> base on XXL Code Generator
<div class="pull-right hidden-xs">
<strong>Copyright &copy; 2018-${.now?string('yyyy')} &nbsp;
<a href="https://github.com/moshowgame/SpringBootCodeGenerator" target="_blank" >SpringBootCodeGenerator</a>
<a href="https://github.com/xuxueli/xxl-code-generator" target="_blank" >xxl-code-generator</a>
</strong><!-- All rights reserved. -->
</div>
</div>
</footer>
<hr>
<footer>
<footer class="bd-footer text-muted" role="contentinfo">
<div class="container">
<strong>Copyright &copy; ${.now?string('yyyy')}-2022 &nbsp;
<p><a href="https://github.com/moshowgame/SpringBootCodeGenerator">SpringBootCodeGenerator</a>由<a href="https://blog.csdn.net/moshowgame" target="_blank">@Moshow/大狼狗/郑锴</a> 开发维护。 由 <a href="https://www.bejson.com">BeJson三叔 </a> 提供在线版本。点击<a href="#" id="donate2">赞赏</a>。</p>
</div>
</footer>
</footer>
</div> <!-- /container -->
</#macro>
<#macro viewerCounter>
var _hmt = _hmt || [];
(function() {
//百度统计一下
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?97fd5ca1a4298ac8349c7e0de9029a0f";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</#macro>

View File

@@ -4,14 +4,17 @@
<meta charset="UTF-8">
<title>SQL转Java JPA、MYBATIS实现类代码生成平台</title>
<meta name="keywords" content="sql转实体类,sql转DAO,SQL转service,SQL转JPA实现,SQL转MYBATIS实现">
<#import "common/common-import.ftl" as netCommon>
<@netCommon.commonStyle />
<@netCommon.commonScript />
<#--<script src="${request.contextPath}/static/js/index-new.js"></script>-->
<script>
$(function () {
<@netCommon.viewerCounter />
$(function () {
/**
* 初始化 table sql 3
*/
@@ -58,15 +61,10 @@
dataType: "json",
success: function (data) {
if (data.code == 200) {
layer.open({
icon: '1',
content: "代码生成成功",
end: function () {
codeData = data.data;
genCodeArea.setValue(codeData.beetlentity);
genCodeArea.setSize('auto', 'auto');
}
});
layer.msg("代码生成成功");
codeData = data.data;
genCodeArea.setValue(codeData.beetlentity);
genCodeArea.setSize('auto', 'auto');
} else {
layer.open({
icon: '2',
@@ -86,6 +84,9 @@
genCodeArea.setSize('auto', 'auto');
}
});
/**
* 捐赠
*/
function donate(){
layer.open({
type: 1,
@@ -100,6 +101,13 @@
$('#donate2').on('click', function(){
donate();
});
$('#btnCopy').on('click', function(){
if(!$.isEmptyObject(genCodeArea.getValue())&&!$.isEmptyObject(navigator)&&!$.isEmptyObject(navigator.clipboard)){
navigator.clipboard.writeText(genCodeArea.getValue());
layer.msg("复制成功");
}
});
});
</script>
</head>
@@ -110,7 +118,7 @@
<a class="navbar-brand" href="http://www.bejson.com">BeJSON在线工具站</a>
<ul class="nav navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="http://blog.csdn.net/moshowgame">大狼狗CSDN</a>
<a class="nav-link" href="http://zhengkai.blog.csdn.net">大狼狗CSDN</a>
</li>
</ul>
</nav>
@@ -121,13 +129,20 @@
<div class="container">
<h2>Spring Boot Code Generator!</h2>
<p class="lead">
基于<code>SpringBoot2</code>+<code>Freemarker</code>的代码生成器,用<code>DDL SQL</code>语句生成<code>JPA</code>/<code>JdbcTemplate</code>/<code>Mybatis</code>/<code>MybatisPlus</code>/<code>BeetlSQL</code>相关代码,支持<code>mysql</code>/<code>oracle</code>/<code>pgsql</code>三大数据库。以<code>释放双手</code>为目的各大模板也在陆续补充和优化。欢迎大家多多提交模板和交流想法如果发现有SQL语句不能识别请<a href="https://github.com/moshowgame/SpringBootCodeGenerator/issues">留言</a>给我分析,同时欢迎大家进行<a href="https://github.com/moshowgame/SpringBootCodeGenerator/pulls">PullRequest</a>和<a href="#" id="donate1">赞赏</a>,谢谢!
基于SpringBoot2+Freemarker的代码生成器√以释放双手为目的√支持mysql/oracle/pgsql三大数据库<br>
√用DDL-SQL语句生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL相关代码。<br>
欢迎大家多多提交模板和交流想法如果发现有SQL语句不能识别请<a href="https://github.com/moshowgame/SpringBootCodeGenerator/issues">留言</a>,同时欢迎大家提<a href="https://github.com/moshowgame/SpringBootCodeGenerator/pulls">PR</a>和<a href="#" id="donate1">点击赞赏</a>,谢谢!
</p>
<hr>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">作者名称</span>
</div>
<input type="text" class="form-control" id="authorName" name="authorName" placeholder="大狼狗">
<div class="input-group-prepend">
<span class="input-group-text">返回封装</span>
</div>
<input type="text" class="form-control" id="returnUtil" name="returnUtil" placeholder="ApiReturnObject">
<div class="input-group-prepend">
<span class="input-group-text">包名路径</span>
</div>
@@ -135,11 +150,17 @@
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">返回封装</span>
<span class="input-group-text">tinyint转换类型</span>
</div>
<input type="text" class="form-control" id="returnUtil" name="returnUtil" placeholder="ApiReturnObject">
<select type="text" class="form-control" id="tinyintTransType"
name="tinyintTransType">
<option value="boolean">boolean</option>
<option value="Boolean">Boolean</option>
<option value="Integer">Integer</option>
<option value="int">int</option>
</select>
<div class="input-group-prepend">
<span class="input-group-text">是否下划线转换为驼峰</span>
<span class="input-group-text">是否转换下划线为驼峰</span>
</div>
<select type="text" class="form-control" id="isUnderLineToCamelCase"
name="isUnderLineToCamelCase">
@@ -155,7 +176,7 @@ CREATE TABLE `userinfo` (
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息'
</textarea><br>
<p><button class="btn btn-primary btn-lg disabled" id="btnGenCode" role="button">开始生成 »</button></p>
<p><button class="btn btn-primary btn-lg disabled" id="btnGenCode" role="button">开始生成 »</button> <button class="btn alert-secondary" id="btnCopy">一键复制</button></p>
<hr>
<!-- Example row of columns -->
<div class="row" style="margin-top: 10px;">
@@ -290,20 +311,6 @@ CREATE TABLE `userinfo` (
<textarea id="genCodeArea" class="form-control btn-lg" ></textarea>
</div>
</div>
<div class="container">
<hr>
<footer>
<footer class="bd-footer text-muted" role="contentinfo">
<div class="container">
<strong>Copyright &copy; ${.now?string('yyyy')}-2022 &nbsp;
<p><a href="https://github.com/moshowgame/SpringBootCodeGenerator">SpringBootCodeGenerator</a>由<a href="https://blog.csdn.net/moshowgame" target="_blank">@Moshow/大狼狗/郑锴</a> 开发维护。 由 <a href="https://www.bejson.com">BeJson三叔 </a> 提供在线版本。点击<a href="#" id="donate2">赞赏</a>。</p>
</div>
</footer>
</footer>
</div> <!-- /container -->
<@netCommon.commonFooter />
</body>
</html>