一、简介
通过maven项目,springBoot构建学习,官方地址文档简介 | MyBatis-Plus (baomidou.com)
官方讲解,mybatis的优点:
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小:启动即会自动注入基本 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 操作智能分析阻断,也可自定义拦截规则,预防误操作
二、使用步骤
1、引入mybatis-plus的依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>Latest Version</version>
</dependency>
2、数据库配置
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.cj.jdbc.Driver druid: url: jdbc:mysql://localhost:3306/mybatis_plus?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT&serverTimezone=Asia/Shanghai username: root password:root //muybatis配置,mapper位置,别名,打印位置 mybatis-plus: mapper-locations: classpath:/mapper/*/*Mapper.xml type-aliases-package: com.lqh.mybatuis.demo configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3、@MapperScan
注解,扫描 Mapper 文件夹
@SpringBootApplication @MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper") public classApplication { public static voidmain(String[] args) { SpringApplication.run(QuickStartApplication.class, args); } }
三、表字段注解
1、@TableName
- 描述:表名注解
2、@TableFileId
- 描述:字段注解(非主键)
3、@TableId
- 描述:主键注解
4、@Version
- 描述:乐观锁注解、标记
@Verison
在字段上
5、@EnumValue
- 描述:通枚举类注解(注解在枚举字段上)
6、@TableLogic
- 描述:表字段逻辑处理注解(逻辑删除)
7、@OrderBy
- 描述:内置 SQL 默认指定排序,优先级低于 wrapper 条件查询
四、CRUD 接口
Service CRUD 接口
- 通用 Service CRUD 封装IService(opens new window)接口,进一步封装 CRUD 采用
get 查询单行
remove 删除
list 查询集合
page 分页
前缀命名方式区分Mapper
层避免混淆, - 泛型
T
为任意实体对象 - 建议如果存在自定义通用 Service 方法的可能,请创建自己的
IBaseService
继承Mybatis-Plus
提供的基类 - 对象
Wrapper
为条件构造器
1、Save
//插入一条记录(选择字段,策略插入) booleansave(T entity); //插入(批量) boolean saveBatch(Collection<T>entityList); //插入(批量) boolean saveBatch(Collection<T> entityList, int batchSize);
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Collection<T> | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
2、SaveOrUpdate
//TableId 注解存在更新记录,否插入一条记录 booleansaveOrUpdate(T entity); //根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper<T>updateWrapper); //批量修改插入 boolean saveOrUpdateBatch(Collection<T>entityList); //批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
Wrapper<T> | updateWrapper | 实体对象封装操作类 UpdateWrapper |
Collection<T> | entityList | 实体对象集合 |
int | batchSize | 插入批次数量 |
3、Remove
//根据 entity 条件,删除记录 boolean remove(Wrapper<T>queryWrapper); //根据 ID 删除 booleanremoveById(Serializable id); //根据 columnMap 条件,删除记录 boolean removeByMap(Map<String, Object>columnMap); //删除(根据ID 批量删除) boolean removeByIds(Collection<? extends Serializable> idList);
类型 | 参数名 | 描述 |
---|---|---|
Wrapper<T> | queryWrapper | 实体包装类 QueryWrapper |
Serializable | id | 主键ID |
Map<String, Object> | columnMap | 表字段 map 对象 |
Collection<? extends Serializable> | idList | 主键ID列表 |
4、Update
//根据 UpdateWrapper 条件,更新记录 需要设置sqlset boolean update(Wrapper<T>updateWrapper); //根据 whereWrapper 条件,更新记录 boolean update(T updateEntity, Wrapper<T>whereWrapper); //根据 ID 选择修改 booleanupdateById(T entity); //根据ID 批量更新 boolean updateBatchById(Collection<T>entityList); //根据ID 批量更新 boolean updateBatchById(Collection<T> entityList, int batchSize);
类型 | 参数名 | 描述 |
---|---|---|
Wrapper<T> | updateWrapper | 实体对象封装操作类 UpdateWrapper |
T | entity | 实体对象 |
Collection<T> | entityList | 实体对象集合 |
int | batchSize | 更新批次数量 |
5、Get
//根据 ID 查询 T getById(Serializable id); //根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1") T getOne(Wrapper<T>queryWrapper); //根据 Wrapper,查询一条记录 T getOne(Wrapper<T> queryWrapper, booleanthrowEx); //根据 Wrapper,查询一条记录 Map<String, Object> getMap(Wrapper<T>queryWrapper); //根据 Wrapper,查询一条记录 <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
类型 | 参数名 | 描述 |
---|---|---|
Serializable | id | 主键ID |
Wrapper<T> | queryWrapper | 实体对象封装操作类 QueryWrapper |
boolean | throwEx | 有多个 result 是否抛出异常 |
T | entity | 实体对象 |
Function<? super Object, V> | mapper | 转换函数 |
6、List
//查询所有 List<T>list(); //查询列表 List<T> list(Wrapper<T>queryWrapper); //查询(根据ID 批量查询) Collection<T> listByIds(Collection<? extends Serializable>idList); //查询(根据 columnMap 条件) Collection<T> listByMap(Map<String, Object>columnMap); //查询所有列表 List<Map<String, Object>>listMaps(); //查询列表 List<Map<String, Object>> listMaps(Wrapper<T>queryWrapper); //查询全部记录 List<Object>listObjs(); //查询全部记录 <V> List<V> listObjs(Function<? super Object, V>mapper); //根据 Wrapper 条件,查询全部记录 List<Object> listObjs(Wrapper<T>queryWrapper); //根据 Wrapper 条件,查询全部记录 <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
类型 | 参数名 | 描述 |
---|---|---|
Wrapper<T> | queryWrapper | 实体对象封装操作类 QueryWrapper |
Collection<? extends Serializable> | idList | 主键ID列表 |
Map<?String, Object> | columnMap | 表字段 map 对象 |
Function<? super Object, V> | mapper | 转换函数 |
7、Page
//无条件分页查询 IPage<T> page(IPage<T>page); //条件分页查询 IPage<T> page(IPage<T> page, Wrapper<T>queryWrapper); //无条件分页查询 IPage<Map<String, Object>> pageMaps(IPage<T>page); //条件分页查询 IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
类型 | 参数名 | 描述 |
---|---|---|
IPage<T> | page | 翻页对象 |
Wrapper<T> | queryWrapper | 实体对象封装操作类 QueryWrapper |
8、Count
//查询总记录数 intcount(); //根据 Wrapper 条件,查询总记录数 int count(Wrapper<T> queryWrapper);
类型 | 参数名 | 描述 |
---|---|---|
Wrapper<T> | queryWrapper | 实体对象封装操作类 QueryWrapper |
五、Mapper CRUD 接口
说明:
- 通用 CRUD 封装BaseMapper(opens new window)接口,为
Mybatis-Plus
启动时自动解析实体表关系映射转换为Mybatis
内部对象注入容器 - 泛型
T
为任意实体对象 - 参数
Serializable
为任意类型主键Mybatis-Plus
不推荐使用复合主键约定每一张表都有自己的唯一id
主键 - 对象
Wrapper
为条件构造器
1、Insert
//插入一条记录 int insert(T entity);
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 |
2、Delete
//根据 entity 条件,删除记录 int delete(@Param(Constants.WRAPPER) Wrapper<T>wrapper); //删除(根据ID 批量删除) int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable>idList); //根据 ID 删除 intdeleteById(Serializable id); //根据 columnMap 条件,删除记录 int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
类型 | 参数名 | 描述 |
---|---|---|
Wrapper<T> | wrapper | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) |
Serializable | id | 主键ID |
Map<String, Object> | columnMap | 表字段 map 对象 |
3、Update
//根据 whereWrapper 条件,更新记录 int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T>whereWrapper); //根据 ID 修改 int updateById(@Param(Constants.ENTITY) T entity);
类型 | 参数名 | 描述 |
---|---|---|
T | entity | 实体对象 (set 条件值,可为 null) |
Wrapper<T> | updateWrapper | 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) |
4、Select
//根据 ID 查询 T selectById(Serializable id); //根据 entity 条件,查询一条记录 T selectOne(@Param(Constants.WRAPPER) Wrapper<T>queryWrapper); //查询(根据ID 批量查询) List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable>idList); //根据 entity 条件,查询全部记录 List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T>queryWrapper); //查询(根据 columnMap 条件) List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object>columnMap); //根据 Wrapper 条件,查询全部记录 List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T>queryWrapper); //根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值 List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T>queryWrapper); //根据 entity 条件,查询全部记录(并翻页) IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T>queryWrapper); //根据 Wrapper 条件,查询全部记录(并翻页) IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T>queryWrapper); //根据 Wrapper 条件,查询总记录数 Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
类型 | 参数名 | 描述 |
---|---|---|
Serializable | id | 主键ID |
Wrapper<T> | queryWrapper | 实体对象封装操作类(可以为 null) |
Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) |
Map<String, Object> | columnMap | 表字段 map 对象 |
IPage<T> | page | 分页查询条件(可以为 RowBounds.DEFAULT) |