1、参考官方文档:https://mybatis.plus/guide/generator.html
2、相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
3、代码生成器
package com.zhui;
import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
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.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;
import java.util.Scanner;
/**
* <p>
* MyBatisPlus代码生成器
* </p>
*
* @author 张辉
* @since 2019-01-11
*/
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
// 1、全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setActiveRecord(false) // 是否支持AR模式
.setEnableCache(false)
.setAuthor("zhui") // 作者
.setOutputDir(projectPath + "/mybatis-plus-demo/src/main/java") // 生成路径
.setFileOverride(true) // 重新生成不覆盖原有文件
.setServiceName("%sService")// 设置生成的service接口的名字的首字母是否为I,如IUserService
// .setBaseResultMap(true)
.setBaseColumnList(true)
.setOpen(false);
// 2、数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL) // 设置数据库类型
.setDriverName("com.mysql.jdbc.Driver")
.setUrl("jdbc:mysql://localhost:3306/test")
.setUsername("root")
.setPassword("root");
// 3、策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true) // 全局大写命名
.setDbColumnUnderline(true) // 指定表名 字段名是否使用下划线
.setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略
// .setTablePrefix("tbl_") // 指定表的前缀
.setInclude(scanner("表名"))
.setRestControllerStyle(true);
// 4、包名策略配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.zhui") // 父包
// .setMapper(scanner("模块名"))
.setMapper("mapper") // mapper包位置
.setService("service") // service包位置
.setController("controller") // controller包位置
.setEntity("entity"); // Entity包位置
// 5、整合配置
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(globalConfig)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig);
// 6、执行
autoGenerator.execute();
}
}
来源:oschina
链接:https://my.oschina.net/u/3804787/blog/3000524