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
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

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;
    }

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

@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
mybatis.type-aliases-package=com.example.springboot_study_demo2.entity
#Load Mybatis map file
mybatis.mapper-locations=classpath:mappers/*Mapper.xml
(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;

@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootStudyDemo2ApplicationTests {


    @Autowired
    private UserMapper userMapper;

    @Test
    void contextLoads() {
    }

    //Test mybatis junit
    @Test
    public void test(){
        User user=userMapper.findByUsername("xiaoming");
        System.out.println(user);
    }

}


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



2020.03.07

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