MybatisPlus官网:https://baomidou.com/
(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 语句来实现条件查询。
List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("age", 18));
List<User> users = userMapper.selectList(new QueryWrapper<User>().ne("age", 18));
List<User> users = userMapper.selectList(new QueryWrapper<User>().gt("age", 18));
List<User> users = userMapper.selectList(new QueryWrapper<User>().ge("age", 18));
List<User> users = userMapper.selectList(new QueryWrapper<User>().lt("age", 18));
List<User> users = userMapper.selectList(new QueryWrapper<User>().le("age", 18));
// 匹配前缀
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%"));
在 Spring Boot 项目中,我们可以通过以下步骤来配置 MybatisPlus:
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3.1version>
dependency>
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
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.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 方法
}
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> {
}
package com.example.demo.service;
import com.baomidou.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> {
}
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);
}
}