JAVA-10-[SpringBoot] Integrate JUnit and MyBatis

SpringBoot test fails with error: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration

1 Integrate JUnit

一、src/main/java下创建包com.example
包下创建启动类MyApplication
二、src/test/java下创建包com.example
包下创建测试类MyApplicationTests

1.1 pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

1.2 Basic application

1.2.1 Start class MyApplication.java

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

1.2.2 Test class MyApplicationTests

package com.example;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class MyApplicationTests {
    
    
    @Test
    void contextLoads(){
    
    
        System.out.println("afsf");
    }
}

1.3 Test object

1、注入要测试的对象@Autowired
2、执行要测试的对象对应的方法

2 Integrate MyBatis

Core configuration: database connection related information (connect what? Who? What permissions?)
Mapping configuration: SQL mapping (XML/annotation)

2.1 pom.xml

1. Import the corresponding starter
2. Configure related information

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

insert image description here

2.2 application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: bigdata

2.3 Database

一、数据库test
二、建表语句
CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
);
三、插入数据
id,username,password
1,lily,1111
2,lucy,2222
3,lilei,3333

2.4 Code files

2.4.1 entity class User

package com.example.domain;

public class User {
    
    
    private Integer id;
    private String username;
    private String password;

    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer id) {
    
    
        this.id = id;
    }

    public String getUsername() {
    
    
        return username;
    }

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

    public String getPassword() {
    
    
        return password;
    }

    public void setPassword(String password) {
    
    
        this.password = password;
    }
    //有参构造方法
    public User(Integer id,String username,String password) {
    
    
        this.id=id;
        this.username=username;
        this.password=password;
    }
    // 无参构造方法
    public User() {
    
    

    }

    @Override
    public String toString() {
    
    
        return "User{id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

2.4.2 Interface class UserDao

package com.example.dao;

import com.example.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserDao {
    
    
    //查询全部数据
    @Select("select * from user")
    public List<User> findAll();
}


2.4.3 Start class MyApplication

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

2.4.4 Test class MyApplicationTests

package com.example;
import com.example.dao.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class MyApplicationTests {
    
    
    @Autowired
    private UserDao userDao;
    @Test
    void contextLoads(){
    
    
        System.out.println(userDao.findAll());
    }
}

Guess you like

Origin blog.csdn.net/qq_20466211/article/details/129969969