Springboot learning notes 0.1

Time:2021-1-19

Springboot learning notes

Introduction to spring boot

1、 Springboot comparison

Spring framework

The core features of spring framework are dependency injection (DI) and inversion of control (IOC). If you can reasonably use Di and IOC, you can develop loosely coupled and well extensible applications.

Spring MVC

Spring MVC provides a friendly way to develop web applications. Web applications can be easily developed by using such tools as dispatcher servlet, modelandview and view resolver.

Spring Boot

The biggest disadvantage of spring and spring MVC is that there are a lot of configurations, and these configurations have high similarity in different projects. This leads to repeated configuration, cumbersome and messy!

Spring boot expects to combineAutomatic configurationandstartersTo solve this problem,Make spring and spring MVC easier to use. In addition, spring boot also provides some functions to build applications that can be used in production environment more quickly.

2、 Advantages of spring boot

  • Make configuration simple
  • Make monitoring simple
  • Make deployment simple
  • Make development simple

3、 Comparing spring boot with spring MVC

  • Agile development (integrated framework)
  • Built in tomcat, Java application running
  • Reduce XML configuration, configure file properties

4、 Main features of spring boot

  • Follow the principle of “agreement is better than configuration” to simplify configuration
  • It can be completely separated from the XML configuration file, using annotation configuration and Java config
  • Embedded servlet container, application can be executed with jar package: Java – jar
  • Fast completion of project construction, integration of third-party library, easy to use
  • The starter POM is provided to facilitate package management and simplify package management configuration
  • Naturally integrated with spring cloud, spring boot is the best solution to realize microservice in Java system

5、 Spring boot starter

Spring boot starter is a group ofCollection of dependent third party class libraries

If you want to develop a web application, you can introduce spring boot starter web through package management tools (such as Maven), instead of introducing many dependent class libraries. Spring boot starter web can help you introduce these common class libraries at one time.

1). spring-boot-starter-parent

All spring boot projects use spring boot starter parent as the parent project of the application by default.

org.springframework.boot
  	spring-boot-starter-parent
  	1.5.6.RELEASE

Benefits of inheriting the parent projectJava: unifies java version configuration and some other dependent class library versions. The parent project manages the version for you. It has passed the compatibility test. It’s better than introducing a version at will.

2、 Springboot installation

Here, use idea to quickly build a springboot application. First, use springboot to build a web project

image

Then click next. If the following figure appears, it is a network connection problem. You can consider climbing over the wall.

img

After successful connection, as shown in the figure below

image

Then click next to generate the project. The first generation may be a little slow, and the next creation will be much faster. The generated directory structure is as follows:

image

3、 Running the springboot project

Then change the startup class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringbootApplication {
    //Operation method
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

    @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")
    String index(){

        return "Hello Spring Boot!";

    }
}

Then click on the top right corner to run

image

After running successfully, enter thehttp://localhost:8080/

1580895826821

You can also create a class

img

package com.example.hellow;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class index {
    @RequestMapping(value = "/index")
    @ResponseBody
    public Map index(){
        Map map = new HashMap<>();

        map.put ("Diao Chan", new string [] {"mage", "Assassin"});
        map.put ("Han Xin", new string [] {"soldier", "Assassin"});
        map.put (Liu Bang, new string,] {mage, tank});
        return map;
    }
}

Then modify the running method

package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication

//Find packages created
@ComponentScan(basePackages = "com.example.hellow")
@RestController
public class SpringbootApplication {
    //Operation method
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

    @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")
    String index(){

        return "Hello Spring Boot!";

    }
}

The operation is as followsimg

4、 Building SSM

On the basis of the previous step, we need to add some related configurations to build SSM. The following is the structure diagram:

image

UserController:

@RestController
/**The data returned automatically is in JSON format***/
public class UserController {
    
    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public List list(){
         //Only pagination query is performed on the next select statement, which has no effect on the subsequent select statement
        List list = userService.findAllUser();
        return list;
    }
}

User:

public class User implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer id; 
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    
    
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
    

}

UserMapper:

@Mapper
public interface UserMapper {
    
    public List findAll();

}

UserService:

public interface UserService {
    
    /**
     *Query users by interface
     */
    public List findAllUser();
}

UserServiceImpl:

@Service
public class UserServiceImpl implements UserService {
        @Resource
    private UserMapper userMapper;
        public List findAllUser() {
        List list = userMapper.findAll();
        return list;
    }

}

UserMapper.XML:

select * from user

SqlMapperConfig:

application.properties:

#server.port=80

logging.level.org.springframework=DEBUG
#springboot   mybatis
#jiazai mybatis peizhiwenjian
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mybatis/sqlMapConfig.xml
#mybatis.type-aliases-package = com.demo.bean

#shujuyuan
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root

Pom.XML:

4.0.0

    com.example
    springbootdemo
    0.0.1-SNAPSHOT
    jar

    springbootdemo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.0
        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
            1.0.25
        
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        


        
        
            org.projectlombok
            lombok
        

        
        
            com.github.ulisesbocchio
            jasypt-spring-boot-starter
            1.14
        

        
        
            org.springframework.cloud
            spring-cloud-starter-sleuth
        

        
            org.springframework.cloud
            spring-cloud-sleuth-zipkin
        


        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin

Post project access http://localhost : 8080 / list, you can see the following results (omit the design of SQL table here, you can build the table according to the entity class)
image.png