Springboot data access (integrating mybatis plus)

Springboot data access (integrating mybatis plus)

For mybatis plus, I have a quick start chapter for Spring. Interested partners can learn about it.

Since mybatis plus has been mentioned earlier, I will directly talk about how SpringBoot integrates mybatis plus. To make a long story short, let's get straight to the point.

First, we need to introduce our dependencies: (since mybatis plus helps us introduce the jar packages of mybatis and jdbc, we'd better remove the jar packages of mybatis and jdbc we previously configured)

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>

All customizations to the properties of mybatis plus are in the mybatis plus prefix.
The default value of the configured maperlocations is classpath*:/mapper/**/*.xml. For convenience, we can directly put the sql mapping files under resources/mapper / in the future.

In addition, SqlSessionFactory and SqlSessionTemplate are configured in the container@ Mapper labeled interfaces are also automatically scanned. Moreover, because the bottom layer uses our default data source, we do not need to change the Druid configuration. He knows how to take it and how to use it.

The official also recommends that we use @ MapperScan annotation to directly scan our Mapper package, so we don't need to add @ Mapper annotation to each Mapper interface.

@MapperScan("com.example.boot.mapper")
@SpringBootApplication
public class SpringBootMysqlApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMysqlApplication.class,args);
    }
}

By the way, let's use the official database table:

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT 'Primary key ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT 'full name',
	age INT(11) NULL DEFAULT NULL COMMENT 'Age',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT 'mailbox',
	PRIMARY KEY (id)
);
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

Then we create a new User class.

package com.example.boot.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

Then we write the Mapper interface, inherit BaseMapper, and the generics in it write the beans we operate. Then MyBatisPlus will help us automatically write many common and simple addition, deletion, modification and query methods!!!

package com.example.boot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.boot.bean.User;

public interface UserMapper extends BaseMapper<User> {
    
}

Then we can test our method directly in the controller method.

package com.example.boot.controller;

import com.example.boot.bean.User;
import com.example.boot.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class UserController {

    @Resource
    UserMapper userMapper;

    @GetMapping("/user/{id}")
    public User findOneById(@PathVariable("id") Integer id){
        User user = userMapper.selectById(id);
        return user;
    }
}

Let's look at the results:


By the way, expand the addition, deletion and modification.
Write a page first:
index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>MP Addition, deletion and modification of</title>
</head>
<body>
    Test get user request:
    <form action="/user/1" method="get">
        <input value="Submit" type="submit">
    </form>
    <br>
    Test add user request:
    <form action="/user" method="post">
        <input value="Submit" type="submit">
    </form>
    <br>
    Test modify user request:
    <form action="/user" method="post">
        <input type="hidden" name="_method" value="put">
        <input value="Submit" type="submit">
    </form>
    <br>
    Test delete user request:
    <form action="/user" method="post">
        <input type="hidden" name="_method" value="delete">
        <input value="Submit" type="submit">
    </form>
    <br>
</body>
</html>

Then write our controller method.

    @PostMapping("/user")
    public String insertUser(){
        User user = new User(null,"Mick",20,"123@163.com");
        userMapper.insert(user);
        return "Insert successful";
    }

    @PutMapping("/user")
    public String updateUser(){
        User user = new User(1L,"Jack",18,"10086@qq.com");
        userMapper.updateById(user);
        return "Modified successfully";
    }

    @DeleteMapping("/user")
    public String deleteUser(){
        userMapper.deleteById(1L);
        return "Delete succeeded";
    }
}

result:

Click the above four submit buttons one by one, and the results are as follows:
1. Check

2. Increase

3. Change

4. Delete

Tags: Java Spring Spring Boot mybatis-plus

Posted on Sat, 18 Sep 2021 22:34:12 -0400 by ngoweb