Beginner's springboot path, mybatis-plus use

0-Preface

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-Integration

1-1, Add Dependency

        <!-- 9,Integrate mybatis-plus  -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

 

1-2, Configuration File Add Configuration

#mybatis-plus
mybatis-plus.mapper-locations=classpath:mapper/*Mapper.xml
mybatis-plus.type-aliases-package=com.anson.model
mybatis-plus.configuration.map-underscore-to-camel-case=true

 

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

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

 

1-4, model (tool generation is sufficient)

package com.anson.model;
import java.io.Serializable;

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) {
        this.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();
    }
}

 

1-5,mapper(XML)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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" />
  </resultMap>
  <sql id="Base_Column_List">
    id, userName, passWord, realName
  </sql>

</mapper>

 

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;

@Repository
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;

@Service
public class UserService extends ServiceImpl<UserMapper, User>
{
    @Autowired
    UserMapper usermapper;
}

 

1-8,controller

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--------------------
    //1,increase

        User user= new User();
        user.setId(6);
        user.setUsername("anson");
        user.setPassword("1236216");
        user.setRealname("Wang Qiqi");
        userservice.save(user);

    }
    //2,Delete
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 = userservice.page(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):

 

Example:

        //1,Delete by condition
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .isNull("username")
                .ge("id", 12)
                .isNotNull("realname");
        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<>();
        queryWrapper3.like("username", "a");
        IPage<User> userIPage = userservice.page(page2, queryWrapper3);

        //4,Update on condition

        User user2 = new User();
        user2.setRealname("zhangsan");

        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