第一步:导入坐标
com.baomidou mybatis-plus-boot-starter 3.5.1 com.baomidou mybatis-plus-generator 3.5.1 org.apache.velocity velocity-engine-core 2.3 io.springfox springfox-boot-starter 3.0.0 org.freemarker freemarker
第二步:创建一个类:
package com.ag.generator;import com.baomidou.mybatisplus.core.mapper.baseMapper;import com.baomidou.mybatisplus.generator.FastAutoGenerator;import com.baomidou.mybatisplus.generator.config.OutputFile;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class My { public static void main(String[] args) { List tables = new ArrayList<>(); tables.add("tb_activity"); tables.add("tb_building"); tables.add("tb_car"); tables.add("tb_charge_detail"); tables.add("tb_charge_item"); tables.add("tb_community"); tables.add("tb_complaint"); tables.add("tb_device"); tables.add("tb_estate_manager"); tables.add("tb_house"); tables.add("tb_letter"); tables.add("tb_owner"); tables.add("tb_parking"); tables.add("tb_parking_use"); tables.add("tb_pet"); tables.add("tb_repair"); String property = System.getProperty("user.dir");//当前的工作目录 FastAutoGenerator.create("jdbc:mysql://localhost:3306/xiaoniu-estate?serverTimezone=GMT%2B8","root","root") .globalConfig(builder -> { builder.author("小牛") //作者 .outputDir(property+"\src\main\java") //输出路径(写到java目录) .enableSwagger() //开启swagger .commentDate("yyyy-MM-dd") .fileOverride(); //开启覆盖之前生成的文件 }) .packageConfig(builder -> { builder.parent("com.ag")// .moduleName("practice") .entity("pojo") .service("service") .serviceImpl("serviceImpl") .controller("controller") .mapper("mapper") .xml("mapper") .pathInfo(Collections.singletonMap(OutputFile.mapperXml,property+"\src\main\resources\mapper")); }) .strategyConfig(builder -> { builder.addInclude(tables) .addTablePrefix("tb_") .serviceBuilder() .formatServiceFileName("%sService") .formatServiceImplFileName("%sServiceImpl") .entityBuilder() .enableLombok() .logicDeleteColumnName("deleted") .enableTableFieldAnnotation() .controllerBuilder() .formatFileName("%sController") .enableRestStyle() .mapperBuilder() .enablebaseResultMap() //生成通用的resultMap .superClass(baseMapper.class) .formatMapperFileName("%sMapper") .enableMapperAnnotation() .formatXmlFileName("%sMapper") .controllerBuilder().enableRestStyle();//restful开启 }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); }}
结果: