①添加两个依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
②生成器主体代码
public class CodeGenerator {
public static void main(String[] args) {
AutoGenerator ag = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
String path = System.getProperty("user.dir");
gc.setOutputDir(path+"/src/main/java");
//需要改动//
gc.setAuthor("abc");
gc.setControllerName("%sController");
gc.setServiceName("I%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setBaseColumnList(true);
gc.setBaseResultMap(true);
//需要改动//
gc.setIdType(IdType.AUTO); //主键是否自增
ag.setGlobalConfig(gc);
PackageConfig pc = new PackageConfig();
//需要改动//
pc.setParent("com.example"); //生成代码所在层级
ag.setPackageInfo(pc);
DataSourceConfig dc = new DataSourceConfig();
dc.setDriverName("com.mysql.jdbc.Driver");
//需要改动//
dc.setUrl("jdbc:mysql://localhost:3306/testmp55");
dc.setUsername("root");
//需要改动//
dc.setPassword("root");
ag.setDataSource(dc);
StrategyConfig sc = new StrategyConfig();
sc.setNaming(NamingStrategy.underline_to_camel);
sc.setColumnNaming(NamingStrategy.underline_to_camel);
//需要改动//
sc.setInclude("employee"); //需要生成的表单名, 不写该数据表单全部生成
ag.setStrategy(sc);
ag.execute();
}
}