https://mp.baomidou.com/guide/generator.html
目前公司所做项目的各模块的dao层接口有大量数据库查询的方法,大部分都是通用的CURD,所以在在网上找到了Mybatis-Plus这个插件,该插件具有通用CURD接口,而且具有前后端代码生成器的功能,这次主要介绍后端代码生成器的使用。
1.Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
码云地址:https://gitee.com/baomidou/mybatis-plus
githb地址:https://github.com/baomidou/mybatis-plus
2.使用方法
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>maven 官方最新版本为准</version>
</dependency>
引入jar包后开始编写代码生成器示例文件
这个我使用了读取properties的方式来生成代码
properties示例如下
Mybatis-Plus.properties
#此处为本项目src所在路径(代码生成器输出路径)
OutputDir=D:/XXX/src
#数据库表名(此处切不可为空,如果为空,则默认读取数据库的所有表名)
tableName=XXX
#生成代码类名类名
className=XXX
#设置作者
author=XXX
#
#
#正常情况下,下面的代码无需修改!!!!!!!!!!
#
#
#自定义包路径
parent=com.XXX
#数据库地址
url=jdbc:mysql://127.0.0.1:3306/XXX?useUnicode=true&characterEncoding=utf-8
#数据库用户名
userName=root
#数据库密码
passWord=XXX
Mybatis-Plus-AutoGenerator 最详细使用方法!
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。可以通过模版等一系列的方式来生成代码,⚠️这个比Mybatis-Generator的更加强大,纯java代码。。官方地址:https://mp.baomidou.com/guide/generator.html
- 
package com.cikers.ps;
 - 
 - 
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
 - 
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.NamingStrategy;
 - 
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
 - 
import org.apache.commons.lang3.StringUtils;
 - 
 - 
import java.util.ArrayList;
 - 
import java.util.List;
 - 
import java.util.Scanner;
 - 
 - 
public class MysqlGenerator {
 - 
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 = “/Users/syk/Documents/*/*/”;
 - 
gc.setOutputDir(projectPath + “/src/main/java”);
 - 
gc.setAuthor(“syk”);
 - 
gc.setOpen(false);
 - 
gc.setBaseResultMap(true);
 - 
gc.setBaseColumnList(true);
 - 
//gc.setControllerName(“SSSSScontroller”);
 - 
// 是否覆盖已有文件
 - 
gc.setFileOverride(false);
 - 
mpg.setGlobalConfig(gc);
 - 
 - 
// 数据源配置
 - 
DataSourceConfig dsc = new DataSourceConfig();
 - 
dsc.setUrl(“jdbc:mysql://******/newstack_db?useUnicode=true&characterEncoding=UTF-8”);
 - 
// dsc.setSchemaName(“public”);
 - 
dsc.setDriverName(“com.mysql.jdbc.Driver”);
 - 
dsc.setUsername(“root”);
 - 
dsc.setPassword(“password”);
 - 
mpg.setDataSource(dsc);
 - 
 - 
// 包配置
 - 
PackageConfig pc = new PackageConfig();
 - 
//pc.setModuleName(scanner(“模块名”));
 - 
pc.setParent(null);
 - 
// 这个地址是生成的配置文件的包路径
 - 
pc.setEntity(“com.cikers.ps.model.entity”);
 - 
 - 
//pc.setController(“com.cikers.ps.controller”);
 - 
pc.setMapper(“com.cikers.ps.mapper”);
 - 
mpg.setPackageInfo(pc);
 - 
// 自定义配置
 - 
InjectionConfig cfg = new InjectionConfig() {
 - 
 - 
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) {
 - 
 - 
public String outputFile(TableInfo tableInfo) {
 - 
// 自定义输出文件名
 - 
return projectPath + “/src/main/resources/mapper/entity”
 - 
+ “/” + tableInfo.getEntityName() + “Mapper” + StringPool.DOT_XML;
 - 
}
 - 
});
 - 
 - 
cfg.setFileOutConfigList(focList);
 - 
mpg.setCfg(cfg);
 - 
 - 
// 配置模板
 - 
TemplateConfig templateConfig = new TemplateConfig();
 - 
 - 
// //配置自定义输出模板
 - 
// 不需要其他的类型时,直接设置为null就不会成对应的模版了
 - 
//templateConfig.setEntity(“…”);
 - 
templateConfig.setService(null);
 - 
templateConfig.setController(null);
 - 
templateConfig.setServiceImpl(null);
 - 
// 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
 - 
// 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也
 - 
// 可以自定义模板名称 只要放到目录下,名字不变 就会采用这个模版 下面这句有没有无所谓
 - 
// 模版去github上看地址:
 - 
/**https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-generator/src/main/resources/templates*/
 - 
//templateConfig.setEntity(“/templates/entity.java”);
 - 
templateConfig.setXml(null);
 - 
mpg.setTemplate(templateConfig);
 - 
 - 
// 策略配置
 - 
StrategyConfig strategy = new StrategyConfig();
 - 
strategy.setNaming(NamingStrategy.underline_to_camel);
 - 
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
 - 
strategy.setSuperEntityClass(“com.cikers.ps.model.BaseEntity”);
 - 
strategy.setSuperMapperClass(“com.cikers.ps.util.IMapper”);
 - 
strategy.setEntityLombokModel(false);
 - 
//strategy.setRestControllerStyle(false);
 - 
//strategy.setSuperControllerClass(“com.cikers.ps.controller.MysqlController”);
 - 
strategy.setInclude(scanner(“表名”));
 - 
// 设置继承的父类字段
 - 
strategy.setSuperEntityColumns(“id”,“modifiedBy”,“modifiedOn”,“createdBy”,“createdOn”);
 - 
//strategy.setControllerMappingHyphenStyle(true);
 - 
//strategy.setTablePrefix(pc.getModuleName() + “_”);
 - 
mpg.setStrategy(strategy);
 - 
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
 - 
mpg.execute();
 - 
}
 - 
 - 
}
 - 
 
其中需要的maven依赖
- 
<dependency>
 - 
<groupId>com.baomidou</groupId>
 - 
<artifactId>mybatis-plus-boot-starter</artifactId>
 - 
<version>3.0-RELEASE</version>
 - 
</dependency>
 - 
<!– mp自动代码生成–>
 - 
<dependency>
 - 
<groupId>com.baomidou</groupId>
 - 
<artifactId>mybatis-plus-generator</artifactId>
 - 
<version>3.0.7.1</version>
 - 
</dependency>
 - 
<!– velocity 模板引擎, 默认 –>
 - 
<dependency>
 - 
<groupId>org.apache.velocity</groupId>
 - 
<artifactId>velocity-engine-core</artifactId>
 - 
<version>2.0</version>
 - 
</dependency>
 - 
 - 
<!– freemarker 模板引擎 –>
 - 
<dependency>
 - 
<groupId>org.freemarker</groupId>
 - 
<artifactId>freemarker</artifactId>
 - 
<version>2.3.23</version>
 - 
</dependency>
 - 
 - 
 - 
<!– beetl 模板引擎 –>
 - 
<dependency>
 - 
<groupId>com.ibeetl</groupId>
 - 
<artifactId>beetl</artifactId>
 - 
<version>2.2.5</version>
 - 
</dependency>
 

运行输入表面就可以了!!!!
转载请注明:SuperIT » Mybatis-Plus代码生成器
