├── HELP.md ├── README.md ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── javaboy │ │ └── generate_code │ │ ├── GenerateCodeApplication.java │ │ ├── controller │ │ ├── DbController.java │ │ └── GenerateCodeController.java │ │ ├── model │ │ ├── ColumnClass.java │ │ ├── Db.java │ │ ├── RespBean.java │ │ └── TableClass.java │ │ ├── service │ │ └── GenerateCodeService.java │ │ └── utils │ │ └── DBUtils.java └── resources │ ├── application.properties │ ├── static │ └── index.html │ └── templates │ ├── Controller.java.ftl │ ├── Mapper.java.ftl │ ├── Mapper.xml.ftl │ ├── Model.java.ftl │ └── Service.java.ftl └── test └── java └── org └── javaboy └── generate_code └── GenerateCodeApplicationTests.java /HELP.md: -------------------------------------------------------------------------------- 1 | # Getting Started 2 | 3 | ### Reference Documentation 4 | For further reference, please consider the following sections: 5 | 6 | * [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) 7 | * [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.4.1/maven-plugin/reference/html/) 8 | * [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.4.1/maven-plugin/reference/html/#build-image) 9 | * [Apache Freemarker](https://docs.spring.io/spring-boot/docs/2.4.1/reference/htmlsingle/#boot-features-spring-mvc-template-engines) 10 | * [Spring Web](https://docs.spring.io/spring-boot/docs/2.4.1/reference/htmlsingle/#boot-features-developing-web-applications) 11 | 12 | ### Guides 13 | The following guides illustrate how to use some features concretely: 14 | 15 | * [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/) 16 | * [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/) 17 | * [Building REST services with Spring](https://spring.io/guides/tutorials/bookmarks/) 18 | * [Accessing data with MySQL](https://spring.io/guides/gs/accessing-data-mysql/) 19 | 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 扫码加微信(微信ID:**a_java_boy3**),进 Spring Boot 讨论群。 2 | 3 |  4 | 5 | 松哥最近正在录制新版的 Spring Boot 视频教程,讲到 Spring Boot 整合 Freemarker 时,我就在琢磨单纯讲整合和 Freemarker 用法太过于枯燥,如果能有个实际案例来展示这一技术点,就会好很多。 6 | 7 | 想来想去感觉做一个 MyBatis 逆向工程比较靠谱,一来 MyBatis 逆向工程大家在日常开发中可能多多少少都会用到,二来很多小伙伴们用 Freemarker 都是做页面模版,可能还没尝试过用它做代码模版。并且从技术上来说,这个代码模版并不难,而且比较有意思。 8 | 9 | 我们先来看一张效果图: 10 | 11 |  12 | 13 | 项目地址: 14 | 15 | - GitHub:https://github.com/lenve/generate_code 16 | - Gitee:https://gitee.com/lenve/generate_code 17 | 18 | ## 技术架构 19 | 20 | 上图中的页面是用 Vue 开发的,因为只有一个页面,所以就没有使用 SPA(单页面)的形式,因为比较折腾,前端 UI 控件使用了 ElementUI,网络请求还是 Axios。 21 | 22 | 服务端当然就是大家所熟知的 Spring Boot,服务端的操作其实并不难,主要是利用 JDBC 获取数据库中的各种元数据信息。 23 | 24 | 页面模版则是 Freemarker: 25 | 26 |  27 | 28 | ## 使用步骤 29 | 30 | 1. clone 项目到本地:`git clone https://github.com/lenve/generate_code.git` 31 | 2. 使用 IntelliJ IDEA/Eclipse 等工具打开项目并启动。 32 | 3. 项目启动成功后,输入 http://localhost:8080/ 访问首页。 33 | 4. 首先输入数据库信息,然后点击**连接数据库**按钮。 34 | 5. 当数据库信息读取成功后,输入要生成代码的包名,然后点击**配置**按钮。 35 | 6. 接下来表格中会展示出所有的表信息和生成的各种类名称,如需修改,可以直接修改,修改完成后,点击生成代码。 36 | 7. 代码生成后,会返回代码生成的位置,点击查看即可。 37 | 38 | ## 手把手教程 39 | 40 | 松哥为这个代码生成工具录制了一套视频教程,手把手教大家做这样一个工具,今天我们先来看前三个学习下,看看是不是真的 so easy! 41 | 42 | [项目视频](https://mp.weixin.qq.com/s/2Ovqwn1hd40D7FtKE60HKw) 43 | 44 | 好啦,今天就先和大家分享这么多。如果小伙伴们觉得视频风格还不错,也可以看看松哥的 [Spring Boot+Vue+微人事视频教程](https://mp.weixin.qq.com/s/aIyP77WrrswWNXaueBXj7w),这套视频中的 Spring Boot 部分新版目前正在录制。 -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.4.1 9 | 10 | 11 | org.javaboy 12 | generate_code 13 | 0.0.1-SNAPSHOT 14 | generate_code 15 | Demo project for Spring Boot 16 | 17 | 18 | 11 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-freemarker 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | com.google.guava 32 | guava 33 | 30.1-jre 34 | 35 | 36 | 37 | mysql 38 | mysql-connector-java 39 | runtime 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/GenerateCodeApplication.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class GenerateCodeApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(GenerateCodeApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/controller/DbController.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code.controller; 2 | 3 | import com.google.common.base.CaseFormat; 4 | import org.javaboy.generate_code.model.Db; 5 | import org.javaboy.generate_code.model.RespBean; 6 | import org.javaboy.generate_code.model.TableClass; 7 | import org.javaboy.generate_code.utils.DBUtils; 8 | import org.springframework.web.bind.annotation.PostMapping; 9 | import org.springframework.web.bind.annotation.RequestBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.sql.Connection; 13 | import java.sql.DatabaseMetaData; 14 | import java.sql.ResultSet; 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | /** 20 | * @author 江南一点雨 21 | * @微信公众号 江南一点雨 vhr 22 | * @网站 http://www.itboyhub.com 23 | * @国际站 http://www.javaboy.org 24 | * @微信 a_java_boy 25 | * @GitHub https://github.com/lenve/vhr 26 | * @Gitee https://gitee.com/lenve/vhr 27 | */ 28 | @RestController 29 | public class DbController { 30 | @PostMapping("/connect") 31 | public RespBean connect(@RequestBody Db db) { 32 | Connection con = DBUtils.initDb(db); 33 | if (con != null) { 34 | return RespBean.ok("数据库连接成功"); 35 | } 36 | return RespBean.error("数据库连接失败"); 37 | } 38 | 39 | @PostMapping("/config") 40 | public RespBean config(@RequestBody Map map) { 41 | String packageName = map.get("packageName"); 42 | try { 43 | Connection connection = DBUtils.getConnection(); 44 | DatabaseMetaData metaData = connection.getMetaData(); 45 | ResultSet tables = metaData.getTables(connection.getCatalog(), null, null, null); 46 | List tableClassList = new ArrayList<>(); 47 | while (tables.next()) { 48 | TableClass tableClass = new TableClass(); 49 | tableClass.setPackageName(packageName); 50 | String table_name = tables.getString("TABLE_NAME"); 51 | String modelName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, table_name); 52 | tableClass.setTableName(table_name); 53 | tableClass.setModelName(modelName); 54 | tableClass.setControllerName(modelName + "Controller"); 55 | tableClass.setMapperName(modelName + "Mapper"); 56 | tableClass.setServiceName(modelName+"Service"); 57 | tableClassList.add(tableClass); 58 | } 59 | return RespBean.ok("数据库信息读取成功", tableClassList); 60 | } catch (Exception e) { 61 | e.printStackTrace(); 62 | } 63 | return RespBean.error("数据库信息读取失败"); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/controller/GenerateCodeController.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code.controller; 2 | 3 | import org.javaboy.generate_code.model.RespBean; 4 | import org.javaboy.generate_code.model.TableClass; 5 | import org.javaboy.generate_code.service.GenerateCodeService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | import java.util.List; 13 | 14 | /** 15 | * @author 江南一点雨 16 | * @微信公众号 江南一点雨 vhr generate-code 17 | * @网站 http://www.itboyhub.com 18 | * @国际站 http://www.javaboy.org 19 | * @微信 a_java_boy 20 | * @GitHub https://github.com/lenve 21 | * @Gitee https://gitee.com/lenve 22 | */ 23 | @RestController 24 | public class GenerateCodeController { 25 | @Autowired 26 | GenerateCodeService generateCodeService; 27 | 28 | 29 | @PostMapping("/generateCode") 30 | public RespBean generateCode(@RequestBody List tableClassList, HttpServletRequest req) { 31 | return generateCodeService.generateCode(tableClassList, req.getServletContext().getRealPath("/")); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/model/ColumnClass.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code.model; 2 | 3 | /** 4 | * @author 江南一点雨 5 | * @微信公众号 江南一点雨 6 | * @网站 http://www.itboyhub.com 7 | * @国际站 http://www.javaboy.org 8 | * @微信 a_java_boy 9 | * @GitHub https://github.com/lenve 10 | * @Gitee https://gitee.com/lenve 11 | */ 12 | public class ColumnClass { 13 | private String propertyName; 14 | private String columnName; 15 | private String type; 16 | private String remark; 17 | private Boolean isPrimary; 18 | 19 | @Override 20 | public String toString() { 21 | return "ColumnClass{" + 22 | "propertyName='" + propertyName + '\'' + 23 | ", columnName='" + columnName + '\'' + 24 | ", type='" + type + '\'' + 25 | ", remark='" + remark + '\'' + 26 | ", isPrimary=" + isPrimary + 27 | '}'; 28 | } 29 | 30 | public String getPropertyName() { 31 | return propertyName; 32 | } 33 | 34 | public void setPropertyName(String propertyName) { 35 | this.propertyName = propertyName; 36 | } 37 | 38 | public String getColumnName() { 39 | return columnName; 40 | } 41 | 42 | public void setColumnName(String columnName) { 43 | this.columnName = columnName; 44 | } 45 | 46 | public String getType() { 47 | return type; 48 | } 49 | 50 | public void setType(String type) { 51 | this.type = type; 52 | } 53 | 54 | public String getRemark() { 55 | return remark; 56 | } 57 | 58 | public void setRemark(String remark) { 59 | this.remark = remark; 60 | } 61 | 62 | public Boolean getPrimary() { 63 | return isPrimary; 64 | } 65 | 66 | public void setPrimary(Boolean primary) { 67 | isPrimary = primary; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/model/Db.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code.model; 2 | 3 | /** 4 | * @author 江南一点雨 5 | * @微信公众号 江南一点雨 6 | * @网站 http://www.itboyhub.com 7 | * @国际站 http://www.javaboy.org 8 | * @微信 a_java_boy 9 | * @GitHub https://github.com/lenve 10 | * @Gitee https://gitee.com/lenve 11 | */ 12 | public class Db { 13 | private String username; 14 | private String password; 15 | private String url; 16 | 17 | public String getUsername() { 18 | return username; 19 | } 20 | 21 | public void setUsername(String username) { 22 | this.username = username; 23 | } 24 | 25 | public String getPassword() { 26 | return password; 27 | } 28 | 29 | public void setPassword(String password) { 30 | this.password = password; 31 | } 32 | 33 | public String getUrl() { 34 | return url; 35 | } 36 | 37 | public void setUrl(String url) { 38 | this.url = url; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/model/RespBean.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code.model; 2 | 3 | /** 4 | * @author 江南一点雨 5 | * @微信公众号 江南一点雨 6 | * @网站 http://www.itboyhub.com 7 | * @国际站 http://www.javaboy.org 8 | * @微信 a_java_boy 9 | * @GitHub https://github.com/lenve 10 | * @Gitee https://gitee.com/lenve 11 | */ 12 | public class RespBean { 13 | private Integer status; 14 | private String msg; 15 | private Object obj; 16 | 17 | public static RespBean ok(String msg,Object obj) { 18 | return new RespBean(200, msg, obj); 19 | } 20 | 21 | 22 | public static RespBean ok(String msg) { 23 | return new RespBean(200, msg, null); 24 | } 25 | 26 | 27 | public static RespBean error(String msg,Object obj) { 28 | return new RespBean(500, msg, obj); 29 | } 30 | 31 | 32 | public static RespBean error(String msg) { 33 | return new RespBean(500, msg, null); 34 | } 35 | 36 | private RespBean() { 37 | } 38 | 39 | private RespBean(Integer status, String msg, Object obj) { 40 | this.status = status; 41 | this.msg = msg; 42 | this.obj = obj; 43 | } 44 | 45 | public Integer getStatus() { 46 | return status; 47 | } 48 | 49 | public void setStatus(Integer status) { 50 | this.status = status; 51 | } 52 | 53 | public String getMsg() { 54 | return msg; 55 | } 56 | 57 | public void setMsg(String msg) { 58 | this.msg = msg; 59 | } 60 | 61 | public Object getObj() { 62 | return obj; 63 | } 64 | 65 | public void setObj(Object obj) { 66 | this.obj = obj; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/model/TableClass.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code.model; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author 江南一点雨 7 | * @微信公众号 江南一点雨 vhr 8 | * @网站 http://www.itboyhub.com 9 | * @国际站 http://www.javaboy.org 10 | * @微信 a_java_boy 11 | * @GitHub https://github.com/lenve 12 | * @Gitee https://gitee.com/lenve 13 | */ 14 | public class TableClass { 15 | private String tableName; 16 | private String modelName; 17 | private String serviceName; 18 | private String mapperName; 19 | private String controllerName; 20 | private String packageName; 21 | private List columns; 22 | 23 | @Override 24 | public String toString() { 25 | return "TableClass{" + 26 | "tableName='" + tableName + '\'' + 27 | ", modelName='" + modelName + '\'' + 28 | ", serviceName='" + serviceName + '\'' + 29 | ", mapperName='" + mapperName + '\'' + 30 | ", controllerName='" + controllerName + '\'' + 31 | ", packageName='" + packageName + '\'' + 32 | ", columns=" + columns + 33 | '}'; 34 | } 35 | 36 | public String getTableName() { 37 | return tableName; 38 | } 39 | 40 | public void setTableName(String tableName) { 41 | this.tableName = tableName; 42 | } 43 | 44 | public String getModelName() { 45 | return modelName; 46 | } 47 | 48 | public void setModelName(String modelName) { 49 | this.modelName = modelName; 50 | } 51 | 52 | public String getServiceName() { 53 | return serviceName; 54 | } 55 | 56 | public void setServiceName(String serviceName) { 57 | this.serviceName = serviceName; 58 | } 59 | 60 | public String getMapperName() { 61 | return mapperName; 62 | } 63 | 64 | public void setMapperName(String mapperName) { 65 | this.mapperName = mapperName; 66 | } 67 | 68 | public String getControllerName() { 69 | return controllerName; 70 | } 71 | 72 | public void setControllerName(String controllerName) { 73 | this.controllerName = controllerName; 74 | } 75 | 76 | public String getPackageName() { 77 | return packageName; 78 | } 79 | 80 | public void setPackageName(String packageName) { 81 | this.packageName = packageName; 82 | } 83 | 84 | public List getColumns() { 85 | return columns; 86 | } 87 | 88 | public void setColumns(List columns) { 89 | this.columns = columns; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/service/GenerateCodeService.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code.service; 2 | 3 | import com.google.common.base.CaseFormat; 4 | import freemarker.cache.ClassTemplateLoader; 5 | import freemarker.template.Configuration; 6 | import freemarker.template.Template; 7 | import freemarker.template.TemplateException; 8 | import org.javaboy.generate_code.model.ColumnClass; 9 | import org.javaboy.generate_code.model.RespBean; 10 | import org.javaboy.generate_code.model.TableClass; 11 | import org.javaboy.generate_code.utils.DBUtils; 12 | import org.springframework.context.annotation.ComponentScan; 13 | import org.springframework.stereotype.Service; 14 | 15 | import java.io.*; 16 | import java.sql.Connection; 17 | import java.sql.DatabaseMetaData; 18 | import java.sql.ResultSet; 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | /** 23 | * @author 江南一点雨 24 | * @微信公众号 江南一点雨 25 | * @网站 http://www.itboyhub.com 26 | * @国际站 http://www.javaboy.org 27 | * @微信 a_java_boy 28 | * @GitHub https://github.com/lenve 29 | * @Gitee https://gitee.com/lenve 30 | */ 31 | @Service 32 | public class GenerateCodeService { 33 | 34 | Configuration cfg = null; 35 | 36 | { 37 | cfg = new Configuration(Configuration.VERSION_2_3_30); 38 | cfg.setTemplateLoader(new ClassTemplateLoader(GenerateCodeService.class, "/templates")); 39 | cfg.setDefaultEncoding("UTF-8"); 40 | } 41 | 42 | public RespBean generateCode(List tableClassList, String realPath) { 43 | try { 44 | Template modelTemplate = cfg.getTemplate("Model.java.ftl"); 45 | Template mapperJavaTemplate = cfg.getTemplate("Mapper.java.ftl"); 46 | Template mapperXmlTemplate = cfg.getTemplate("Mapper.xml.ftl"); 47 | Template serviceTemplate = cfg.getTemplate("Service.java.ftl"); 48 | Template controllerTemplate = cfg.getTemplate("Controller.java.ftl"); 49 | Connection connection = DBUtils.getConnection(); 50 | DatabaseMetaData metaData = connection.getMetaData(); 51 | for (TableClass tableClass : tableClassList) { 52 | ResultSet columns = metaData.getColumns(connection.getCatalog(), null, tableClass.getTableName(), null); 53 | ResultSet primaryKeys = metaData.getPrimaryKeys(connection.getCatalog(), null, tableClass.getTableName()); 54 | List columnClassList = new ArrayList<>(); 55 | while (columns.next()) { 56 | String column_name = columns.getString("COLUMN_NAME"); 57 | String type_name = columns.getString("TYPE_NAME"); 58 | String remarks = columns.getString("REMARKS"); 59 | ColumnClass columnClass = new ColumnClass(); 60 | columnClass.setRemark(remarks); 61 | columnClass.setColumnName(column_name); 62 | columnClass.setType(type_name); 63 | columnClass.setPropertyName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, column_name)); 64 | primaryKeys.first(); 65 | while (primaryKeys.next()) { 66 | String pkName = primaryKeys.getString("COLUMN_NAME"); 67 | if (column_name.equals(pkName)) { 68 | columnClass.setPrimary(true); 69 | } 70 | } 71 | columnClassList.add(columnClass); 72 | } 73 | tableClass.setColumns(columnClassList); 74 | String path = realPath + "/" + tableClass.getPackageName().replace(".", "/"); 75 | generate(modelTemplate, tableClass, path + "/model/"); 76 | generate(mapperJavaTemplate, tableClass, path + "/mapper/"); 77 | generate(mapperXmlTemplate, tableClass, path + "/mapper/"); 78 | generate(serviceTemplate, tableClass, path + "/service/"); 79 | generate(controllerTemplate, tableClass, path + "/controller/"); 80 | } 81 | return RespBean.ok("代码已生成", realPath); 82 | } catch (Exception e) { 83 | e.printStackTrace(); 84 | } 85 | return RespBean.error("代码生成失败"); 86 | } 87 | 88 | private void generate(Template template, TableClass tableClass, String path) throws IOException, TemplateException { 89 | File folder = new File(path); 90 | if (!folder.exists()) { 91 | folder.mkdirs(); 92 | } 93 | String fileName = path + "/" + tableClass.getModelName() + template.getName().replace(".ftl", "").replace("Model", ""); 94 | FileOutputStream fos = new FileOutputStream(fileName); 95 | OutputStreamWriter out = new OutputStreamWriter(fos); 96 | template.process(tableClass,out); 97 | fos.close(); 98 | out.close(); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/org/javaboy/generate_code/utils/DBUtils.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code.utils; 2 | 3 | import org.javaboy.generate_code.model.Db; 4 | 5 | import java.sql.Connection; 6 | import java.sql.DriverManager; 7 | import java.sql.SQLException; 8 | 9 | /** 10 | * @author 江南一点雨 11 | * @微信公众号 江南一点雨 12 | * @网站 http://www.itboyhub.com 13 | * @国际站 http://www.javaboy.org 14 | * @微信 a_java_boy 15 | * @GitHub https://github.com/lenve 16 | * @Gitee https://gitee.com/lenve 17 | */ 18 | public class DBUtils { 19 | private static Connection connection; 20 | 21 | public static Connection getConnection() { 22 | return connection; 23 | } 24 | 25 | public static Connection initDb(Db db) { 26 | if (connection == null) { 27 | try { 28 | Class.forName("com.mysql.cj.jdbc.Driver"); 29 | connection = DriverManager.getConnection(db.getUrl(), db.getUsername(), db.getPassword()); 30 | } catch (ClassNotFoundException | SQLException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | return connection; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 代码生成工具 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 数据库用户名: 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 数据库密码: 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 数据库连接地址: 35 | 36 | 37 | 38 | jdbc:mysql:// 39 | 40 | ?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai 41 | 42 | 43 | 44 | 45 | 46 | 47 | 连接数据库 48 | *{{msg}}* 49 | 50 | 配置 51 | 52 | 57 | 61 | 62 | 65 | 66 | 67 | 68 | 69 | 71 | 72 | 73 | 74 | 75 | 77 | 78 | 79 | 80 | 81 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 生成代码 90 | *{{result}}* 91 | {{codePath}} 92 | 93 | 94 | 154 | 155 | -------------------------------------------------------------------------------- /src/main/resources/templates/Controller.java.ftl: -------------------------------------------------------------------------------- 1 | package ${packageName}.controller; 2 | 3 | import ${packageName}.model.${modelName}; 4 | import ${packageName}.service.${serviceName}; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import java.util.List; 9 | 10 | @RestController 11 | public class ${controllerName}{ 12 | 13 | @Autowired 14 | ${serviceName} ${serviceName?uncap_first}; 15 | 16 | @GetMapping("/${modelName?lower_case}s") 17 | public List<${modelName}> getAll${modelName}s(){ 18 | return ${serviceName?uncap_first}.getAll${modelName}s(); 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/resources/templates/Mapper.java.ftl: -------------------------------------------------------------------------------- 1 | package ${packageName}.mapper; 2 | 3 | import ${packageName}.model.${modelName}; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import java.util.List; 6 | 7 | @Mapper 8 | public interface ${mapperName}{ 9 | List<${modelName}> getAll${modelName}s(); 10 | } -------------------------------------------------------------------------------- /src/main/resources/templates/Mapper.xml.ftl: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | <#list columns as column> 8 | <<#if column.primary??>id<#else>result#if> column="${column.columnName}" property="${column.propertyName?uncap_first}" jdbcType="<#if column.type='INT'>INTEGER<#elseif column.type='DATETIME'>TIMESTAMP<#elseif column.type='TEXT'>VARCHAR<#else>${column.type}#if>" /> 9 | #list> 10 | 11 | 12 | 13 | select * from ${tableName}; 14 | 15 | -------------------------------------------------------------------------------- /src/main/resources/templates/Model.java.ftl: -------------------------------------------------------------------------------- 1 | package ${packageName}.model; 2 | 3 | import java.util.Date; 4 | 5 | public class ${modelName}{ 6 | 7 | <#if columns??> 8 | <#list columns as column> 9 | <#if column.type='VARCHAR'||column.type='TEXT'||column.type='CHAR'> 10 | /** 11 | * ${column.remark} 12 | */ 13 | private String ${column.propertyName?uncap_first}; 14 | #if> 15 | <#if column.type='INT'> 16 | /** 17 | * ${column.remark} 18 | */ 19 | private Integer ${column.propertyName?uncap_first}; 20 | #if> 21 | <#if column.type='DATETIME'> 22 | /** 23 | * ${column.remark} 24 | */ 25 | private Date ${column.propertyName?uncap_first}; 26 | #if> 27 | <#if column.type='BIGINT'> 28 | /** 29 | * ${column.remark} 30 | */ 31 | private Long ${column.propertyName?uncap_first}; 32 | #if> 33 | <#if column.type='DOUBLE'> 34 | /** 35 | * ${column.remark} 36 | */ 37 | private Double ${column.propertyName?uncap_first}; 38 | #if> 39 | <#if column.type='BIT'> 40 | /** 41 | * ${column.remark} 42 | */ 43 | private Boolean ${column.propertyName?uncap_first}; 44 | #if> 45 | #list> 46 | #if> 47 | <#if columns??> 48 | <#list columns as column> 49 | <#if column.type='VARCHAR'||column.type='TEXT'||column.type='CHAR'> 50 | public String get${column.propertyName}(){ 51 | return ${column.propertyName?uncap_first}; 52 | } 53 | public void set${column.propertyName}(String ${column.propertyName?uncap_first}){ 54 | this.${column.propertyName?uncap_first}=${column.propertyName?uncap_first}; 55 | } 56 | #if> 57 | <#if column.type='INT'> 58 | public Integer get${column.propertyName}(){ 59 | return ${column.propertyName?uncap_first}; 60 | } 61 | public void set${column.propertyName}(Integer ${column.propertyName?uncap_first}){ 62 | this.${column.propertyName?uncap_first}=${column.propertyName?uncap_first}; 63 | } 64 | #if> 65 | <#if column.type='DATETIME'> 66 | public Date get${column.propertyName}(){ 67 | return ${column.propertyName?uncap_first}; 68 | } 69 | public void set${column.propertyName}(Date ${column.propertyName?uncap_first}){ 70 | this.${column.propertyName?uncap_first}=${column.propertyName?uncap_first}; 71 | } 72 | #if> 73 | <#if column.type='BIGINT'> 74 | public Long get${column.propertyName}(){ 75 | return ${column.propertyName?uncap_first}; 76 | } 77 | public void set${column.propertyName}(Long ${column.propertyName?uncap_first}){ 78 | this.${column.propertyName?uncap_first}=${column.propertyName?uncap_first}; 79 | } 80 | #if> 81 | <#if column.type='DOUBLE'> 82 | public Double get${column.propertyName}(){ 83 | return ${column.propertyName?uncap_first}; 84 | } 85 | public void set${column.propertyName}(Double ${column.propertyName?uncap_first}){ 86 | this.${column.propertyName?uncap_first}=${column.propertyName?uncap_first}; 87 | } 88 | #if> 89 | <#if column.type='BIT'> 90 | public Boolean get${column.propertyName}(){ 91 | return ${column.propertyName?uncap_first}; 92 | } 93 | public void set${column.propertyName}(Boolean ${column.propertyName?uncap_first}){ 94 | this.${column.propertyName?uncap_first}=${column.propertyName?uncap_first}; 95 | } 96 | #if> 97 | #list> 98 | #if> 99 | } -------------------------------------------------------------------------------- /src/main/resources/templates/Service.java.ftl: -------------------------------------------------------------------------------- 1 | package ${packageName}.service; 2 | 3 | import ${packageName}.model.${modelName}; 4 | import ${packageName}.mapper.${mapperName}; 5 | import org.springframework.stereotype.Service; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import java.util.List; 8 | 9 | @Service 10 | public class ${serviceName}{ 11 | 12 | @Autowired 13 | ${mapperName} ${mapperName?uncap_first}; 14 | public List<${modelName}> getAll${modelName}s(){ 15 | return ${mapperName?uncap_first}.getAll${modelName}s(); 16 | } 17 | } -------------------------------------------------------------------------------- /src/test/java/org/javaboy/generate_code/GenerateCodeApplicationTests.java: -------------------------------------------------------------------------------- 1 | package org.javaboy.generate_code; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class GenerateCodeApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | --------------------------------------------------------------------------------