SpringBoot integration Mysql, Mybatis, Mybatis-Plus, the implementation of add, delete, change and check

One, foreword

Mybatis-Plus encapsulates paging and can be used with a simple configuration. I also briefly encapsulated the paging request parameters and the response.

Second, Sql statements

CREATE TABLE `user_info` (
	`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'user id',
	`username` VARCHAR(20) NOT NULL DEFAULT ' ' COMMENT 'Username' COLLATE 'utf8mb4_general_ci',
	`password` VARCHAR(100) NOT NULL DEFAULT ' ' COMMENT 'password' COLLATE 'utf8mb4_general_ci',
	`is_deleted` INT(2) NOT NULL DEFAULT '0' COMMENT 'Deleted 0- Not deleted 1- Deleted',
	`create_time` DATETIME NOT NULL COMMENT 'Creation time',
	`update_time` DATETIME NOT NULL COMMENT 'Update Time'.PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3
;
Copy the code

Configure paging

@Configuration
public class MyConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(a) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // Database paging
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        returninterceptor; }}Copy the code
  • Here’s the page typeDbType.MYSQL

4. Encapsulation of paging request parameters and responses

BasePageParam paging parameter class

package com.llh.springbootpage.param;

/ * * *@author llh
 */
public class BasePageParam {
    private Long pageNum;
    private Long pageSize;

    public Long getPageNum(a) {
        return pageNum;
    }

    public void setPageNum(Long pageNum) {
        this.pageNum = pageNum;
    }

    public Long getPageSize(a) {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString(a) {
        return "BasePageParam{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                '} '; }}Copy the code

BasePageResult paging response class

package com.llh.springbootpage.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/ * * *@author llh
 */
public class BasePageResult<T> {
    private List<T> data;
    private Long totalNum;

    public BasePageResult(List<T> data, Long totalNum) {
        this.data = data;
        this.totalNum = totalNum;
    }

    public static <E> BasePageResult<E> newInstance(IPage<E> page) {
        return new BasePageResult<E>(page.getRecords(), page.getTotal());
    }

    public List<T> getData(a) {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Long getTotalNum(a) {
        return totalNum;
    }

    public void setTotalNum(Long totalNum) {
        this.totalNum = totalNum;
    }

    @Override
    public String toString(a) {
        return "BasePageResult{" +
                "data=" + data +
                ", totalNum=" + totalNum +
                '} '; }}Copy the code

Five, paging fuzzy query

package com.llh.springbootpage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llh.springbootpage.config.CommonResult;
import com.llh.springbootpage.entity.UserInfo;
import com.llh.springbootpage.param.UserInfoPageParam;
import com.llh.springbootpage.result.BasePageResult;
import com.llh.springbootpage.service.UserInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/ * * *@author llh
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/page")
    public CommonResult<BasePageResult<UserInfo>> page(@RequestBody UserInfoPageParam param) {
        IPage<UserInfo> pageParam = new Page<>(param.getPageNum(), param.getPageSize());
        IPage<UserInfo> pageResult = userInfoService.page(pageParam, new LambdaQueryWrapper<UserInfo>()
                .like(UserInfo::getUsername, param.getKeyword()));
        returnCommonResult.success(BasePageResult.newInstance(pageResult)); }}Copy the code
  • Fuzzy match user name according to keyword,LambdaQueryWrapperthelike
  • UserInfoPageParaminheritedBasePageParam

UserInfoPageParam Request parameter class

package com.llh.springbootpage.param;

/ * * *@author admin
 */
public class UserInfoPageParam extends BasePageParam {
    private String keyword;

    public String getKeyword(a) {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    @Override
    public String toString(a) {
        return "UserInfoPageParam{" +
                "keyword='" + keyword + '\' ' +
                "}" + super.toString(); }}Copy the code

Six, test,

Seven, conclusion

  • Code address: github.com/tigerleeli/…
  • Read my previous article for basic usage of Mybatis-Plus

Synchronize wechat official account: Little Tiger’s technology blog