参考:
MyBatis官网地址: https://mybatis.org/mybatis-3/zh/index.html
MyBatis-Plus官网地址:https://baomidou.com/pages/24112f/#%E7%89%B9%E6%80%A7
前言
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MyBatis 使用要点汇总:
常用转义字符:
< | < | 小于 |
> | > | 大于 |
& | & | 与 |
' | ' | 单引号 |
" | " | 双引号 |
MyBatis-Plus使用要点汇总:
特性
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小:启动即会自动注入基本 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、SQLServer 等多种数据库
- 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
- 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
配置:
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
安装:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.1</version>
</dependency>
MyBatis 代码生成器的使用
查看代码
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class MysqlGenerator {
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("D://generator");
gc.setFileOverride(true);
gc.setActiveRecord(false);
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setOpen(true);
gc.setAuthor("XBX");
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("user_fms_fas_amortization");
dsc.setPassword("xxxx]");
dsc.setUrl(
"jdbc:mysql://XXXXXX:3306/fms_fas_amortization?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix("fms_");// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略s
strategy.setInclude(new String[]{"fms_amortize_info","fms_amortize_info_clean",
"fms_amortize_info_schedule","fms_amortize_log","fms_amortize_rule"}); // 需要生成的表
// strategy.setExclude(new String[]{"test"}); // 排除生成的表
// 自定义实体父类
// strategy.setSuperEntityClass("top.ibase4j.core.base.BaseModel")
// 自定义实体,公共字段
// strategy.setSuperEntityColumns(
// new String[] { "id_", "enable_", "remark_", "create_by", "create_time", "update_by", "update_time" });
// 自定义 mapper 父类
strategy.setSuperMapperClass("com.xxxx.base.mapper.BaseWriteMapper");
// 自定义 service 父类(生成后请把Service的父类删除)
strategy.setSuperServiceClass("com.xxxx.base.service.IBaseService");
// 自定义 service 实现类父类
strategy.setSuperServiceImplClass("com.xxxx.base.service.impl.BaseServiceImpl");
// 自定义 controller 父类
//strategy.setSuperControllerClass("com.xxxx.springcloud.common.controller.BaseController");
//生成 <code>@RestController</code> 控制器
strategy.setRestControllerStyle(true);
// 【实体】是否生成字段常量(默认 false)
// public static final String ID = "test_id";
// strategy.setEntityColumnConstant(true);
// 【实体】是否为构建者模型(默认 false)
// public User setName(String name) {this.name = name; return this;}
// strategy.setEntityBuilderModel(true);
// strategy.setLogicDeleteFieldName("enable");
//【实体】是否为lombok模型(默认 false)
strategy.setEntityLombokModel(false);
mpg.setStrategy(strategy);
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
// InjectionConfig cfg = new InjectionConfig() {
// @Override
// public void initMap() {
// Map<String, Object> map = new HashMap<String, Object>();
// // map.put("providerClass", "IBizProvider");
// // map.put("providerClassPackage", "org.xshop.provider.IBizProvider");
// //map.put("rpcService", false);
// this.setMap(map);
// }
// };
// mpg.setCfg(cfg);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.xxxx.fms.fas.amortization");
pc.setEntity("dao.dataobject");
pc.setMapper("dao.mapper");
pc.setXml("mapper.xml");
pc.setService("service.service");
pc.setServiceImpl("service.service.impl");
pc.setController("service.facede");
mpg.setPackageInfo(pc);
// 放置自己项目的 src/main/resources/template 目录下, 默认名称一下可以不配置,也可以自定义模板名称
// TemplateConfig tc = new TemplateConfig();
// tc.setEntity("tpl/entity.java.vm");
// tc.setMapper("tpl/mapper.java.vm");
// tc.setXml("tpl/mapper.xml.vm");
// tc.setService("tpl/iservice.java.vm");
// tc.setServiceImpl("tpl/service.java.vm");
// tc.setController("tpl/controller.java.vm");
// mpg.setTemplate(tc);
// 执行生成
mpg.execute();
}
}
MybatisPlus的条件构造器Wrapper常用的构造函数总结
转载: https://blog.csdn.net/weixin_44870909/article/details/108761753