SpringBoot integrates JUnit, Mybatis, Mybatis plus and Druid

Keywords: Java Apache Spring Spring Boot

Integrate JUnit

@SpringBootTest defines that Springboot04JunitApplicationTests is a Test class that supports injecting objects. Once injected, this object can participate in @ Test.

Name: @ SpringBootTest
Type: test class annotation
Location: above the test class definition
Function: set the spring boot startup class loaded by JUnit
example:

@SpringBootTest
class Springboot04JunitApplicationTests
Summary:

1. The starter corresponding to the import test is available when the project is created
2. The test class is decorated with @ SpringBootTest
3. Add the object to be tested in the form of automatic assembly

classes property

When we change the location of the test class to the upper directory, the test class is unavailable.
If the current test class is in the package or sub package where the boot class of the project is located, it can be ignored. However, if the location of the test class and the boot class does not meet the above requirements, the test class will not get the spring container, and the classes attribute needs to be introduced

package com.zg;

import com.zg.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest(classes = Springboot04JunitApplication.class)
//@ContextConfiguration(classes = Springboot04JunitApplication.class)
//The above two methods can help the test class get to the spring container
class Springboot04JunitApplicationTests {
    //1. Inject the object to be tested
    @Autowired//First, assemble it automatically and inject bookdao into it
    private BookDao bookDao;


    @Test
    void contextLoads() {
        //2. Execute the code corresponding to the object to be tested
        bookDao.save();

    }

}

If the test class is in the package or sub package of the SpringBoot boot class, you can omit the setting of the boot class, that is, omit the setting of classes

Integrate MyBatis

Core configuration: database connection related information (connected with what? Connected with who? With what permissions)
Mapping configuration: SQL mapping (XML / annotation)

SpringBoot integrates MyBatis

1. Create a new module, select Spring initialization, and configure the basic information related to the module
2. Select the technology set (MyBatis, MySQL) that the current module needs to use

3. Set data source parameters
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mp
    username: root
    password: root

When the SpringBoot version is lower than 2.4.3 (not included) and the Mysql driver version is greater than 8.o, you need to configure the time zone JDBC: mysq1: / / localhost: 3306 / SSM in the url connection string_ db?serverTimezone=UTC
Or configure the time zone on the MySQL database side to solve this problem

4. Define data layer interface and mapping configuration
package com.zg.dao;

import com.zg.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Mapper
public interface UserDao {
    @Select("select * from tb_user where id=#{id}")
    public User getById(Long id);

}

5. Define pojo
package com.zg.domain;

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

@Data
@NoArgsConstructor
@AllArgsConstructor

public class User {

    private Long id;
    private String user_name;
    private String password;
    private String name;
    private Integer age;
    private String email;

}

6. Test in test class
package com.zg;

import com.zg.dao.BookDao;
import com.zg.dao.UserDao;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

class Springboot05MybatisApplicationTests {

    @Autowired
    private UserDao userDao;

    @Test
    void contextLoads() {
        System.out.println(userDao.getById(2L));
    }

}

Integrate mybatis plus

What is the difference between MyBatis plus and MyBatis
Import coordinates are different
Data layer implementation simplification

SpringBoot integrates mybatis plus

1. Manually add the coordinates of spring boot integration mybatis plus, which can be obtained through mvnrepository
<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>

Since the coordinate version of mybatis plus is not included in SpringBoot, you need to specify the corresponding version

2. Define the data layer interface and mapping configuration, and inherit BaseMapper
package com.zg.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zg.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Mapper
public interface UserDao extends BaseMapper<User> {

}

3. Set data source parameters
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mp
    username: root
    password: root
4. Define pojo
package com.zg.domain;

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

@Data
@NoArgsConstructor
@AllArgsConstructor

public class User {

    private Long id;
    private String user_name;
    private String password;
    private String name;
    private Integer age;
    private String email;

}

5. Test in test class
package com.zg;

import com.zg.dao.BookDao;
import com.zg.dao.UserDao;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

class Springboot05MybatisApplicationTests {

    @Autowired
    private UserDao userDao;

    @Test
    void contextLoads() {
        System.out.println(userDao.getById(2L));
    }

}

Integrate Druid

Import the starter corresponding to druid

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.8</version>
</dependency>

specify data source

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mp
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
You can also change the druid configuration
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/mp
      username: root
      password: root 

Posted by scialom on Mon, 29 Nov 2021 03:56:03 -0800