SpringBoot basic learning 6 -- integrating third-party data

Catalogue of series articles

1, SpringBoot basic learning 1 - introduction case (idea online version)

2, SpringBoot basic learning 2 - introduction case (official website creation version)

3, SpringBoot basic learning 3 - introductory case (alicloud version)

4, SpringBoot basic learning 4 - REST development related knowledge

5, SpringBoot basic learning 5 - basic configuration

preface

Tip: Here you can add the general contents to be recorded in this article:

For example, with the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This paper introduces the basic content of machine learning.

Tip: the following is the main content of this article. The following cases can be used for reference

1, Spring boot integrates Junit

1.1 create a new module





1.2 modify POM XML file

1.3 testing


(1) Code

package com.yan;

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

@SpringBootTest
class Springboot04JunitApplicationTests {

    @Test
    void contextLoads() {
        System.out.println("test...");
    }

}

(2) Operation results

1.4 test cases

(1) Directory structure

(2) Code
①BookDao.java

package com.yan.dao;

public interface BookDao {

    public void save();
}

②BookDaoImpl.java

package com.yan.dao.impl;

import com.yan.dao.BookDao;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("book dao is running...");
    }
}

③ Test class springboot04junitapplicationtests java

package com.yan;

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

@SpringBootTest
class Springboot04JunitApplicationTests {

    //1. Inject the object you want to test
    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        //System.out.println("test...");

        //2. Execute the method corresponding to the object to be tested
        bookDao.save();
    }

}

(3) Operation results

1.5 @SpringBootTest

(1) Name: @ SpringBootTest
(2) Type: test class annotation
(3) Upper class definition: test position
(4) Function: set the SpringBoot startup class loaded by Junit
(5) Related properties: classes - sets the SpringBoot startup class

1.6 precautions

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

  • If the test class exists in the package or sub package of the boot class, there is no need to specify the boot class
  • If the test class does not exist in the package or sub package of the boot class, you need to specify the boot class through the classes attribute
@SpringBootTest(classes = Springboot04JunitApplication.class)

2, Integrate Mybatis

2.1

(1) Core configuration: database connection related information
(2) Mapping configuration: SQL mapping (XML / annotation)

2.2 create a new module, select Spring initialization, and configure relevant basic information





Select the technology set (MyBatis, MySQL) that the current module needs to use

Project directory structure

Apply Change properties to application yml

2.3 create database and related data tables


2.4 configure application YML, set data source parameters

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springbootdb?serverTimezone=UTC
    username: root
    password: root

2.5 writing entity classes

package com.yan.domain;

public class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;

    public Integer getId() {
        return id;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Book{");
        sb.append("id=").append(id);
        sb.append(", type='").append(type).append('\'');
        sb.append(", name='").append(name).append('\'');
        sb.append(", description='").append(description).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

2.6 define data layer interface and mapping configuration

package com.yan.dao;

import com.yan.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

//Database SQL mapping needs to add @ Mapper, which is recognized by the container
@Mapper
public interface BookDao {

    @Select("select * from tb_book where id = #{id}")
    public Book getById(Integer id);
}

2.7 inject dao interface into the test class to test functions

package com.yan;

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

@SpringBootTest
class Springboot05MybatisApplicationTests {

    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        System.out.println(bookDao.getById(1));
    }

}

2.8 operation results


Project directory structure

2.9 SpringBoot integration MyBatis FAQ handling

(1)MYSQL 8. The X-drive forces the time zone to be set

  • Modify the url and add the serverTimezone setting
  • Modify MYSQL database configuration

(2) The driver class is out of date, and the reminder is replaced with com mysql. cj. jdbc. Driver

3, SpringBoot integrates mybatis plus

3.1 differences between MyBatis plus and MyBatis

  • Import coordinates are different
  • Data layer implementation simplification

3.2 creating a new module




3.3 manually add mybatis plus coordinates to POM XML file

(1) Go maven warehouse Query MyBatis plus

<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

Note: since the coordinate Version of mybatis plus is not included in SpringBoot, you need to specify the corresponding Version

(2)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yan</groupId>
    <artifactId>springboot_06_MyBatisPlus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_06_MyBatisPlus</name>
    <description>springboot_06_MyBatisPlus</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>-->

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.4 project directory structure

3.5 application.yml

# 2. Configuration related information
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springbootdb?serverTimezone=UTC
    username: root
    password: root

# Set MP related configuration
mybatis-plus:
  global-config:
    db-config:
      table-prefix: tb_

Or add @ TableName("") table name annotation on the entity class

3.6 entity Book

package com.yan.domain;

public class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;

    public Integer getId() {
        return id;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Book{");
        sb.append("id=").append(id);
        sb.append(", type='").append(type).append('\'');
        sb.append(", name='").append(name).append('\'');
        sb.append(", description='").append(description).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

3.7 interface file of Dao layer BookDao

package com.yan.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yan.domain.Book;
import org.apache.ibatis.annotations.Mapper;

//Database SQL mapping needs to add @ Mapper, which is recognized by the container
@Mapper
public interface BookDao extends BaseMapper<Book> {
}

3.8 testing

package com.yan;

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

@SpringBootTest
class Springboot06MyBatisPlusApplicationTests {

    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        System.out.println(bookDao.selectById(1));
    }

}

3.9 operation results

4, Spring boot integrates Druid

4.1 create a new module


4.2 manually import Druid coordinates

<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.8</version>
</dependency>

4.3 spring boot_ 05_ Book, BookDao and application in mybatis project Copy YML package to springboot_07_druid project

4.4 configuring druid

# 2. Configuration related information

# To configure druid, method 1:
#spring:
#  datasource:
#    driver-class-name: com.mysql.cj.jdbc.Driver
#    url: jdbc:mysql://localhost:3306/springbootdb?serverTimezone=UTC
#    username: root
#    password: root
#    type: com.alibaba.druid.pool.DruidDataSource

# Configure druid, method 2:
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/springbootdb?serverTimezone=UTC
      username: root
      password: root

4.3 testing

(1) Code

package com.yan;

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

@SpringBootTest
class Springboot07DruidApplicationTests {

    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        System.out.println(bookDao.getById(1));
    }

}

(2) Operation results

Tags: Java Spring Boot

Posted by skatermike21988 on Mon, 18 Apr 2022 07:39:12 +0930