maven依赖
com.baomidou mybatis-plus-generator 3.3.2 org.freemarker freemarker 2.3.30 com.baomidou mybatis-plus-boot-starter 3.4.3.1 mysql mysql-connector-java 8.0.13
代码生成器
package com.authine.cloudpivot.ext.util;
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 MyBatisPlusGenerator {
static String packageName = "com.authine.cloudpivot.ext"; // 当前包名
static String author = "zhouzhicheng"; // 作者
static String sqlUrl = "mysql://120.79.224.149:3306/"; // 数据库类型及地址
static String sqlDb = "cloudpivot"; // 数据库名
static String sqlUser = "root";
static String sqlPassword = "test123456";
static String table = "iavz8_manufacturing_subsidiary,iavz8_reviewer"; // 表,用逗号隔开
static String prefix = "iavz8_"; //表名前缀 生成代码类名去除该前缀
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); //项目路径
gc.setAuthor(author);
gc.setOpen(false);
gc.setServiceName("%sService");
gc.setFileOverride(true); //覆盖原有文件
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:" + sqlUrl + sqlDb + "?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(sqlUser);
dsc.setPassword(sqlPassword);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(packageName);
//pc.setEntity("entity");
mpg.setPackageInfo(pc);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
//templateConfig.setXml(null); // 不生成MapperXML
templateConfig.setController(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setRestControllerStyle(true);
strategy.setEntityTableFieldAnnotationEnable(true);
strategy.setInclude(table.split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(prefix); // 表前缀,如t_user,没有就注释掉此行
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
mybatis-plus yml配置
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/*Mapper.xml
mapper类未加@Mapper注解时需在主启动类统一注解@MapperScan(basePackages = {"com.authine.cloudpivot.ext.mapper"})



