简介
尽管 java 的代码生成工具有很多,可是很多时候不是本人喜爱的格调,改起来比拟艰难,所以我筹备从零和大家一起搭建一套基于 springboot3.0 的框架,
这次就先搞定一套代码生成性能,后续再一直的欠缺其它
咱们应用到的三方库:
- beelt 模版引擎,用于生成代码。官网:http://ibeetl.com
- mybatis-plug 官网:https://www.baomidou.com/
开始
第一步,创立一个 maven 我的项目,而后在 pom.xml 中引入相干依赖,用到数据库驱动,阿里的数据库连接池等
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>springboot-generate</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>20</maven.compiler.source> <maven.compiler.target>20</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.6</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.5.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- https://mvnrepository.com/artifact/com.ibeetl/beetl --> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl-springboot-starter-jdk17</artifactId> <version>3.15.0.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
第二步,在 java 文件夹下创立个包 com.light
,并在下创立启动入口 Application.java
,留神不要间接在 java 下创立,代码
@MapperScan("com.light.business.*.mapper") @EnableBeetl //会拦挡.btl文件应用Beetl语法解析@RestController@SpringBootApplicationpublic class Application { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }}
第三步,去实现代码生成,思路是通过一个页面(这样可视化,看起来更直观)来展现能够生成的表,抉择后调用一个接口生成到我的项目中所以,咱们先来个html用于展现表名以及操作
咱们在 resouces
下创立 templates/generate/index.btl
文件,templates
是缺省目录,咱们就间接用,而index.btl
不必.html
因为咱们用模板引擎。
能够看到,很简略展现一下表名称,而后能够抉择提交
<head> <meta charset="UTF-8"> <title>代码主动生成器</title></head><style> body { display: flex; flex-direction: column; align-items: center; padding: 25px; }</style><body> <h1> 欢送应用 Light 代码一键生成器 </h1> <div style="min-width: 450px"> <div> <input type="button" style="margin-right:45px" onclick="selectReverse()" value="反选"> <input type="button" value="提交并生成代码" onclick="document.tableForm.submit()"> </div> <div style="margin-top: 12px;margin-bottom: 2px"> <b>请抉择要生成的数据表:</b> </div> <form name="tableForm" method="post" action="index"> <% for(tableName in tableNames) { println("<input type='checkbox' name='table' value='" + tableName + "'>" + tableName + "<br>"); } %> </form> </div> <script> function selectReverse() { let tables = document.getElementsByName("table"); for (let table of tables) { table.checked = !table.checked; } } </script></body>
第四步,因为上一步中须要展现表名,所以应该要一个查问表名称的办法,咱们在 com.light
下创立 common.generate.service.GenerateService.java
,而后增加查问表名的办法
@Autowiredprivate JdbcTemplate jdbcTemplate;public List<String> getTableNames(String tableSchema) { String sql = "SELECT table_name as tableName FROM INFORMATION_SCHEMA.TABLES\n" + "WHERE table_schema = '" + tableSchema + "'"; return jdbcTemplate.query(sql, new RowMapper<String>() { @Override public String mapRow(ResultSet resultSet, int i) throws SQLException { return resultSet.getString(1); } });}
第五步,咱们在 com.light
下创立common.generate.controller.GenerateController
,来用于拜访,如果有抉择了表,就会调用生成性能
@Value("${spring.datasource.url}")private String databaseUrl;@Autowiredprivate GenerateService generateService;//引入包的前缀private static String comPath = "com.light.business";//文件生成的门路private static String[] filePath = new String[]{ System.getProperty("user.dir"), "src", "main", "java", "com", "light", "business" };@RequestMapping(value = "/index")public ModelAndView index(String[] table) throws IOException { String schema = databaseUrl.substring(databaseUrl.lastIndexOf("/") + 1, databaseUrl.indexOf("?")); if (table != null) { generateService.generation(Util.addFileSeparator(filePath), comPath, schema, Arrays.asList(table)); } ModelAndView view = new ModelAndView(); view.setViewName("/generate/index.btl"); view.addObject("tableNames", generateService.getTableNames(schema)); return view;}
第六步,能够看到外围就是 service
的 generation
办法,所以在 GenerateService.java
中增加办法
public void generation(String filePath, String comPath, String schema, List<String> tableNames) throws IOException { //咱们在 resouces下创立个 beetl-back-end 用于放模板 ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("beetl-back-end"); Configuration cfg = Configuration.defaultConfiguration(); //加载模板组 GroupTemplate gt = new GroupTemplate(resourceLoader, cfg); //获取表以及其字段等信息 List<TableInfo> tableInfoList = getTableInfoList(schema, tableNames); System.out.println("generate start"); for (TableInfo tableInfo: tableInfoList) { //生成PO... doPo(gt, tableInfo, comPath, filePath); doVo(gt, tableInfo, comPath, filePath); doMapperJava(gt, tableInfo, comPath, filePath); doMapperXml(gt, tableInfo, comPath, filePath); doService(gt, tableInfo, comPath, filePath); doController(gt, tableInfo, comPath, filePath); } System.out.println("generate over");}
其它代码就不贴出来了,开源的,能够本人到 github 上。
以后能够生成 controller, service, mapjava, mapxml, PO, VO
思考
预留了依据正文生成对应字段的枚举性能,因为还在思考如何才正当。那么是否还能够依据数据字段信息来生成校验性能呢?或者你认为还有什么是须要生成的呢?而后咱们再来持续扩大
应用
下载我的项目后,配置 application.yml
中的数据库连贯
启动我的项目后在浏览器中输出 http://localhost:8888/generate/index
即可拜访代码生成入口,咱们全选后点击提交
而后就实现了,能够看到我的项目的 com.light
下创立了个 business
目录,外面就是生成的代码了,所有的性能就曾经实现了
如果增加表或批改了表,点击须要的表从新生成即可,PO、VO等会笼罩生成,具体在 com.light.common.generate.Config.java
中配置了
测试
因为咱们从零开始创立的,所以还没有 swagger 等等,postman 一个一个测试太慢了,所以咱们这里借助前端主动生成性能来测试
因为前端是在线生成,所以拜访本地存在跨域问题,那们在本地咱们先容许任何域拜访,在com.light.common
下创立 config.CustomCorsConfiguration.java
,简略配置:
@Configurationpublic class CustomCorsConfiguration implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 放行哪些原始域 .allowedOrigins("*") // 放行哪些申请形式 .allowedMethods("*") // 放行哪些原始申请头部信息 .allowedHeaders("*"); }}
重启我的项目
关上前端代码生成器网站:https://light2f.com 到我的我的项目下,点击 AI创立我的项目,输出数据库信息或者导入数据库构造
如果没有配置过端口与path应该输出如下根本门路信息。
后抉择或主动生成一套母版应用
因为咱们springboot框架是从零搭建的,所以还没有封装 response,所以将模版批改红框中数据为上面
间接确定生成
点击刚刚生成的我的项目点击眼睛进入预览
咱们还没有token与登录,所以间接点击左边跳过
ok, 接口字段等曾经接入,能够测试了。
1.gif
总结
一个实现的从前到后性能曾经实现了,然而理论应用中会有平安问题、权限问题等等,那么咱们那一期再欠缺一下 token 等,或者等小伙伴提需要
一起一步一步欠缺后继搭建
地址
springboot-generate 开源地址:https://github.com/yangaijun/springboot-generate
前端在线生成网站:https://light2f.com