前言
👏作者简介:我是笑霸final,一名热爱技术的在校学生。
📝个人主页:​​​个人主页1​​​ || ​​笑霸final的主页2​​ 📕系列专栏:《资料专栏》
📧如果文章知识点有错误的地方,请指正!和大家一起学习,一起进步👀
🔥如果感觉博主的文章还不错的话,👍点赞👍 + 👀关注👀 + 🤏收藏🤏

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 ​​Entity​​​、​​Mapper​​​、​​Mapper XML​​​、​​Service​​​、​​Controller​​ 等各个模块的代码,极大的提升了开发效率。

添加 代码生成器 依赖

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.0</version>
</dependency>

添加 模板引擎 依赖,MyBatis-Plus 支持 Velocity(默认)

<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-engine-core -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>

编写配置

public static void main(String[] args) {

// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();

// 2、全局配置
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
//gc.setOutputDir(projectPath + "/src/main/java");
//代码生成的路径E:\学习\学习项目\硅谷课堂\ggkt_parent
gc.setOutputDir("E:\\学习\\学习项目\\硅谷课堂\\ggkt_parent\\service\\service_vod"+"/src/main/java");
gc.setServiceName("%sService"); //去掉Service接口的首字母I
gc.setAuthor("xbfinal");
gc.setOpen(false);
mpg.setGlobalConfig(gc);

// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/glkt_vod");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");//数据库账号
dsc.setPassword("root");//数据库密码
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);

// 4、包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.xbfinal.ggkt");
pc.setModuleName("vod"); //模块名
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);

// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("teacher");
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
mpg.setStrategy(strategy);

// 6、执行
mpg.execute();
}

注意

适用版本:​​mybatis-plus-generator 3.5.1 以下版本​​​,3.5.1 及以上的请参考官网​​https://baomidou.com/​​​​点此跳转​