首先导入mybaits-plus的依赖(版本最好在3.5以下)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version> 3.3.2</version>
</dependency>
接下来就是最重要的一步,只需要三步
- Ctrl + A
- Ctrl + C
- Ctrl + V
创建一个MybatisGeneration类,将下面的代码转移到你的MybatisGeneration类里
package com.ywt.base.mybatis;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.ywt.common.utils.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* MyBatis自动生成配置文件
*/
public class MybatisGeneration {
//生成的文件路径,默认按照这个来就行,用完就删,问题不大
String outputDir = "C:\\demo";
public static void main(String[] args) {
MybatisGeneration generation = new MybatisGeneration();
generation.executeCode("需要生成表名");
}
/**
* @param tables 表名
*/
public void executeCode(String tables) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
// 是否覆盖已有文件
gc.setFileOverride(true);
// 生成文件的输出目录
String projectPath = System.getProperty("user.dir");
//gc.setOutputDir(projectPath + "/src/main/java");
gc.setOutputDir(outputDir);
// 开发人员
gc.setAuthor("");
// 是否打开输出目录
gc.setOpen(true);
// 开启 BaseResultMap
gc.setBaseResultMap(true);
// 指定生成的主键的ID类型
gc.setIdType(IdType.ASSIGN_ID);
// 时间类型对应策略: 只使用 java.util.date 代替
gc.setDateType(DateType.TIME_PACK);
//swagger加ApiOperation注解
gc.setSwagger2(false); //默认false
// entity 命名方式
gc.setEntityName("%s");
// service 命名方式
gc.setServiceName("%sService");
// service impl 命名方式
gc.setServiceImplName("%sServiceImpl");
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://你的url路径/你的数据库?serverTimezone=UTC&allowMultiQueries=true&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
// dsc.setDriverName("com.mysql.jdbc.Driver"); //mysql5.6以下的驱动
dsc.setUsername("root");
dsc.setPassword("root <--你的数据库密码");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
//pc.setModuleName("generate");//模块名称--父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setParent("com.cn");//自定义包的路径
// Entity包名
pc.setController("web");
pc.setEntity("model");
pc.setService("service");
pc.setServiceImpl("service.impl");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输入文件名称
// if (StringUtils.isEmpty(pc.getModuleName())) {
// return projectPath + "/src/main/resources/mapper/" + tableInfo.getXmlName() + StringPool.DOT_XML;
// }else {
// return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getXmlName() + StringPool.DOT_XML;
// }
if (StringUtils.isBlank(pc.getModuleName())) {
return outputDir+"/" + tableInfo.getXmlName() + StringPool.DOT_XML;
}else {
return outputDir +"/"+ pc.getModuleName() + "/" + tableInfo.getXmlName() + StringPool.DOT_XML;
}
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// 数据库表映射到实体的命名策略: 下划线转驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
// 数据库表字段映射到实体的命名策略: 下划线转驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 【实体】是否为lombok模型(默认 false)
strategy.setEntityLombokModel(false);
//配置 rest 风格的控制器(@RestController)
strategy.setRestControllerStyle(true);
// 需要包含的表名,允许正则表达式(与exclude二选一配置)
strategy.setInclude(tables);
// 驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//strategy.setTablePrefix(tablePrefixs);// 去掉表前缀
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
!!!注意:生成的代码必须在数据库中有这张表
然后将数据库的表名填写到main方法里
最后运行main方法,会弹出生成代码的文件,然后快乐的CV搬运就好啦!
对大家有帮助的可以点点赞!