一:官网
URL:https://mp.baomidou.com/
二:特性
无侵入
:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑损耗小
:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作强大的 CRUD 操作
:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求支持 Lambda 形式调用
:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错支持主键自动生成
:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题支持 ActiveRecord 模式
:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作支持自定义全局通用操作
:支持全局通用方法注入( Write once, use anywhere )内置代码生成器
:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用内置分页插件
:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询分页插件支持多种数据库
:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer2005、SQLServer 等多种数据库内置性能分析插件
:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询内置全局拦截插件
:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
三:集成
-
Spring Boot:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.1.2'
-
注意:
Mybatis Plus在集成过程中一定排除
项目中是否有以下依赖
三:配置文件详解
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setAuthor("乐弟");
gc.setEntityName("%sDO");
gc.setOpen(false);
gc.setBaseColumnList(true);
gc.setBaseResultMap(true);
gc.setSwagger2(true);
gc.setIdType(IdType.AUTO);
mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("数据库地址");
dsc.setUsername("***");
dsc.setPassword("***");
mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig();
pc.setParent("com.le.di");
pc.setEntity("common.domain.dao");
pc.setPathInfo(pathInfo(projectPath));
mpg.setPackageInfo(pc);
GeneratorInject generatorInject = new GeneratorInject();
generatorInject.setFileCreate(new FileCreate());
mpg.setCfg(generatorInject);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setMapper(TemplateFormatEnum.MAPPER_JAVA.getFormatPath());
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix("erp_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
gc.setAuthor("乐弟");
这一项必须配置.不然通过freemaker无法生成文件
pc.setParent("com.le.di");
pc.setEntity("common.domain.dao");
用于配置package包路径信息
pc.setPathInfo(pathInfo(projectPath));
private static Map<String, String> pathInfo(String projectPath) {
Map<String, String> map = new HashMap<>();
map.put(ConstVal.ENTITY,projectPath+"\\"+TemplateFormatEnum.ENTITY.getDocPath() );
map.put(ConstVal.MAPPER,projectPath+"\\"+TemplateFormatEnum.MAPPER_JAVA.getDocPath() );
map.put(ConstVal.XML, projectPath+"\\"+TemplateFormatEnum.MAPPER_XML.getDocPath());
map.put(ConstVal.ENTITY_PATH,projectPath+"\\"+TemplateFormatEnum.ENTITY.getDocPath() );
map.put(ConstVal.MAPPER_PATH,projectPath+"\\"+TemplateFormatEnum.MAPPER_JAVA.getDocPath() );
map.put(ConstVal.XML_PATH, projectPath+"\\"+TemplateFormatEnum.MAPPER_XML.getDocPath());
return map;
}
此项可以配置自定义生成包与类文件
mpg.setCfg(new InjectionConfig() {
@Override
public void initMap() {}
}
);
此项必须存在,否则无法创建文件夹
strategy.setTablePrefix("db_");
用于设置表的前缀,设置此项后生成的类名将不包含前缀.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)