Beginner's springboot path, mybatis-plus use


Mybatis plus is an enhancement to mybatis. After integrating mybatis plus, simple CRUD and paging are not needed to write, which is very convenient and recommended by five stars.


1-1, Add Dependency

        <!-- 9,Integrate mybatis-plus  -->


1-2, Configuration File Add Configuration



1-3. Add paging bean s to the configuration class

    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();


1-4, model (tool generation is sufficient)

package com.anson.model;

public class User implements  Serializable
    private Integer id;
    private String username;
    private String password;
    private String realname;

    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    public String getUsername() {
        return username;

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();

    public String getRealname() {
        return realname;

    public void setRealname(String realname) {
        this.realname = realname == null ? null : realname.trim();



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN" "">
<mapper namespace="com.anson.dao.UserMapper">
  <resultMap id="BaseResultMap" type="com.anson.model.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="userName" jdbcType="VARCHAR" property="username" />
    <result column="passWord" jdbcType="VARCHAR" property="password" />
    <result column="realName" jdbcType="VARCHAR" property="realname" />
  <sql id="Base_Column_List">
    id, userName, passWord, realName



1-6, dao(mapper class), inherits BaseMapper

package com.anson.dao;

import com.anson.model.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import javafx.scene.control.Pagination;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

public interface UserMapper extends BaseMapper<User>


1-7, service Inheritance ServiceImpl

package com.anson.service;

import com.anson.dao.UserMapper;
import com.anson.model.User;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class UserService extends ServiceImpl<UserMapper, User>
    UserMapper usermapper;



No, there is hardly a line of code above, but we can use add, delete, change, and page breaks directly in the controller

//-----------mybatis plus--------------------

        User user= new User();
        user.setRealname("Wang Qiqi");;

userservice.removeById(id); //3,check User b =userservice.getById(id); //4,change User update = new User(); update.setId(6); update.setUsername("alex2266"); userservice.updateById(update); } //5,paging IPage<User> page=new Page(1,3); page =;


Well, is it cool? There are only a few special operations in the project that require writing SQL and methods separately, and most CRUD s don't.


2. Attentions

For underscore'', mybatis is converted to uppercase by default, such as our database table field'update_time', which converts mybatis to'updateTime';

So, in order to avoid pits, direct mandatory recommendations (or conclusions):

1. Database table design, do not use case, use lowercase, multiple combinations of words use underline connection, such as field "update_time", "edit_by"

2. In the model, all fields underlined are converted to camel case, for example:

"update_time" is "updateTime" in the entity class.

"edit_by" is "editBy" in the entity class


3. mybatis plus powerful conditional constructors queryWrapper, updateWrapper

muybatis plus can add various queries and update conditions through a conditional constructor, which has the following four conditional constructors (mainly the last two, Lambda depends on personal preference):

LambdaQueryWrapper: You can also see by name that it is the query Wrapper used in Lambda syntax
Lambda Update Wrapper:Lambda Update Encapsulation Wrapper
QueryWrapper:Entity object encapsulates operation class, not lambda syntax
UpdateWrapper:Update condition wrapper for Entity object update operation

Post a graph of common conditional constructors (pictures from the network):



        //1,Delete by condition
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                .ge("id", 12)
        boolean delete = userservice.remove(queryWrapper);

        //2,Query by condition
        QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("username", "eee");
        User user = userservice.getOne(queryWrapper2);

        //3,Query and Page by Conditions
        Page<User> page2 = new Page<>(1, 5);
        QueryWrapper<User> queryWrapper3 = new QueryWrapper<>();"username", "a");
        IPage<User> userIPage =, queryWrapper3);

        //4,Update on condition

        User user2 = new User();

        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("username", "eee");

        boolean update = userservice.update(user, userUpdateWrapper);

Tags: Java Mybatis Lambda xml SQL

Posted on Sun, 08 Mar 2020 21:02:56 -0400 by adx