![33c8a896ffd70e09c33955c09f2cd52b.png](https://i-blog.csdnimg.cn/blog_migrate/1d194a62eba3498aa56c857536c73594.jpeg)
MyBatisplus根据数据表自动生成实体类、Mapper、Service、Servicelmpl、Controller
1.pom.xml导入MyBatis Plus Generator
2.pom.xml导入velocity依赖
![0790b8420b42c25fc9aa3431b4520166.png](https://i-blog.csdnimg.cn/blog_migrate/52c3d8145c9371382361fd4da8cc516a.png)
启动类
package com.lin.mybatisplus.config;
import com.baomidou.mybatisplus.annotation.DbType;
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: ccgg
* @create: 2020-08-09 22:38
**/
public class Main {
public static void main(String[] args) {
//创建autoGenerator
AutoGenerator autoGenerator = new AutoGenerator();
//数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
//选用数据库
dataSourceConfig.setDbType(DbType.MYSQL);
//设置数据库连接
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/ccgg?useUnicode=true&characterEncoding=UTF-8");
//设置用户名
dataSourceConfig.setUsername("root");
//设置密码
dataSourceConfig.setPassword("root");
//设置数据库驱动
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
//装填数据源
autoGenerator.setDataSource(dataSourceConfig);
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
//获取工程绝对路径
globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
//关闭文件夹
globalConfig.setOpen(false);
//设置作者
globalConfig.setAuthor("ccgg");
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);
//包信息
PackageConfig packageConfig = new PackageConfig();
//存放路径
packageConfig.setParent("com.southwind.mybatisplus");
//设置模块名
packageConfig.setModuleName("generator");
//设置controller
packageConfig.setController("controller");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setMapper("mapper");
packageConfig.setEntity("entity");
autoGenerator.setPackageInfo(packageConfig);
//配置策略
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("user","tab2");
//自动添加lombokmodel注解
strategyConfig.setEntityLombokModel(true);
//设置驼峰名命
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
autoGenerator.setStrategy(strategyConfig);
autoGenerator.execute();
}
}
注意在启动类要添加新的mapper的扫描