用maven引入相关包
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
运行下面代码生成类即可
public class CodeGenerator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg =new AutoGenerator();
// 全局配置
GlobalConfig gc =new GlobalConfig();
String projectPath ="E:\\Bruce\\sources\\IdeaProjects\\bruce-mall-coding\\bruce-mall-coding\\bruce-mall-service\\bruce-mall-ums";
gc.setOutputDir(projectPath +"/src/main/java");
gc.setAuthor("BruceWong");
gc.setOpen(false);
gc.setFileOverride(true);
gc.setServiceName("%sService");//去Service的I前缀。
gc.setIdType(IdType.ID_WORKER);
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc =new DataSourceConfig();
dsc.setUrl("jdbc:mysql://192.168.1.4:3306/bruce-mall?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc =new PackageConfig();
pc.setParent("com.bruce");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy =new StrategyConfig();
strategy.setInclude("ums_member");
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setLogicDeleteFieldName("deleted");
// 自动填充
TableFill gmtCreate =new TableFill("create_time",FieldFill.INSERT);
TableFill gmtModify =new TableFill("update_time",FieldFill.INSERT_UPDATE);
ArrayList tableFills =new ArrayList();
tableFills.add(gmtCreate);
tableFills.add(gmtModify);
strategy.setTableFillList(tableFills);
//乐观锁
strategy.setVersionFieldName("version");
// restcontroller
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true);// localhost:xxx/hello_2
mpg.setStrategy(strategy);
mpg.execute();
}
}