一:什么是 MyBatis-Plus?
MyBatis-Plus 是 MyBatis 的一个强大的增强工具包,用于简化开发。该工具包为 MyBatis 提供了一些高效、有用、开箱即用的功能,使用它可以有效地节省您的开发时间。
企业版 Mybatis-Mate 高级特性
特征
- 完全兼容 MyBatis
- 启动时自动配置
- 开箱即用的数据库操作界面
- 强大而灵活的 where 条件包装器
- 生成主键的多种策略
- Lambda 风格的 API
- 全能且高度可定制的代码生成器
- 自动分页操作
- SQL注入防御
- 支持主动记录
- 支持可插拔的自定义接口
- 内置许多有用的扩展
Getting started
- Maven:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>Latest Version</version>
</dependency>
- Gradle
compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: 'Latest Version'
代码自动生成器
import com.baomidou.mybatisplus.annotation.DbType;
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.rules.NamingStrategy;
import java.util.HashMap;
import java.util.Map;
/**
* Oracle数据库
* mybatis-plus代码生成器(用于生成entity)<br>
* @author Administrator
*/
public class MyBatisPlusGenerator {
public static void main(String[] args) {
//模块名
String moduleName="equipment-server";
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
System.out.println(projectPath+"/"+moduleName);
gc.setOutputDir(projectPath+"/"+moduleName + "/src/main/java");
gc.setFileOverride(true);// 是否覆盖
gc.setActiveRecord(true);
// gc.setIdType(IdType.ASSIGN_ID); //主键策略
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(true);// XML columList
gc.setAuthor("yin");
gc.setSwagger2(true);
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.ORACLE);
dsc.setTypeConvert(new OracleTypeConvert());
//配置数据源
dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
dsc.setUsername("testDb");
dsc.setPassword("123456");
dsc.setUrl("jdbc:oracle:thin:@192.168.0.120:1521/orcl");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setTablePrefix(new String[]{"SYS_"});// 此处修改为表前缀
strategy.setInclude(new String[]{"SF_CHARGE_CONFIRMATION_ORDER"});//设置要映射的表名
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName(moduleName);
pc.setParent("cn.gewut.business.core.plus");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setXml("mapper");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/controller.java.vm";
//
// // 自定义输出配置
// List<FileOutConfig> focList = new ArrayList<>();
// // 自定义配置会被优先输出
// focList.add(new FileOutConfig(templatePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
//
// return "../controller/"+tableInfo.getEntityName()
// + StringPool.DOT_JAVA;
// }
// });
// cfg.setFileOutConfigList(focList);
TemplateConfig tc = new TemplateConfig();
tc.setController("/templates/controller.java.vm");
tc.setEntity("/templates/entity.java.vm");
tc.setMapper("/templates/mapper.java.vm");
tc.setXml("/templates/mapper.xml.vm");
tc.setService("/templates/service.java.vm");
tc.setServiceImpl("/templates/serviceImpl.java.vm");
// 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
mpg.setTemplate(tc);
mpg.setCfg(cfg);
// 执行生成
mpg.execute();
// 打印注入设置
System.err.println(mpg.getCfg().getMap().get("abc"));
}
}