Practical series (2) | MybatisPlus detailed introduction, including detailed code explanation


MybatisPlus is a powerful MyBatis enhancement tool that provides rich features to simplify the code for operating databases. It is mainly used to simplify JDBC operations, save development time, and automate all CRUD codes.
insert image description here

MybatisPlus official website: https://baomidou.com/

1. Basic functions of MybatisPlus

  • Provides rich CRUD methods, including: insert, selectById, selectBatchIds, update, delete, etc.
  • Provide optimistic locking function and support version number management.
  • A query condition generator is provided to simplify the writing of query conditions.
  • Provide a code generator to automatically generate entity classes, Mapper interfaces, Service classes, Controller classes, etc.
  • Support multiple database types, such as: MySQL, Oracle, SQL Server, etc.

2. Basic Usage

(1) Paging plug-in
MybatisPlus' paging logic bottom layer is completed by paging plug-in. The realization principle of the paging plug-in is mainly based on the dynamic SQL generation of MyBatis, and the paging function is realized through the realization of the count and offset of MyBatis.
(2) Automatic assembly
MybatisPlus provides the automatic assembly function, which can automatically generate the corresponding Mapper interface, Service class and Controller class according to the entity class. The realization principle of automatic assembly is based on the XML configuration file of Mybatis, and the corresponding Java code is generated by parsing the XML file.
(3) Conditional query
MybatisPlus provides a wealth of query methods, such as eq, ne, gt, ge, lt, le, like, etc. The bottom layer of these query methods is realized through the dynamic SQL generation of MyBatis, and the conditional query is realized by splicing SQL statements.

  1. eq: equal
    to query records whose field value is equal to the specified value.
List<User> users = userMapper.selectList(new QueryWrapper<User>().eq("age", 18));  
  1. ne: not equal
    It is used to query the records whose field value is not equal to the specified value.
List<User> users = userMapper.selectList(new QueryWrapper<User>().ne("age", 18));  
  1. gt: greater than
    is used to query records whose field value is greater than the specified value.
List<User> users = userMapper.selectList(new QueryWrapper<User>().gt("age", 18));  
  1. ge: greater than or equal
    to query records whose field value is greater than or equal to the specified value.
List<User> users = userMapper.selectList(new QueryWrapper<User>().ge("age", 18));  
  1. lt: less than
    Used to query records whose field value is less than the specified value.
List<User> users = userMapper.selectList(new QueryWrapper<User>().lt("age", 18));  
  1. le: less than or equal
    to query records whose field value is less than or equal to the specified value.
List<User> users = userMapper.selectList(new QueryWrapper<User>().le("age", 18));  
  1. like: fuzzy query
    It is used to perform fuzzy query based on the specified string. You can specify to match a prefix, a suffix, or specify multiple characters.
// 匹配前缀  
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 configuration

In the Spring Boot project, we can configure MybatisPlus through the following steps:

  • Add the MybatisPlus dependency in the pom.xml file:
<dependency>  
   <groupId>com.baomidou</groupId>  
   <artifactId>mybatis-plus-boot-starter</artifactId>  
   <version>3.4.3.1</version>  
</dependency>  
  • Configure MybatisPlus in the application.properties file:
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. Entity class, Mapper interface, Service class and Controller class of MybatisPlus

  • Entity class (Entity): The entity class is used to map the database table, and usually contains the fields of the table and the corresponding getter and setter methods. For example, suppose there is a user table (user), then the corresponding entity class may be as follows:
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 方法  
}
  • Mapper interface: The Mapper interface is used to define operations related to database tables. For example, for the user table (user), the corresponding Mapper interface may be as follows:
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> {
    
      
}
  • Service class: The Service class is used to handle business logic. It usually contains some methods related to database operations. For example, for the user table (user), the corresponding Service class might be as follows:
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> {
    
      
}
  • Controller class: The Controller class is used to handle HTTP requests. It usually contains some methods related to database operations. For example, for the user table (user), the corresponding Controller class might be as follows:
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);  
   }  
}

Guess you like

Origin blog.csdn.net/superdangbo/article/details/132685409