SpringBoot的Mybatis-plus实战之基础知识
Mybatis-plus是对MyBatis的增强工具,在MyBatis的基础上进行了扩展和优化,提供了CRUD操作、代码生成器、条件构造器等功能,大大减少了开发者的工作量。本文将详细介绍如何在SpringBoot中集成Mybatis-plus,并展示其基础使用方法。

一、环境配置
1. 引入依赖
在SpringBoot项目中,首先需要在 pom.xml中引入Mybatis-plus的依赖。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2. 配置数据源
在 application.yml或 application.properties文件中配置数据源。
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
3. 配置Mybatis-plus
在配置文件中添加Mybatis-plus的配置。
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.example.demo.entity
二、实体类和Mapper
1. 创建实体类
创建与数据库表对应的实体类,并使用Mybatis-plus提供的注解进行配置。
package com.example.demo.entity;
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;
private String email;
}
2. 创建Mapper接口
创建Mapper接口并继承 BaseMapper,Mybatis-plus会自动为其生成实现类。
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
三、基本CRUD操作
1. 创建Service类
在Service层中使用 UserMapper进行CRUD操作。
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.User;
public interface UserService extends IService<User> {
}
实现Service接口。
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
2. 使用Service进行CRUD操作
在Controller层中调用Service进行操作。
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;
@PostMapping
public boolean saveUser(@RequestBody User user) {
return userService.save(user);
}
@DeleteMapping("/{id}")
public boolean deleteUser(@PathVariable Long id) {
return userService.removeById(id);
}
@PutMapping
public boolean updateUser(@RequestBody User user) {
return userService.updateById(user);
}
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.getById(id);
}
@GetMapping
public List<User> getAllUsers() {
return userService.list();
}
}
四、条件构造器
Mybatis-plus提供了强大的条件构造器,可以方便地进行复杂的查询操作。
1. 示例代码
使用 QueryWrapper进行条件查询。
package com.example.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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("/age/{age}")
public List<User> getUsersByAge(@PathVariable Integer age) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("age", age);
return userService.list(queryWrapper);
}
}
五、分页插件
Mybatis-plus集成了分页插件,支持分页查询。
1. 引入分页插件
在配置类中引入分页插件。
package com.example.demo.config;
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();
}
}
2. 使用分页查询
在Service层中进行分页查询。
package com.example.demo.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.*;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/page")
public IPage<User> getUserPage(@RequestParam int page, @RequestParam int size) {
Page<User> userPage = new Page<>(page, size);
return userService.page(userPage);
}
}
六、分析说明表
| 功能 | 描述 | 示例代码 |
|---|---|---|
| 引入依赖 | 在 pom.xml中引入Mybatis-plus依赖 |
<dependency>...</dependency> |
| 配置数据源 | 在 application.yml中配置数据库连接信息 |
spring.datasource.url=jdbc:mysql://... |
| 创建实体类 | 创建与数据库表对应的实体类 | public class User { ... } |
| 创建Mapper接口 | 创建Mapper接口并继承 BaseMapper |
public interface UserMapper extends BaseMapper<User> |
| 基本CRUD操作 | 在Service和Controller层中实现基本的CRUD操作 | public class UserController { ... } |
| 条件构造器 | 使用 QueryWrapper进行复杂条件查询 |
QueryWrapper<User> queryWrapper = ... |
| 分页插件 | 配置和使用分页插件 | public IPage<User> getUserPage(...) { ... } |
总结
通过本文的介绍,您应该已经掌握了如何在SpringBoot项目中集成和使用Mybatis-plus。Mybatis-plus极大地简化了CRUD操作,提供了强大的条件构造器和分页插件,能够有效提升开发效率。通过实际操作和示例代码,您可以快速上手并应用于自己的项目中。合理利用Mybatis-plus的功能,可以使您的项目更加高效、稳定和易于维护。


