From bc5e93860d18611e94c1fc3c4d8a0201ef020a7f Mon Sep 17 00:00:00 2001 From: "moshowgame@126.com" Date: Wed, 26 Sep 2018 12:42:18 +0800 Subject: [PATCH] beetl supoort --- README.md | 3 +- .../generator/controller/IndexController.java | 4 + .../system/generator/util/StringUtils.java | 30 ++++++++ .../src/main/resources/static/js/index-new.js | 18 +++++ .../src/main/resources/templates/index.ftl | 21 ++++-- .../xxl-code-generator/beetlcontroller.ftl | 73 +++++++++++++++++++ .../xxl-code-generator/beetlentity.ftl | 24 ++++++ .../templates/xxl-code-generator/beetlmd.ftl | 31 ++++++++ .../templates/xxl-code-generator/entity.ftl | 7 -- .../xxl-code-generator/jpacontroller.ftl | 2 +- .../xxl-code-generator/jtdaoimpl.ftl | 4 +- .../templates/xxl-code-generator/model.ftl | 9 +-- 12 files changed, 202 insertions(+), 24 deletions(-) create mode 100644 generator-web/src/main/resources/templates/xxl-code-generator/beetlcontroller.ftl create mode 100644 generator-web/src/main/resources/templates/xxl-code-generator/beetlentity.ftl create mode 100644 generator-web/src/main/resources/templates/xxl-code-generator/beetlmd.ftl diff --git a/README.md b/README.md index 04f5116..2482b86 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ SpringBootCodeGenerator ---- -基于SpringBoot2+Freemarker的代码生成器,用DDL语句生成JPA/JdbcTemplate/Mybatis相关代码,支持mysql/oracle/pgsql三大数据库。目前以JPA的为主,各大模板也在陆续优化。欢迎大家提交模板和交流想法! +基于SpringBoot2+Freemarker的代码生成器,用DDL SQL语句生成JPA/JdbcTemplate/Mybatis相关代码,支持mysql/oracle/pgsql三大数据库。目前以JPA的为主,各大模板也在陆续优化。欢迎大家提交模板和交流想法!

另外,感谢bejson三叔将他部署在http://java.bejson.com/generator上

@@ -16,6 +16,7 @@ SpringBootCodeGenerator CSDN博客 http://blog.csdn.net/moshowgame 更新日期 更新内容 +20180926全新BeetlSQL模块,以及一些小细节优化。 20180925优化SQL表和字段备注的推断,包括pgsql/oralce的comment on column/table情况处理等。 20180918优化SQL类型推断。优化PrimaryKey判断。修复jpacontroller中Repository拼写错误问题。 20180917全新首页,静态文件全部采用CDN。新增jdbcTemplate模块。 diff --git a/generator-web/src/main/java/com/softdev/system/generator/controller/IndexController.java b/generator-web/src/main/java/com/softdev/system/generator/controller/IndexController.java index 0b8a4f5..8586878 100644 --- a/generator-web/src/main/java/com/softdev/system/generator/controller/IndexController.java +++ b/generator-web/src/main/java/com/softdev/system/generator/controller/IndexController.java @@ -75,6 +75,10 @@ public class IndexController { result.put("jtdao", freemarkerTool.processString("xxl-code-generator/jtdao.ftl", params)); result.put("jtdaoimpl", freemarkerTool.processString("xxl-code-generator/jtdaoimpl.ftl", params)); + result.put("beetlmd", freemarkerTool.processString("xxl-code-generator/beetlmd.ftl", params)); + result.put("beetlentity", freemarkerTool.processString("xxl-code-generator/beetlentity.ftl", params)); + result.put("beetlcontroller", freemarkerTool.processString("xxl-code-generator/beetlcontroller.ftl", params)); + // 计算,生成代码行数 int lineNum = 0; for (Map.Entry item: result.entrySet()) { diff --git a/generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java b/generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java index e57deed..da42f82 100644 --- a/generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java +++ b/generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java @@ -1,5 +1,13 @@ package com.softdev.system.generator.util; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.bean.copier.CopyOptions; +import com.alibaba.fastjson.JSON; +import com.softdev.system.generator.entity.ClassInfo; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * string tool * @@ -54,4 +62,26 @@ public class StringUtils { return result.toString(); } + public static void main(String[] args) { + /*String tableSql="comment on column check_info is `体检信息` ,comment on column check_xxxxx is `体检信息xx` "; + String fieldName="check_info"; + String fieldComment="aaa"; + Matcher columnCommentMatcher = Pattern.compile(fieldName+" is `").matcher(tableSql); // "\\{(.*?)\\}" + while(columnCommentMatcher.find()){ + String columnCommentTmp = columnCommentMatcher.group(); + System.out.println(columnCommentTmp); + fieldComment = tableSql.substring(tableSql.indexOf(columnCommentTmp)+columnCommentTmp.length()).trim(); + fieldComment = fieldComment.substring(0,fieldComment.indexOf("`")).trim(); + } + System.out.println(fieldComment);*/ + /*ClassInfo classInfo=new ClassInfo(); + classInfo.setClassComment("xxxx"); + classInfo.setTableName("2b"); + ClassInfo newInfo=new ClassInfo(); + classInfo.setClassName("bbbb"); + classInfo.setTableName("3b"); + //以new为主 + BeanUtil.copyProperties(classInfo,newInfo,true, CopyOptions.create()); + System.out.println(JSON.toJSONString(newInfo));*/ + } } diff --git a/generator-web/src/main/resources/static/js/index-new.js b/generator-web/src/main/resources/static/js/index-new.js index 8294599..4b5b5f6 100644 --- a/generator-web/src/main/resources/static/js/index-new.js +++ b/generator-web/src/main/resources/static/js/index-new.js @@ -138,5 +138,23 @@ $(function () { genCodeArea.setSize('auto', 'auto'); } }); + $('#beetlcontroller').click(function () { + if(!$.isEmptyObject(codeData)) { + genCodeArea.setValue(codeData.beetlcontroller); + genCodeArea.setSize('auto', 'auto'); + } + }); + $('#beetlmd').click(function () { + if(!$.isEmptyObject(codeData)) { + genCodeArea.setValue(codeData.beetlmd); + genCodeArea.setSize('auto', 'auto'); + } + }); + $('#beetlentity').click(function () { + if(!$.isEmptyObject(codeData)) { + genCodeArea.setValue(codeData.beetlentity); + genCodeArea.setSize('auto', 'auto'); + } + }); }); \ No newline at end of file diff --git a/generator-web/src/main/resources/templates/index.ftl b/generator-web/src/main/resources/templates/index.ftl index 4d38923..0ec6124 100644 --- a/generator-web/src/main/resources/templates/index.ftl +++ b/generator-web/src/main/resources/templates/index.ftl @@ -30,8 +30,7 @@

Spring Boot Code Generator!

-

基于SpringBoot2+xxl-codegenerator的代码生成器。用DDL语句生成JPA/JdbcTemplate/Mybatis相关代码,目前以JPA的为主,各大模板也在陆续优化。如果发现有SQL语句不能识别,请留言给我分析,谢谢! - + 基于SpringBoot2+Freemarker的代码生成器,用DDL SQL语句生成JPA/JdbcTemplate/Mybatis相关代码,支持mysql/oracle/pgsql三大数据库。目前以JPA的为主,各大模板也在陆续优化。欢迎大家提交模板和交流想法,如果发现有SQL语句不能识别,请留言给我分析,谢谢!

@@ -65,6 +64,18 @@ CREATE TABLE `userinfo` (
+
- + diff --git a/generator-web/src/main/resources/templates/xxl-code-generator/beetlcontroller.ftl b/generator-web/src/main/resources/templates/xxl-code-generator/beetlcontroller.ftl new file mode 100644 index 0000000..1410985 --- /dev/null +++ b/generator-web/src/main/resources/templates/xxl-code-generator/beetlcontroller.ftl @@ -0,0 +1,73 @@ +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import java.util.List; +import java.util.Map; + +/** +* ${classInfo.classComment} +* @author ${authorName} ${.now?string('yyyy-MM-dd')} +*/ +@RestController +@RequestMapping("/${classInfo.className?uncap_first}") +public class ${classInfo.className}Controller { + + @Autowired + private SQLManager sqlManager; + + /** + * 新增或编辑 + */ + @PostMapping("/save") + public Object save(${classInfo.className} ${classInfo.className?uncap_first}){ + ${classInfo.className} ${classInfo.className?uncap_first}=sqlManager.unique(${classInfo.className}.class,${classInfo.className?uncap_first}.getId()); + if(${classInfo.className?uncap_first}!=null){ + sqlManager.updateById(${classInfo.className?uncap_first}); + return ApiReturnUtil.success("编辑成功"); + }else{ + sqlManager.insert(${classInfo.className?uncap_first}); + return ApiReturnUtil.error("保存成功"); + } + } + + /** + * 删除 + */ + @PostMapping("/delete") + public Object delete(int id){ + ${classInfo.className} ${classInfo.className?uncap_first}=sqlManager.unique(${classInfo.className}.class,id); + if(${classInfo.className?uncap_first}!=null){ + sqlManager.deleteById(id); + return ApiReturnUtil.success("删除成功"); + }else{ + return ApiReturnUtil.error("没有找到该对象"); + } + } + + /** + * 查询 + */ + @PostMapping("/find") + public Object find(int id){ + ${classInfo.className} ${classInfo.className?uncap_first}=sqlManager.unique(${classInfo.className}.class,id); + if(${classInfo.className?uncap_first}!=null){ + return ApiReturnUtil.success(${classInfo.className?uncap_first}); + }else{ + return ApiReturnUtil.error("没有找到该对象"); + } + } + + /** + * 分页查询 + */ + @PostMapping("/list") + public Object list(${classInfo.className} ${classInfo.className?uncap_first}, + @RequestParam(required = false, defaultValue = "0") int pageNumber, + @RequestParam(required = false, defaultValue = "10") int pageSize) { + List<${classInfo.className}> list = sqlManager.query(${classInfo.className}.class).select(); + return ApiReturnUtil.error(list); + } + +} diff --git a/generator-web/src/main/resources/templates/xxl-code-generator/beetlentity.ftl b/generator-web/src/main/resources/templates/xxl-code-generator/beetlentity.ftl new file mode 100644 index 0000000..9a272ee --- /dev/null +++ b/generator-web/src/main/resources/templates/xxl-code-generator/beetlentity.ftl @@ -0,0 +1,24 @@ +import java.io.Serializable; +import lombok.Data; +import java.util.Date; +import java.util.List; + +/** +* ${classInfo.classComment} +* @author ${authorName} ${.now?string('yyyy-MM-dd')} +*/ +@Data +public class ${classInfo.className} implements Serializable { + private static final long serialVersionUID = 1L; + +<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> +<#list classInfo.fieldList as fieldItem > + /** + * ${fieldItem.fieldComment} + */ + private ${fieldItem.fieldClass} ${fieldItem.fieldName}; + + + + +} \ No newline at end of file diff --git a/generator-web/src/main/resources/templates/xxl-code-generator/beetlmd.ftl b/generator-web/src/main/resources/templates/xxl-code-generator/beetlmd.ftl new file mode 100644 index 0000000..2dcdb64 --- /dev/null +++ b/generator-web/src/main/resources/templates/xxl-code-generator/beetlmd.ftl @@ -0,0 +1,31 @@ +sample +=== + +select #use("cols")# from ${classInfo.tableName} where #use("condition")# + +cols +=== +<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + <#list classInfo.fieldList as fieldItem > + `${fieldItem.columnName}`<#if fieldItem_has_next>, + + + +updateSample +=== +<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + <#list classInfo.fieldList as fieldItem > + `${fieldItem.columnName}=#${fieldItem.fieldName}#`<#if fieldItem_has_next>, + + + +condition +=== + 1 = 1 +<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + <#list classInfo.fieldList as fieldItem > + @if(!isEmpty(${fieldItem.fieldName})){ + and `${fieldItem.columnName}`=#${fieldItem.fieldName}# + @} + + diff --git a/generator-web/src/main/resources/templates/xxl-code-generator/entity.ftl b/generator-web/src/main/resources/templates/xxl-code-generator/entity.ftl index 6da44db..d0c8e1b 100644 --- a/generator-web/src/main/resources/templates/xxl-code-generator/entity.ftl +++ b/generator-web/src/main/resources/templates/xxl-code-generator/entity.ftl @@ -1,10 +1,3 @@ -<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> - <#list classInfo.fieldList as fieldItem > - <#if fieldItem.fieldClass == "Date"> - <#assign importDdate = true /> - - - import java.io.Serializable; import javax.persistence.*; import lombok.Data; diff --git a/generator-web/src/main/resources/templates/xxl-code-generator/jpacontroller.ftl b/generator-web/src/main/resources/templates/xxl-code-generator/jpacontroller.ftl index 6b7e7eb..c8126ef 100644 --- a/generator-web/src/main/resources/templates/xxl-code-generator/jpacontroller.ftl +++ b/generator-web/src/main/resources/templates/xxl-code-generator/jpacontroller.ftl @@ -47,7 +47,7 @@ public class ${classInfo.className}Controller { * 查询 */ @GetMapping("/find") - public Object load(int id){ + public Object find(int id){ Optional<${classInfo.className}> ${classInfo.className?uncap_first}=${classInfo.className?uncap_first}Repository.findById(id); if(${classInfo.className?uncap_first}.isPresent()){ return ApiReturnUtil.success(${classInfo.className?uncap_first}.get()); diff --git a/generator-web/src/main/resources/templates/xxl-code-generator/jtdaoimpl.ftl b/generator-web/src/main/resources/templates/xxl-code-generator/jtdaoimpl.ftl index 2351469..95e1211 100644 --- a/generator-web/src/main/resources/templates/xxl-code-generator/jtdaoimpl.ftl +++ b/generator-web/src/main/resources/templates/xxl-code-generator/jtdaoimpl.ftl @@ -39,7 +39,7 @@ public class ${classInfo.className}DaoImpl implements I${classInfo.className}Dao @Override public ${classInfo.className} findById(int id) { - List<${classInfo.className}> list = jdbcTemplate.query("select * from ${classInfo.tableName} where <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0><#list classInfo.fieldList as fieldItem ><#if fieldItem_index = 0>${fieldItem.columnName}=?<#break > = ?", new Object[]{id}, new BeanPropertyRowMapper(${classInfo.className}.class)); + List<${classInfo.className}> list = jdbcTemplate.query("select * from ${classInfo.tableName} where <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0><#list classInfo.fieldList as fieldItem ><#if fieldItem_index = 0>${fieldItem.columnName}=?<#break > = ?", new Object[]{id}, new BeanPropertyRowMapper<${classInfo.className}>(${classInfo.className}.class)); if(list!=null && list.size()>0){ ${classInfo.className} ${classInfo.className?uncap_first} = list.get(0); return ${classInfo.className?uncap_first}; @@ -50,7 +50,7 @@ public class ${classInfo.className}DaoImpl implements I${classInfo.className}Dao @Override public List<${classInfo.className}> findAllList(Map params) { - List<${classInfo.className}> list = jdbcTemplate.query("select * from ${classInfo.tableName}", new Object[]{}, new BeanPropertyRowMapper(${classInfo.className}.class)); + List<${classInfo.className}> list = jdbcTemplate.query("select * from ${classInfo.tableName}", new Object[]{}, new BeanPropertyRowMapper<${classInfo.className}>(${classInfo.className}.class)); if(list!=null && list.size()>0){ return list; }else{ diff --git a/generator-web/src/main/resources/templates/xxl-code-generator/model.ftl b/generator-web/src/main/resources/templates/xxl-code-generator/model.ftl index d9e4675..79b2503 100644 --- a/generator-web/src/main/resources/templates/xxl-code-generator/model.ftl +++ b/generator-web/src/main/resources/templates/xxl-code-generator/model.ftl @@ -1,10 +1,3 @@ -<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> - <#list classInfo.fieldList as fieldItem > - <#if fieldItem.fieldClass == "Date"> - <#assign importDdate = true /> - - - import java.io.Serializable; import java.util.Date; import java.util.List; @@ -14,7 +7,7 @@ import java.util.List; * @author ${authorName} ${.now?string('yyyy-MM-dd')} */ public class ${classInfo.className} implements Serializable { - private static final long serialVersionUID = 42L; + private static final long serialVersionUID = 1L; <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> <#list classInfo.fieldList as fieldItem >