目录
1、简介2、适用情况3、mybatis-plus前期准备(工程将以 H2 作为默认数据库进行演示)
1、使用 Spring Initializer快速初始化一个 Spring Boot 工程2、导入mybatis-plus依赖3、yml文件中添加相关配置4、在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹5、编写实体类和Mapper类6、service继承IService7、serviceImpl继承ServiceImpl4、mybatis-plus的sql操作(Service层)
1、Save:插入2、SaveOrUpdate:修改插入3、Remove:删除4、Update:更新5、Get:单体查询6、List:多条查询7、Page:分页查询(需要导入相关的配置或依赖)8、Count:记录数据个数5、详细资料 总结
1、简介
MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。
MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。
2、适用情况
1、对于只进行单表操作来说,mybatis-plus代码量比mybatis的代码量少很多,极大的提高了开发效率
2、对于多表操作来说,更推荐mybatis,因为mybatis-plus的方法比较难以理解,用起来不太方便,不如自己写sql语句的逻辑那么清晰明了
1、对于只进行单表操作来说,mybatis-plus代码量比mybatis的代码量少很多,极大的提高了开发效率
2、对于多表操作来说,更推荐mybatis,因为mybatis-plus的方法比较难以理解,用起来不太方便,不如自己写sql语句的逻辑那么清晰明了
3、mybatis-plus前期准备(工程将以 H2 作为默认数据库进行演示)
1、使用 Spring Initializer快速初始化一个 Spring Boot 工程
2、导入mybatis-plus依赖
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
3、yml文件中添加相关配置
| 1 2 3 4 5 6 7 8 9 | # DataSource Config spring: datasource: driver-class-name: org.h2.Driver schema: classpath:db/schema-h2.sql data: classpath:db/data-h2.sql url: jdbc:h2:mem:test username: root password: test |
4、在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
| 1 | @MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper") |
5、编写实体类和Mapper类
| 1 2 3 4 5 6 7 8 9 10 11 12 | //entity @Data public class User { private Long id; private String name; private Integer age; private String email; } //Mapper public interface UserMapper extends baseMapper } |
6、service继承IService
| 1 | public interface UserService extends IService |
7、serviceImpl继承ServiceImpl
| 1 | public class UserServiceImpl extends ServiceImpl |
4、mybatis-plus的sql操作(Service层)
1、Save:插入
| 1 2 3 4 5 6 | // 插入一条记录(选择字段,策略插入) boolean save(T entity); // 插入(批量) boolean saveBatch(Collection // 插入(批量) boolean saveBatch(Collection |
2、SaveOrUpdate:修改插入
| 1 2 3 4 5 6 7 8 | // TableId 注解存在更新记录,否插入一条记录 boolean saveOrUpdate(T entity); // 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法 boolean saveOrUpdate(T entity, Wrapper // 批量修改插入 boolean saveOrUpdateBatch(Collection // 批量修改插入 boolean saveOrUpdateBatch(Collection |
3、Remove:删除
| 1 2 3 4 5 6 7 8 | // 根据 entity 条件,删除记录 boolean remove(Wrapper // 根据 ID 删除 boolean removeById(Serializable id); // 根据 columnMap 条件,删除记录 boolean removeByMap(Map // 删除(根据ID 批量删除) boolean removeByIds(Collection extends Serializable> idList); |
4、Update:更新
| 1 2 3 4 5 6 7 8 9 10 | // 根据 UpdateWrapper 条件,更新记录 需要设置sqlset boolean update(Wrapper // 根据 whereWrapper 条件,更新记录 boolean update(T updateEntity, Wrapper // 根据 ID 选择修改 boolean updateById(T entity); // 根据ID 批量更新 boolean updateBatchById(Collection // 根据ID 批量更新 boolean updateBatchById(Collection |
5、Get:单体查询
| 1 2 3 4 5 6 7 8 9 10 | // 根据 ID 查询 T getById(Serializable id); // 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1") T getOne(Wrapper // 根据 Wrapper,查询一条记录 T getOne(Wrapper // 根据 Wrapper,查询一条记录 Map // 根据 Wrapper,查询一条记录 |
6、List:多条查询
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // 查询所有 List // 查询列表 List // 查询(根据ID 批量查询) Collection // 查询(根据 columnMap 条件) Collection // 查询所有列表 List // 查询列表 List // 查询全部记录 List // 查询全部记录 // 根据 Wrapper 条件,查询全部记录 List // 根据 Wrapper 条件,查询全部记录 |
7、Page:分页查询(需要导入相关的配置或依赖)
| 1 2 3 4 5 6 7 8 | // 无条件分页查询 IPage // 条件分页查询 IPage // 无条件分页查询 IPage // 条件分页查询 IPage |
8、Count:记录数据个数
| 1 2 3 4 | // 查询总记录数 int count(); // 根据 Wrapper 条件,查询总记录数 int count(Wrapper |
5、详细资料
由于篇幅有限,先写到这里
具体内容请看
1、mybatis-plus官网:Redirect
2、MyBatis-Plus 通用IService使用详解
由于篇幅有限,先写到这里
具体内容请看
1、mybatis-plus官网:Redirect
2、MyBatis-Plus 通用IService使用详解
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注!



