Spring boot integrates Mybatis (annotation only development version)

Keywords: Java Spring Boot Back-end


In fact, the process is not complicated. I just write such a thing to continue to practice some use of springboot and mybatis, just for review.


The first step is to create a new Module maven project. Here, we use Spring Initializr to create springboot applications quickly:

Next, you can create it step by step according to your own needs. No, there is another article in my column on learning the Springboot framework, which has been introduced. I won't repeat it here (because it's really simple).
Because we are integrating mybatis, we need to introduce the following package dependencies (circled in red in the figure):

The top-down dependencies are:

It is used to simplify the writing of JavaBean s, that is, we can no longer write the corresponding get, set methods and toString construction methods.


This is well understood. It is our spring MVC framework for Java Web.

Mybatis Framework:

This is the Mybatis framework, and we integrate it.

MySQL Driver:

Because the database uses MySQL, we need this driver.

Then create the project. The project directory is as follows:

When you open the pom file, you can see that all the scene initiators we checked earlier have:

Now let's prepare a database springdb and create a user table:

Then we configure our database in the application file under the resources directory:

# Configuration database
    url: jdbc:mysql://localhost:3306/springdb
    username: root
    password: 121314
    driver-class-name: com.mysql.jdbc.Driver

Then write our entity class and put it under the entity package:

package com.haha.demo.entity;

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

public class User {
    private int id;
    private String name;
    private String email;
    private int age;

Write the mapper file under the mapper package:

package com.haha.demo.mapper;

import com.haha.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {
    //Query all methods
    @Select("select * from user")
    public List<User> findAll();

Mapper layer, that is, the data layer, writes corresponding SQL statements and gives the interface for processing data.
Then write the business layer service:

package com.haha.demo.service;

import com.haha.demo.entity.User;
import com.haha.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

public class UserService {

    //Injection properties
    UserMapper userMapper;

    public List<User> findAll(){
        List<User> list = userMapper.findAll();
        return list;


The service layer is the business layer. The business layer uses the data layer method to process the business logic, that is, the implementation of the data layer interface method.
Then write to the control layer controller:

package com.haha.demo.controller;

import com.haha.demo.entity.User;
import com.haha.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

public class UserController {
    UserService userService;

    //Query method
    public List<User> findAll(){
        return userService.findAll();

Start the project, access port:

You can see that the query succeeds, and the backend returns the corresponding JSON string.

Posted by sanch on Sun, 31 Oct 2021 23:12:43 -0700