Spring Boot basic knowledge learning

This paper mainly introduces the very simple knowledge of spring boot, not in-depth understanding, only touching the surface, but also describing some more complex content. If there are any mistakes in the article, please let me know. Thank you~

In the last article Spring Boot basic knowledge learning (1) In, we only use spring boot, but in the actual development process, we will not only use spring boot, but also integrate mybatis, junit and other frameworks. Here is a brief introduction.

1, Simple use of spring boot to integrate other frameworks

(1) New spring initializr project

Select dependency (or no choice. After the project is created, you can manually create it in pom.xml file)

(2) View pom.xml file


You can find that the persistence framework mybatis and the test framework junit have been imported.

(3) Configure spring boot information (only database information is included for the time being).

Add connection information of data amount in application.properties


Note that the test database already exists on the local implementation, including a user table user.

(4) Create user entity class and corresponding mapper

Class User

package com.example.springboot_study_demo2.entity;

public class User {
    // Primary key
    private Long id;
    // User name
    private String username;

    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    public String getUsername() {
        return username;

    public void setUsername(String username) {
        this.username = username;

    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +

mapper class corresponding to User

package com.example.springboot_study_demo2.mapper;

import com.example.springboot_study_demo2.entity.User;
import org.apache.ibatis.annotations.Mapper;

public interface UserMapper {

     * Query users by user name
     * @return
    User findByUsername(String username);


Pay attention to the @ Mapper annotation here. Don't forget it, or you will make mistakes.

(5) Configure Mapper mapping file

Create a new mappers directory in the src\main\resources \ directory, and then create a new UserMapper.xml configuration file (note the path)

(6) Add information of mybatis to application.properties
#Alias scan package
#Load Mybatis map file
(7) Verify mybatis and junit in spring boot

When you create a new project, you automatically create a test directory, which corresponds to the main directory and is specially used for testing. IDEA also automatically generates the test class of the whole file, in which we will write the corresponding code

package com.example.springboot_study_demo2;

import com.example.springboot_study_demo2.entity.User;
import com.example.springboot_study_demo2.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

class SpringbootStudyDemo2ApplicationTests {

    private UserMapper userMapper;

    void contextLoads() {

    //Test mybatis junit
    public void test(){
        User user=userMapper.findByUsername("xiaoming");


Note the name of the project and the path of each class. The @ RunWith(SpringRunner.class), @ SpringBootTest, @ Autowired, @ Test annotations that appear in the code are very important. Remember that the knowledge about annotations is not expanded here.
The final result should output the user object whose username is xiaoming

(8) The structure of the whole project is as follows


Published 52 original articles, won praise 59, visited 6765
Private letter follow

Tags: Spring Mybatis Junit xml

Posted on Sat, 07 Mar 2020 03:49:03 -0800 by elgordo1960