1.SpringBoot中勾选Spring web,MyBatis和Mysql Driver。
2.在POM.xml文件中添加插件
org.mybatis.generator mybatis-generator-maven-plugin1.3.2 src/main/resources/generatorConfig.xml true true
3.配置generatorConfig.xml配置文件:在resource目录下新建generatorConfig.xml文件,按下面注释配置好。
需要作出修改的地方是:
数据库驱动,数据库地址及表名,账号,密码
本机数据库驱动jar包存放目录:
生成对应表及类名,domainObjectName是设置实体类的名字:
根据数据库中的表修改对应表
4.修改application.properties
5.运行Maven插件:调出Maven面板—>展开Plugins—>展开mybatis-generator—>右键Run Maven Build
参考博客链接:
https://blog.csdn.net/jqc874789596/article/details/79105829springboot整合mybatis-plus逆向工程的实现
1.SpringBoot中勾选Spring web,MyBatis-Plus和Mysql Driver。
2.在POM.xml文件中添加添加依赖:
而外需要添加代码生成器依赖,lombok依赖,freemarker模板引擎依赖(因为Apache默认的是velocity依赖)
org.springframework.boot spring-boot-starter-webcom.baomidou mybatis-plus-boot-starter3.4.2 com.baomidou mybatis-plus-generator3.1.1 mysql mysql-connector-javaruntime org.freemarker freemarker2.3.28 org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-engine
3.定义配置类
全局配置
数据源配置
包配置
策略配置
代码生成器执行
package com.example.genrator5;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodeGenerator {
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
GlobalConfig globalConfig = new GlobalConfig();
//生成文件的输出目录
String projectPath = System.getProperty("user.dir");
globalConfig.setOutputDir(projectPath + "/src/main/java");
//Author设置作者
globalConfig.setAuthor("zst");
//是否覆盖文件
globalConfig.setFileOverride(true);
//生成后打开文件
globalConfig.setOpen(false);
globalConfig.setbaseResultMap(true);
globalConfig.setbaseColumnList(true);
globalConfig.setActiveRecord(true);
mpg.setGlobalConfig(globalConfig);
DataSourceConfig dataSourceConfig = new DataSourceConfig();
// 数据库类型,默认MYSQL
dataSourceConfig.setDbType(DbType.MYSQL);
//自定义数据类型转换
dataSourceConfig.setTypeConvert(new MySqlTypeConvert());
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/seckill?characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("root");
mpg.setDataSource(dataSourceConfig);
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
//父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setParent("com.example.mpdemo");
mpg.setPackageInfo(pc);
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
//如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
List focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/"+ pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
//设置命名格式
strategy.setCapitalMode(true);//全局大写命名
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略,下划线转驼峰
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
//实体是否为lombok模型(默认 false)
strategy.setEntityLombokModel(true);
//生成 @RestController 控制器
strategy.setRestControllerStyle(true);
//设置自定义继承的Entity类全称,带包名
//strategy.setSuperEntityClass("com.jiangfeixiang.mpdemo.baseEntity");
//设置自定义继承的Controller类全称,带包名
//strategy.setSuperControllerClass("com.jiangfeixiang.mpdemo.baseController");
//设置自定义基础的Entity类,公共字段
strategy.setSuperEntityColumns("id");
//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//表名前缀
// strategy.setTablePrefix(pc.getModuleName() + "_");
strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
System.getProperty(“user.dir”) :获取当前工作目录绝对路径
工作目录不是指当前项目目录,而是工作目录(Working directory)
如idea中:打开某个项目,则获取的路径为项目文件夹最上级目录在电脑中的绝对路径,无论是在最外层项目使用还是在子项目中使用,也可以修改为指定的目录
globalConfig.setOutputDir("G:\JavaEEProject\genrator5\src\main\java");
参考博客链接
https://www.zhangshengrong.com/p/l51gwLjJ10/ https://blog.csdn.net/weixin_39520967/article/details/99704442



