SpringBoot entry notes

Keywords: Spring Mybatis SpringBoot MySQL

Article directory

Spring boot and microservices

SpringBoot

Java enterprise application process - > J2EE - > spring - > SpringBoot, which is a process of constantly simplifying development. Due to the development of spring, there are various integration files in the project, which leads us to write a lot of spring configuration code. Spring boot is the development framework abstracted out under such a background, in order to make it easier for everyone to use spring and integrate various commonly used middleware and open-source software.
    Spring Boot is developed based on spring. Spring Boot does not provide the core features and extension functions of spring framework itself, but is only used for rapid and agile development of new generation of applications based on spring framework. That is to say, it is not a solution to replace spring, but a tool to enhance spring developer experience in close combination with spring framework.

Micro service

                                 .
Microservice is a kind of architecture style. It is an application of distributed cluster, which divides different tasks into different servers.
Microservice paper

Spring boot environment construction

Official document of SpringBoot
SpringBoot unofficial Chinese document

Official document generation

At the bottom of the spring boot page on the official website is:

Click Spring Initializr to enter the configuration page. After configuration, select generate Ctrl + enter to download the spring boot initial session file.

idea integration

The process of selecting Spring Initializr configuration when creating a new project is similar to the above, which will not be covered in detail.

Introduction to the initial environment

  • The business package should be built under the same package as the @ SpringBootApplication decoration class. Errors will be reported elsewhere.
  • Banner generation: create a new banner.txt file in the resources folder, and paste the customized banner into it banner online generation

Spring boot principle

Automatic configuration

  • When opening the pom file, we will find that our pom file already has a parent dependency, and the core dependencies are all in spring boot dependencies.
  • The pom parent class specifies the configuration file format that overrides the spring boot default configuration.

Starter:

<!-- web Project launcher -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--Hot deployment configuration-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

Note: reason for failure of hot deployment of IDEA for SpringBoot
This is not a hot deployment configuration problem. The root cause is that Intellij IEDA does not automatically compile by default. It needs to set the IDEA to automatically compile: idea hot deployment configuration

YML file

grammar

  1. Normal object or map
person:
    name: tdte
    age: 30

# Inline representation
person: {name: tdte, age: 20}

  1. array
city:
    - beijing
    - shanghai
    - guangzhou
    - shenzhen

# Inline representation
city: [beijing,shanghai,guangzhou,shenzhen]

# Array objects
student:
  - name: zhangsan
    age: 18
    score: 100
  - name: lisi
    age: 28
    score: 88
  - name: wangwu
    age: 38
    score: 90
# Inline representation
student: [{name: tom,age: 18,addr: beijing},{name: lucy,age: 17, addr: tianjin}]

Property mapping between configuration files and configuration classes

Use annotation @ Value mapping

We can map the values in the configuration file to the fields of a Spring managed Bean through the @ Value annotation

For example:

The configuration of application.properties is as follows:

person:
  name: zhangsan
  age: 18

Alternatively, application.yml is configured as follows:

person:
  name: zhangsan
  age: 18

The entity Bean code is as follows:

@Controller
public class QuickStartController {

    @Value("${person.name}")
    private String name;
    @Value("${person.age}")
    private Integer age;

    @RequestMapping("/quick")
    @ResponseBody
    public String quick(){
        return "springboot Successful visit! name="+name+",age="+age;
    }
}

Use annotation @ ConfigurationProperties mapping

By annotating @ ConfigurationProperties(prefix = prefix of key in configuration file), the configuration in configuration file can be automatically mapped with entity

The configuration of application.properties is as follows:

person:
  name: zhangsan
  age: 18

Alternatively, application.yml is configured as follows:

person:
  name: zhangsan
  age: 18

The entity Bean code is as follows:

@Controller
// Object person prefix of yaml
@ConfigurationProperties(prefix = "person")
public class QuickStartController {
    // Auto assemble according to key name
    private String name;
    private Integer age;

    @RequestMapping("/quick")
    @ResponseBody
    public String quick(){
        return "springboot Successful visit! name="+name+",age="+age;
    }

    // The precondition of assembly is to set the get/set method to the property, otherwise the value obtained is null
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

Actuator:

<!-- Actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

SpringBoot integration

SpringBoot integrates Mybatis

  • Note that unsatisfactory state exception will occur in SpringBoot2.2.5
  1. rely on
<!-- mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

<!--mysql drive-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
  1. Database configuration:
    application.properties
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://47.94.145.43:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=311776


#spring integrates with Mybatis environment
#pojo alias scanning package
mybatis.type-aliases-package=top.tdte.pojo
#Load Mybatis map file
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
  1. Writing Mapper
@Mapper
@Repository
public interface UserMapper {
	public List<User> queryUserList();
}

Note: @ mapper marks that this class is a mapper interface of mybatis, which can be automatically scanned into the spring context by spring boot

  1. Configure Mapper mapping file

Add the UserMapper.xml configuration file under the src\main\resources\mapper path“

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="top.tdte.mapper.UserMapper">
    <select id="queryUserList" resultType="user">
        select * from user
    </select>
</mapper>

Integrated unit test

Start dependence

<!--Test start dependency-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

Write the test class under the test package:

//The runner selects spring's
@RunWith(SpringRunner.class)
//Note here that the initiator class of the non test class is selected
@SpringBootTest(classes = SpringbootMybatisApplication.class)
public class MapperTest {
    // Take the unit we need to test from spring for testing
    @Autowired
    private UserMapper userMapper;

    @Test
    public void test(){
        List<User> users = userMapper.queryUserList();
        System.out.println(users);
    }
}
Published 55 original articles, won praise 22, visited 3052
Private letter follow

Posted by karikamiya on Sat, 07 Mar 2020 20:02:37 -0800