pom文件引入以下
<!-- MyBatis-Plus代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!--MyBatis-Plus逆向功能所需的模板引擎-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
创建以下类直接执行main方法即可
public class CodeGenerateUtils {
/**
* 运行这个main方法进行代码生成
*/
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("cll");
gc.setOpen(false);
// 实体属性 Swagger2 注解
gc.setSwagger2(false);
gc.setServiceName("%sService");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/planning_management?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
Connection jdbcConnection = null;
StringBuilder stringBuilder = new StringBuilder();
try {
jdbcConnection = DriverManager.getConnection(dsc.getUrl(), dsc.getUsername(), dsc.getPassword());
DatabaseMetaData md = jdbcConnection.getMetaData();
ResultSet rs = md.getTables(null, null, "%", new String[]{"TABLE"});
while (rs.next()) {
if (!"sys_config".equals(rs.getString(3))) {
System.out.println(rs.getString(3));
stringBuilder.append(rs.getString(3)).append(",");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
/*
包配置,可以根据自己的项目情况自定义生成后的存放路径
entity默认路径为父目录.entity
mapper默认路径为父目录.mapper
service默认路径为父目录.service
serviceImpl默认路径为父目录.service.impl
controller默认路径为父目录.controller
*/
PackageConfig pc = new PackageConfig();
pc.setModuleName(null);
//设置父目录
pc.setParent("com.example.generator.generator");
//自定义entity的生成位置
pc.setEntity("model.entity");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1).split(","));
strategy.setControllerMappingHyphenStyle(true);
// 设置表前缀
strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}