1、向pom文件添加依赖mybatisplus相关依赖
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.1</version> </dependency> <!--模板引擎反向生成代码用--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl</artifactId> <version>3.2.3.RELEASE</version> </dependency> <!--mysql依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2、配置mybatis-plus.properties文件
#此处为本项目src所在路径(代码生成器输出路径) OutputDir=E:/liangd/springboot/springboot-mp-generator/src/main/java #mapper.xml的生成位置 OutputDirXml=E:/liangd/springboot/springboot-mp-generator/src/main/resources #数据库表名(此处切不可为空,如果为空,则默认读取数据库的所有表名),也可在config文件中指定 #tableName=sys_user,sys_role,sys_permission #设置作者 author=liangd #自定义包路径 parent=com.donleo.mp.generator #数据库地址与yml配置的数据源地址一致 url=jdbc:mysql://localhost:3306/spring_security?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai userName=root password=123456 driver=com.mysql.cj.jdbc.Driver
3、编写CodeGenerator工具类
package com.donleo.mp.generator.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; /** * @author liangd * date 2020-12-16 16:19 * code mybatisplus代码生成器配置 * 生成完毕最好注释或删除此文件 */ public class CodeGenerator { public static void main(String[] args) throws InterruptedException { //用来获取Mybatis-Plus.properties文件的配置信息 final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus"); // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(rb.getString("OutputDir")); gc.setOpen(false); gc.setBaseResultMap(true); gc.setBaseColumnList(true); gc.setAuthor(rb.getString("author")); gc.setMapperName("%sMapper"); gc.setXmlName("%sMapper"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setControllerName("%sController"); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setUrl(rb.getString("url")); dsc.setDriverName(rb.getString("driver")); dsc.setUsername(rb.getString("userName")); dsc.setPassword(rb.getString("password")); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent(rb.getString("parent")); pc.setController("controller"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setEntity("model"); pc.setMapper("mapper"); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定义输入文件名称 return rb.getString("OutputDirXml") + "/mapper/" + tableInfo.getEntityName() + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setInclude("sys_user", "sys_role", "sys_permission", "sys_role_permission_relation", "sys_user_permission_relation", "sys_user_role_relation"); strategy.setTablePrefix("sys_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
4、点击运行即可生成