根据数据库中的字段数据自动生成代码,如下:
1.添加依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency>
2.编写如下main方法内容:
package com.star.servicedriveruser.generator; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.Collections; /** * 自动生成代码的工具类 */ public class MysqlGenerator { public static void main(String[] args) {
//添加数据库的驱动以及用户名和密码 FastAutoGenerator.create("jdbc:mysql://localhost:3306/service_driver_user?characterEncoding=utf-8&serverTimezone=GMT%2B8", "root","123456") .globalConfig(builder -> {
//添加作者姓名,并且指定代码生成的路径 builder.author("星辰").fileOverride().outputDir("C:\\Users\\online-taxi-public\\service-driver-user\\src\\main\\java"); }) .packageConfig(builder ->{
//指定生成的父级目录,并且生成mapper层目录结构 builder.parent("com.star.serviceDriverUser").pathInfo(Collections.singletonMap(OutputFile .mapperXml,"C:\\Users\\online-taxi-public\\service-driver-user\\src\\main\\java\\com\\star\\servicedriveruser\\mapper")); })
//指定生成数据的实体类 .strategyConfig(builder -> { builder.addInclude("car"); }) .templateEngine(new FreemarkerTemplateEngine()) .execute(); } }
标签:代码生成,generator,builder,baomidou,import,main,com From: https://www.cnblogs.com/hellostar/p/16923064.html