
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。以下是一些 MyBatis-Plus 的基本语法和用法示例:
1. 环境配置
首先,你需要在项目中引入 MyBatis-Plus 的依赖。以 Maven 为例,你可以在 pom.xml 中添加如下依赖:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本号</version> </dependency>然后,配置数据源和 MyBatis-Plus 的相关设置。通常在 Spring Boot 项目中,可以在 application.yml 或 application.properties 中进行配置。
2. 实体类与 Mapper 接口
实体类
实体类通常对应数据库中的一张表。使用 MyBatis-Plus 注解可以简化很多配置。例如:
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("user") // 指定对应的数据库表名 public class User { @TableId // 主键字段 private Long id; private String name; private Integer age; // 其他字段... }Mapper 接口
Mapper 接口用于定义数据访问方法。MyBatis-Plus 提供了基础的 CRUD 方法,你可以直接继承 BaseMapper 来获得这些方法:
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapper<User> { // 可以自定义其他方法... }3. Service 层与 Controller 层
在实际开发中,通常会通过 Service 层来封装业务逻辑,并通过 Controller 层提供 API 接口。
Service 层
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; @Service public class UserService extends ServiceImpl<UserMapper, User> { // 可以自定义其他业务方法... }Controller 层
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("/") public List<User> getAllUsers() { return userService.list(); } @PostMapping("/") public boolean addUser(@RequestBody User user) { return userService.save(user); } // 其他接口... }4. 条件构造器
MyBatis-Plus 提供了强大的条件构造器 QueryWrapper 和 UpdateWrapper,用于构建复杂的查询和更新条件。
查询示例
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/byName") public List<User> getUsersByName(@RequestParam String name) { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("name", name); return userService.list(queryWrapper); } }更新示例
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController public class UserController { @Autowired private UserService userService; @PutMapping("/{id}") public boolean updateUserAge(@PathVariable Long id, @RequestParam Integer age) { UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("id", id).set("age", age); return userService.update(null, updateWrapper); // 第一个参数为 null 表示更新所有匹配记录的字段值,具体字段由 updateWrapper 指定 } }5. 分页插件
MyBatis-Plus 还提供了分页插件,可以非常方便地实现分页功能。
配置分页插件
在 MyBatis-Plus 配置类中注册分页插件:
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }使用分页查询
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/page") public IPage<User> getUserPage(@RequestParam int currentPage, @RequestParam int pageSize) { Page<User> page = new Page<>(currentPage, pageSize); return userService.page(page, null); // 第二个参数为 null 表示不带任何查询条件 } }以上只是 MyBatis-Plus 的基础用法介绍,实际上 MyBatis-Plus 还有更多高级功能和特性,如性能分析插件、乐观锁插件等,可以根据项目需求进行深入学习和使用。
