实战系列(二)| MybatisPlus详细介绍,包含代码详解

news/2025/2/23 15:55:11

目录

  • 1. MybatisPlus 的基本功能
  • 2. 基本用法
  • 3. MybatisPlus 的配置
  • 4. MybatisPlus 的实体类、Mapper 接口、Service 类和 Controller 类

MybatisPlus 是一个功能强大的 MyBatis 增强工具,它提供了丰富的特性来简化操作数据库的代码。它主要用于简化 JDBC 操作,节省开发时间,并能够自动化完成所有的 CRUD 代码。
在这里插入图片描述

MybatisPlus官网:https://baomidou.com/

1. MybatisPlus 的基本功能

  • 提供丰富的 CRUD 方法,包括:insert、selectById、selectBatchIds、update、delete 等。
  • 提供乐观锁功能,支持版本号管理。
  • 提供查询条件生成器,简化查询条件的编写。
  • 提供代码生成器,自动生成实体类、Mapper 接口、Service 类、Controller 类等。
  • 支持多种数据库类型,如:MySQL、Oracle、SQL Server 等。

2. 基本用法

(1) 分页插件
MybatisPlus 的分页逻辑底层是通过分页插件来完成的。分页插件的实现原理主要是基于 MyBatis 的动态 SQL 生成,通过 Mybatis 的 count 和 offset 的实现来实现分页功能。
(2) 自动装配
MybatisPlus 提供了自动装配功能,可以自动根据实体类生成对应的 Mapper 接口、Service 类和 Controller 类。自动装配的实现原理是基于 Mybatis 的 XML 配置文件,通过解析 XML 文件生成对应的 Java 代码。
(3) 条件查询
MybatisPlus 提供了丰富的查询方法,如 eq、ne、gt、ge、lt、le、like 等。这些查询方法底层是通过 MyBatis 的动态 SQL 生成来实现的,通过拼接 SQL 语句来实现条件查询。

  1. eq:等于
    用于查询字段值等于指定值的记录。
java">List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("age", 18));  
  1. ne:不等于
    用于查询字段值不等于指定值的记录。
java">List<User> users = userMapper.selectList(new QueryWrapper<User>().ne("age", 18));  
  1. gt:大于
    用于查询字段值大于指定值的记录。
java">List<User> users = userMapper.selectList(new QueryWrapper<User>().gt("age", 18));  
  1. ge:大于等于
    用于查询字段值大于等于指定值的记录。
java">List<User> users = userMapper.selectList(new QueryWrapper<User>().ge("age", 18));  
  1. lt:小于
    用于查询字段值小于指定值的记录。
java">List<User> users = userMapper.selectList(new QueryWrapper<User>().lt("age", 18));  
  1. le:小于等于
    用于查询字段值小于等于指定值的记录。
java">List<User> users = userMapper.selectList(new QueryWrapper<User>().le("age", 18));  
  1. like:模糊查询
    用于根据指定的字符串进行模糊查询。可以指定匹配前缀、后缀或指定多个字符。
java">// 匹配前缀  
List<User> users = userMapper.selectList(new QueryWrapper<User>().like("name", "zhang%"));
// 匹配后缀  
List<User> users = userMapper.selectList(new QueryWrapper<User>().like("name", "%zhang"));
// 匹配多个字符  
List<User> users = userMapper.selectList(new QueryWrapper<User>().like("name", "z%a%"));  

3. MybatisPlus 的配置

在 Spring Boot 项目中,我们可以通过以下步骤来配置 MybatisPlus:

  • 在 pom.xml 文件中添加 MybatisPlus 的依赖:
<dependency>  
   <groupId>com.baomidou</groupId>  
   <artifactId>mybatis-plus-boot-starter</artifactId>  
   <version>3.4.3.1</version>  
</dependency>  
  • 在 application.properties 文件中配置 MybatisPlus:
mybatis-plus.mapper-locations=classpath:mapper/*.xml  
mybatis-plus.type-aliases-package=com.example.demo.entity  
mybatis-plus.global-config.id-type=auto  
mybatis-plus.global-config.db-config.logic-delete-value=1  
mybatis-plus.global-config.db-config.logic-not-delete-value=0  

4. MybatisPlus 的实体类、Mapper 接口、Service 类和 Controller 类

  • 实体类(Entity):实体类是用于映射数据库表的,通常包含了表的字段以及对应的 getter 和 setter 方法。例如,假设有一个用户表(user),那么对应的实体类可能如下:
java">package com.example.demo.entity;
import com.baomidou.mybatisplus>mybatisplus.annotation.IdType;  
import com.baomidou.mybatisplus>mybatisplus.annotation.TableId;  
import com.baomidou.mybatisplus>mybatisplus.annotation.TableName;
@TableName("user")  
public class User {  
   @TableId(type = IdType.AUTO)  
   private Long id;  
   private String name;  
   private Integer age;  
   private String email;
   // getter 和 setter 方法  
}
  • Mapper 接口:Mapper 接口用于定义与数据库表相关的操作。例如,对于用户表(user),对应的 Mapper 接口可能如下:
java">package com.example.demo.mapper;
import com.baomidou.mybatisplus>mybatisplus.core.mapper.BaseMapper;  
import com.example.demo.entity.User;  
import org.apache.ibatis.annotations.Mapper;
@Mapper  
public interface UserMapper extends BaseMapper<User> {  
}
  • Service 类:Service 类用于处理业务逻辑。它通常包含了一些与数据库操作相关的方法。例如,对于用户表(user),对应的 Service 类可能如下:
java">package com.example.demo.service;
import com.baomidou.mybatisplus>mybatisplus.extension.service.impl.ServiceImpl;  
import com.example.demo.entity.User;  
import com.example.demo.mapper.UserMapper;  
import org.springframework.stereotype.Service;
@Service  
public class UserService extends ServiceImpl<UserMapper, User> {  
}
  • Controller 类:Controller 类用于处理 HTTP 请求。它通常包含了一些与数据库操作相关的方法。例如,对于用户表(user),对应的 Controller 类可能如下:
java">package com.example.demo.controller;
import com.example.demo.entity.User;  
import com.example.demo.service.UserService;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController  
@RequestMapping("/user")  
public class UserController {  
   @Autowired  
   private UserService userService;
   @GetMapping  
   public List<User> getUsers() {  
       return userService.list();  
   }
   @GetMapping("/{id}")  
   public User getUserById(@PathVariable Long id) {  
       return userService.getById(id);  
   }
   @PostMapping  
   public void addUser(@RequestBody User user) {  
       userService.save(user);  
   }
   @PutMapping("/{id}")  
   public void updateUser(@PathVariable Long id, @RequestBody User user) {  
       userService.updateById(id, user);  
   }
   @DeleteMapping("/{id}")  
   public void deleteUser(@PathVariable Long id) {  
       userService.removeById(id);  
   }  
}

http://www.niftyadmin.cn/n/5001212.html

相关文章

mysql 数据库面试题整理

Mysql 中 MyISAM 和 InnoDB 的区别 1、InnoDB 支持事务MyISAM 不支持 2、InnoDB 支持外键MyISAM 不支持 3、InnoDB 是聚集索引&#xff0c;MyISAM 是非聚集索引 4、InnoDB 不保存表的具体行数 5、InnoDB 最小的锁粒度是行锁&#xff0c;MyISAM是表锁 mysql中有就更新&#xf…

webpack5 (三)

webpack 高级配置 其实就是对 webpack 进行优化&#xff0c;让代码在编译/运行时性能更好 1. 提升开发体验 2. 提升打包构建速度 3. 减少代码体积 4. 优化代码运行性能 一、提升开发体验 sourcemap 在编译打包后所有的 css 和 js 都合并为了一个文件&#xff0c;并多了很多…

layui 新增tab标签页

// $("#fjyj").click(function () {// //window.location.href "/sysconfig/SuperVisorEdit";// navigateToTeamPersonModule(/CollectData/GradeWarning/EduIndex, 分级预警);// });function navigateToTeamPersonModule(url, name) {var ids n…

无人化在线静电监控系统的组成

无人化在线静电监控系统是一种用于检测和监控静电情况的系统&#xff0c;它可以自动地实时监测各个区域的静电水平&#xff0c;并在出现异常情况时发出报警信号。静电监控报警器则是该系统中的一个重要组成部分&#xff0c;用于接收和传达报警信号。 无人化在线静电监控系统通…

Qt/C++音视频开发49-推流到各种流媒体服务程序

一、前言 最近将推流程序完善了很多功能&#xff0c;尤其是增加了对多种流媒体服务程序的支持&#xff0c;目前支持mediamtx、LiveQing、EasyDarwin、nginx-rtmp、ZLMediaKit、srs、ABLMediaServer等&#xff0c;其中经过大量的对比测试&#xff0c;个人比较建议使用mediamtx和…

vue3请求成功后实现类似打字效果输出

要在 Vue 3 中实现请求成功后的类似打字效果输出&#xff0c;您可以使用 ​axios​ 或其他适合您的方法来发起异步请求。在请求成功后&#xff0c;您可以将返回的文本存储在响应式对象中&#xff0c;并使用一段时间间隔逐个字符地将文本输出到界面上。下面是一个示例代码&#…

R语言STAN贝叶斯线性回归模型分析气候变化影响北半球海冰范围和可视化检查模型收敛性...

原文链接&#xff1a;http://tecdat.cn/?p24334 像任何统计建模一样&#xff0c;贝叶斯建模可能需要为你的研究问题设计合适的模型&#xff0c;然后开发该模型&#xff0c;使其符合你的数据假设并运行&#xff08;点击文末“阅读原文”获取完整代码数据&#xff09;。 相关视频…

【Linux】高级IO和多路转接 | select/poll/epoll

多路转接和高级IO 咳咳&#xff0c;写的时候出了点问题&#xff0c;标点符号全乱了&#xff08;批量替换了几次&#xff09;&#xff0c;干脆就把全文的逗号和句号都改成英文的了&#xff08;不然代码块里面的代码都是中文标点就跑不动了&#xff09; 1.高级IO 1.1 五种IO模型…