Mybatis Plus (特性、快速入门、日志、CRUD)

[复制链接]
发表于 6 天前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
MyBatis-Plus:特性、快速入门与 CRUD 实战

一、简介

MyBatis-Plus(简称 MP)是 MyBatis 的增强工具,在 MyBatis 基础上只做增强不做改变,为简化开发、提高效率而生。愿景是成为 MyBatis 最好的搭档。

二、核心特性

  • 无侵入:只做增强不做改变,引入后对现有工程无影响。
  • 损耗小:启动即自动注入基本 CRUD,性能基本无损耗,直接面向对象操作 BaseMapper。
  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,少量配置即可实现单表大部分 CRUD,并带强大条件构造器。
  • 支持 Lambda 形式调用:用 Lambda 表达式编写查询条件,无需担心字段名写错。
  • 支持主键自动生成:支持多种主键策略(含分布式 ID 生成器 Sequence)。
  • 支持 ActiveRecord 模式:实体类继承 Model 即可进行 CRUD。
  • 内置代码生成器:可快速生成 Mapper、Model、Service、Controller 层代码。
  • 内置分页插件:基于 MyBatis 物理分页,写分页等同于普通 List 查询。
  • 内置性能分析插件:可输出 SQL 及其执行时间,便于揪出慢查询。
  • 内置全局拦截插件:对全表 delete/update 提供智能分析阻断,预防误操作。


三、快速入门

使用第三方组件的一般流程:导入依赖 → 研究配置 → 编写代码 → 扩展提升。具体:建表并搭建 Spring Boot 工程,引入依赖。

  1. <dependency>
  2.     <groupId>com.baomidou</groupId>
  3.     <artifactId>mybatis-plus-boot-starter</artifactId>
  4.     <version>3.4.2</version>
  5. </dependency>
复制代码


配置数据源(MySQL 8,注意时区):

  1. url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
复制代码


传统 MyBatis 开发路径:pojo - dao(mapper.xml) - service - controller;使用 MP 后为:pojo - mapper 接口 - 使用。

Mapper 接口示例:

  1. package com.ithema.reggie.mapper;

  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.ithema.reggie.entity.Employee;
  4. import org.apache.ibatis.annotations.Mapper;

  5. @Mapper
  6. public interface EmployeeMapper extends BaseMapper<Employee> {
  7. }
复制代码


启动类扫描 mapper 包:

  1. @MapperScan("com.ithema.reggie.mapper")
复制代码


查询全部示例:

  1. @SpringBootTest
  2. class ReggieApplicationTests {
  3.     @Autowired
  4.     private EmployeeMapper mapper;

  5.     @Test
  6.     void contextLoads() {
  7.         List<Employee> employees = mapper.selectList(null);
  8.         employees.forEach(System.out::println);
  9.     }
  10. }
复制代码


四、配置日志日志

默认 SQL 不可见,开发时可开启 StdOutImpl 日志日志,上线关闭。

  1. mybatis-plus:
  2.   configuration:
  3.     map-underscore-to-camel-case: true
  4.     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
复制代码


五、CRUD 扩展

插入(insert)

  1. @Test
  2. public void insert() {
  3.     User user = new User();
  4.     user.setName("郝泾钊");
  5.     user.setAge(8);
  6.     mapper.insert(user);
  7. }
复制代码


发现未设置 id 时会自动插入(数据库 id 为全局唯一默认值,如雪花算法)。主键生成策略包括 uuid、自增 id、雪花算法、redis、zookeeper 等。

更新(update)

  1. @Test
  2. public void update() {
  3.     User user = new User();
  4.     user.setId(6L);
  5.     user.setName("郝泾钊");
  6.     user.setAge(8);
  7.     mapper.updateById(user);
  8. }
复制代码


查询(select)

  1. @Test
  2. public void selectById() {
  3.     User user = userMapper.selectById(1L);
  4. }

  5. @Test
  6. public void selectByBatchId() {
  7.     List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
  8.     users.forEach(System.out::println);
  9. }

  10. @Test
  11. public void selectByMap() {
  12.     HashMap<String, Object> map = new HashMap<>();
  13.     map.put("name", "郝泾钊");
  14.     List<User> users = userMapper.selectByMap(map);
  15.     users.forEach(System.out::println);
  16. }
复制代码


分页查询

配置分页拦截器:

  1. @Bean
  2. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  3.     MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  4.     interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
  5.     return interceptor;
  6. }
复制代码


使用内置 Page 对象:

  1. @Test
  2. public void testPage() {
  3.     Page<User> page = new Page<>(1, 5);
  4.     userMapper.selectPage(page, null);
  5.     page.getRecords().forEach(System.out::println);
  6. }
复制代码


删除(delete)

  1. @Test
  2. public void testDelete() {
  3.     userMapper.deleteById(1L);
  4.     userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
  5.     HashMap<String, Object> map = new HashMap<>();
  6.     map.put("name", "郝泾钊");
  7.     userMapper.deleteByMap(map);
  8. }
复制代码


六、逻辑删除

逻辑删除:数据库未真正移除,通过 deleted=0 变 deleted=1 标记;物理删除则是真实移除。逻辑删除可防止数据丢失,类似回收站。

  • 数据库增加 deleted 字段
  • 实体类添加 deleted 属性并加 @TableLogic 注解
  • 配置逻辑删除值


  1. mybatis-plus:
  2.   global-config:
  3.     db-config:
  4.       logic-delete-field: flag
  5.       logic-delete-value: 1
  6.       logic-not-delete-value: 0
复制代码


查询时会自动过滤已逻辑删除的记录。

qidao123.com企服评测-应用市场 编辑提示
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表