Spring Boot learning the basics (b)

This article is introduced for spring boot in very shallow knowledge, do not understand, just contact surface, some of the more complex content is not too much description. The text of the error, please keep, thank you ~

In the previous article Spring Boot learn the basics (a) , the only uses only spring boot, but the actual development process, not only the spring boot, will be integrated mybatis, junit and other frameworks, be here this a brief introdction.

A, spring boot integrate simple to use other frameworks

(1) New spring initializr project

Here Insert Picture Description
Here Insert Picture Description
The choice depends (after or may not choose to create a good project, they can go inside to manually create the pom.xml file)
Here Insert Picture Description
Here Insert Picture Description

(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>

Mybatis persistence framework can be found and the frame junit test has been introduced.

(3) spring boot configuration information (this being only comprises database information).

Adding an amount of data in the connection information 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 implement test database already exists on the local, containing a user table user ..
Here Insert Picture Description

(4) Create a user entity classes and corresponding mapper

User class

package com.example.springboot_study_demo2.entity;

public class User {
    // 主键
    private Long id;
    // 用户名
    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 + '\'' +
                '}';
    }
}

User class corresponding mapper

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 {

    /**
     * 通过用户名查询用户
     * @return
     */
    User findByUsername(String username);

}

Note here @Mapper comment, be sure not to forget, or be wrong.

(5) Mapper mapping configuration file

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

(6) adding the information mybatis in application.properties
#别名扫描包
mybatis.type-aliases-package=com.example.springboot_study_demo2.entity
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mappers/*Mapper.xml
(7) the spring boot authentication and junit mybatis

When creating a new project automatically creates atestDirectory, the directory and the main test is the corresponding directories, specifically for testing purposes. IDEA also automatically generates a test class of the entire file, we will write the code in this class inside

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() {
    }

    //测试 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. @RunWith (SpringRunner.class), @ SpringBootTest code appears, @ Autowired, @ Test annotation is very important to keep in mind; additional knowledge on notes not in this expansion.
The end result should be output as the username of the user objects xiaoming
Here Insert Picture Description

(8) follows the structure of the entire project

Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description
2020.03.07

Published 52 original articles · won praise 59 · views 6806

Guess you like

Origin blog.csdn.net/ataraxy_/article/details/104710485