mybatis plus 是什么?
mybatis plus 能帮我们什么?
此处我们使用 mybatis plus 来帮我们生成后台代码,包括 controller、entity、mapper.java、service、serviceImpl、mapper.xml 一系列代码,实用不实用用了就知道了,其他强大功能可以去查看官方文档
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!--mybatis plus 相关依赖版本注意保持一致,否则会有惊喜-->
<!--mybatis plus 集成 springboot 核心依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--mybatis plus 代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
<!-- swagger 注解-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.20</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.20</version>
</dependency>
</dependencies>
package com.wxw.mybatisplus_generator.config;
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.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
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 MybatisPlusGenerateCode {
/**
*
* 读取控制台内容
*
*/
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 gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("WXW");//文件创建注释
gc.setOpen(false); //生辰完成之后,是否弹出生成文件路径(默认为true)
gc.setFileOverride(false);// 是否覆盖同名文件,默认是 false
gc.setActiveRecord(true);// 不需要ActiveRecord 特性的请改为 false
gc.setEnableCache(false);// 是否需要 XML 二级缓存
gc.setBaseResultMap(true);// 是否生成 XML ResultMap
gc.setBaseColumnList(true);// 是否生成 XML columList
gc.setSwagger2(true); //实体属性是否使用 Swagger2 注解
/* 自定义文件命名,注意 %s 会自动填充表实体属性! */
gc.setServiceName("%sService");
// gc.setMapperName("%sDao");
// gc.setXmlName("%sDao");
// gc.setServiceImplName("%sServiceDiy");
// gc.setControllerName("%sAction");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("test");
dsc.setPassword("test");
dsc.setUrl("jdbc:mysql://localhost:3306/test");
dsc.setTypeConvert(new MySqlTypeConvert() {//数据库字段类型转换
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
if (fieldType.toLowerCase().contains("number")) {//number 转 Integer
return DbColumnType.INTEGER;
}
if (fieldType.toLowerCase().contains("datetime")) {//datetime 转 Date
return DbColumnType.DATE;
}
if (fieldType.toLowerCase().contains("date")) {//date 转 Date
return DbColumnType.DATE;
}
return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
}
});
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));//指定生成到具体的模块名
pc.setParent("com.wxw.mybatisplus_generator");//指定生成的父路径
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<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {// 调整 xml 生成目录演示
@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();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
//让默认生成 mapper.xml 的目录不再生成
templateConfig.setXml(null);
// templateConfig.setMapper(null);//不生成 mapper
// templateConfig.setController(null);//不生成 controller
// templateConfig.setService(null);// 不生成 service
// templateConfig.setServiceImpl(null);// 不生成serviceImpl
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);//表名生成策略(下划线转换成驼峰命名)
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//字段生成策略(下划线转换成驼峰命名)
// strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");//实体类集成 BaseEntity
strategy.setEntityLombokModel(true);//使用lombok注解
strategy.setRestControllerStyle(true);//controller 加上 restController 注解
strategy.setControllerMappingHyphenStyle(true);//controller 中 RequestMapping 注解使用驼峰命令
// strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController"); //公共父类BaseController
strategy.setSuperEntityColumns("id"); // 写于父类中的公共字段
// strategy.setInclude(new String[]{"",""});//需要生成的表名,多个表可以用英文逗号分开
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));//需要生成的表名,多个表可以用英文逗号分开
// strategy.setTablePrefix(pc.getModuleName() + "_");//生成时是否以模块名为前缀,默认模块名为前缀
strategy.setEntityTableFieldAnnotationEnable(true);//实体类属性增加 @TableField 注解与数据库字段对应
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
最后就能看到 mybatis plus 帮你生成的代码了。