1.需要引入的依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<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.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
2.代码
package com.shuo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.List;
public class CodeGenerator {
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
final String mapperPath = projectPath + "/src/main/resources/mapper/";
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("作者");
gc.setOpen(false);
gc.setServiceName("%sService");
gc.setIdType(IdType.AUTO);
mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/库?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("账号");
dsc.setPassword("密码");
mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig();
pc.setModuleName("test");
pc.setParent("com.shuo");
mpg.setPackageInfo(pc);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude("o2o_repository");
strategy.setTablePrefix("o2o_");
mpg.setStrategy(strategy);
mpg.setConfig(new ConfigBuilder(mpg.getPackageInfo(), mpg.getDataSource(), mpg.getStrategy(), mpg.getTemplate(), mpg.getGlobalConfig()));
List<TableInfo> tableList = mpg.getConfig().getTableInfoList();
for (TableInfo table : tableList) {
table.setEntityName(table.getEntityName() + "Mp");
table.setMapperName(table.getEntityName() + "Mapper");
table.setXmlName(table.getEntityName() + "Mapper");
table.setServiceName(table.getEntityName() + "Service");
table.setServiceImplName(table.getEntityName() + "ServiceImpl");
}
mpg.execute();
}
}
3. 结果图
