注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码。
在前面章节对 MyBatis Plus 的 AutoGenerator 代码生成器有了一个简单的认识,并且介绍了怎样安装它们的 Maven 依赖。本文将使用 FreeMarker 作为代码生成器的模板引擎,添加如下依赖:
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency>
下面示例将生成“user”数据表的 controller、mapper、entity、service 代码。代码如下:
package com.hxstrive.mybatis_plus.generator.demo1; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; public class MysqlGenerator { public static void main(String[] args) { String userDir = System.getProperty("user.dir"); System.out.println("userDir=" + userDir); // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/mybatis-plus-learn/src/main/java"); gc.setAuthor("hxstrive"); // 作者名称 gc.setBaseResultMap(true); // mapper.xml 生成 ResultMap gc.setBaseColumnList(true); // mapper.xml 生成 ColumnList gc.setOpen(false); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_test?useSSL=false"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("aaaaaa"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.hxstrive.mybatis.auto"); mpg.setPackageInfo(pc); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setInclude("user"); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
运行结果:
userDir=D:\learn\JavaDatabase\MyBatis\mybatis-workspaces 22:11:23.487 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...========================== 22:11:24.669 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\entity] 22:11:24.672 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\controller] 22:11:24.674 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\mapper] 22:11:24.689 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\service\impl] 22:11:25.416 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/entity.java.ftl; 文件:D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\entity\User.java 22:11:25.456 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.java.ftl; 文件:D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\mapper\UserMapper.java 22:11:25.466 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/service.java.ftl; 文件:D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\service\IUserService.java 22:11:25.474 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/serviceImpl.java.ftl; 文件:D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\service\impl\UserServiceImpl.java 22:11:25.493 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/controller.java.ftl; 文件:D:\learn\JavaDatabase\MyBatis\mybatis-workspaces/mybatis-plus-learn/src/main/java\com\hxstrive\mybatis\auto\controller\UserController.java 22:11:25.493 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================
生成的代码效果图如下:
谢谢分享
谢谢支持!