1,引入依赖
<!-- MP代码生成器的依赖 生成器依赖+模板引擎 -->
<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.2</version>
</dependency>
<!-- Mybatis plus + mysql 驱动 + lombok -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<optional>true</optional>
</dependency>
2 配置数据源(springBoot)
#配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=用户名
spring.datasource.password=密码
3,配置参数
https://mp.baomidou.com/guide/generator.html
package cn.kgc.util;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
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.NamingStrategy;
/**
* @description:
* @author: 王恒
* @time: 2020/7/3 14:12
*/
public class TestGenerator {
public static void main (String[] args){
//1、全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(false)//不开启AR模式
.setAuthor("wang")//设置作者
//生成路径(一般都是生成在此项目的src/main/java的绝对路径)
.setOutputDir("D:\\K9512-U4\\day_day_buy_parent\\ddbuy_common_service\\src\\main\\java")
.setFileOverride(true)//第二次生成会把第一次生成的覆盖掉
.setIdType(IdType.AUTO)//主键策略
.setServiceName("%sService")//生成的service接口名字首字母是否为I,这样设置就没有I
.setBaseResultMap(true)//生成resultMap
.setBaseColumnList(true);//在xml中生成基础列
//2、数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)//数据库类型
.setDriverName("com.mysql.jdbc.Driver")
.setUrl("jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8")
.setUsername("用户名")
.setPassword("密码");
//3、策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true)//开启全局大写命名
.setNaming(NamingStrategy.underline_to_camel)//下划线到驼峰的命名方式
.setTablePrefix("tb_")//表名前缀
.setEntityLombokModel(true)//使用lombok
.setInclude("表名1","表名2");//逆向工程使用的表
//4、包名策略配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("cn.kgc")//设置包名的parent
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("entity")
.setXml("mapper");//设置xml文件的目录
//5、整合配置
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig);
//6、执行
autoGenerator.execute();
}
}
4,启动