package com.sf.util; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.config.rules.DbColumnType; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.sql.Types; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class GeneUtils { public static void main(String[] args) { FastAutoGenerator .create("jdbc:mysql://localhost:3306/novel-cloud?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root123456") .globalConfig(builder -> { builder.author("Qing") // 设置作者 // .fileOverride() // 覆盖已生成文件 .outputDir("src/main/java/"); // 指定输出目录 java文件的整体地址 // .outputDir("src\\main\\java\\"); // windows中修改为\\ }) .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> { int typeCode = metaInfo.getJdbcType().TYPE_CODE; if (typeCode == Types.SMALLINT) { // 自定义类型转换 // tinyInt smallInt < int < bigInt // byte short int long return DbColumnType.INTEGER; } if (typeCode == Types.TINYINT) { return DbColumnType.INTEGER; } return typeRegistry.getColumnType(metaInfo); })) .packageConfig(builder -> { builder.parent("com.sf") // 设置父包名 自定义的源代码地址 对应Application主程序入口的包名 .moduleName("") // 设置父包模块名 .pathInfo(Collections.singletonMap( OutputFile.xml, "src/main/resources/mapper")) // 设置mapperXml生成路径 .entity("po") // 实体类对应的包名 entity->po .service("service") .serviceImpl("service.impl") // 服务层实现类对应的位置 .mapper("mapper") // dao层对应的包名 dao->mapper .xml("mapper.xml") .controller("controller") // .other("other") // .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://")) .build(); }) .strategyConfig(builder -> { // sys_user t_user User UserMapper UserService // List tableList = new ArrayList<>(); // tableList.add("chapter_content"); // tableList.add("fiction_shelf"); // builder.addInclude(tableList); // 设置需要生成的表名 // builder.addInclude("chapter_content","fiction_shelf"); // 设置需要生成的表名 builder.addInclude("user_info"); // 设置需要生成的表名 // .addTablePrefix("t_", "c_"); // 设置过滤表前缀 }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); } }