现在mybatis-plus的应用真的是太广泛,但是mybatis-plus到底是干什么的呢?
mybatis-plus就是通过我们重新建一个项目,把需要创建的表的整个后台执行流程给自动写出来,然后mybatis-plus里面内置了许多增删改查的代码,这些也省去了许多配置供我们直接调用。
如果按照传统的我们先写controller再写service再写.........等等十分麻烦,但我们用了mybatis-plus逆向工程就不一样了,可以直接创建好然后复制到我们原项目里去了。
再讲mybatis-plus之前,我们先统一好我们原项目的框架如图:
一定要写好,因为这里要与后面讲到的新辅助项目对应
-----------------------------------------------------------------------------------------
好了,废话不多说,我们直接开始。
第一步,重新建一个项目,这个项目很简单了,不需要配置太多的东西,我们先把要的依赖写出来:
com.baomidou mybatis-plus-boot-starter3.4.0 com.baomidou mybatis-plus-generator3.4.0 org.freemarker freemarker2.3.30 mysql mysql-connector-javaruntime junit junit4.12
其他依赖都是自带的,这几个需要加上。就是mybatis-plus的依赖,都是从mybatis-plus官网里考培过来的。(注意如果依赖里存在
--------------------------------------------------------------------------------
第二步最核心了,就是通过我们编写的类自动生成整个流程,我直接上代码:
*
这是代码:
package com.xxx.generator;
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.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 java.util.*;
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
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.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("LIHAO");
gc.setOpen(false);
gc.setBaseColumnList(true);
gc.setBaseResultMap(true);
gc.setDateType(DateType.ONLY_DATE);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/seckill?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.xxxxx.seckilldemo")
.setEntity("pojo")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl")
.setController("controller");
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/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig()
.setEntity("templates/entity.java")
.setMapper("templates/mapper.java")
.setService("templates/service.java")
.setServiceImpl("templates/serviceImpl.java")
.setController("templates/controller.java");
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
代码比较多,大家需要改的地方就是那个标红的地方,你的数据库密码和版本的不同,路径有的是com.mysql.cj.jdbc.Driver;有的是com.mysql.jdbc.Driver。完成之后我们运行会出现让你写表的名称,表的信息我已经在开发文档一里写了,这里我们直接输入t_user回车,就可以直接自动生成了。成功如图:
第三步,生成之后我们把这自动生成包全部复制下来,拷贝到我们第一个项目的总包下:
这些东西都需要复制,可不是复制总包哈,是复制这四个分包以及单独复制xml到原项目里所对应的地方。就是在这里:
复制到原项目里的这两个地方。如此我们就算完工了。以下就是完成的图片:



