SpringBoot学习

Spring Boot

1.SpringBoot是什么

maven的集合,简化Sping环境的配置、开发、部署、监控

  1. 全局——修改Maven中的JDK配置使用1.8

maven的setting.xml

1
2
3
4
5
6
7
8
9
10
11
12
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>

2.HelloSpringBoot

  1. 创建parent模块

  2. 创建自模块

    1. 在parent模块中pom对SpringBoot做版本控制
    2. 本模块中引入SpringBoot
  3. 创建Controller

  4. 创建启动类

    1. SpringBootApplication 标记启动类
    2. main方法中编写SpringApplication.run(启动类.class)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.zhangxing.springboot;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import javax.swing.*;

    /**
    * @author zhangxing
    * @date 2019/3/20
    */
    @SpringBootApplication
    public class ApplicationInit {

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

    }

3.SpringBoot原理

==SpringBoot会扫描启动类所在包下的所有注解(递归扫描),进行环境的加载==

4.SpringBoot的热部署

原理:热部署插件有两个ClassLoader,一个用来加载第三方Jar包,不常改变的,一个用来加载修改的类,重启只重新加载了改变的类

  1. 添加热部署依赖
1
2
3
4
5
6
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>true</scope>
<optional>true</optional>
</dependency>
  1. 添加build插件
1
2
3
4
5
6
7
8
9
10
11
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

5.SpringBoot-Web

创建一个包含web项目的maven模块

返回Json

SpringBoot中返回Json不需要使用@ResponseBody ,而是在控制器使用@RestController 来标记控制器,相当于每一个方法都加了@ResponseBody

视图解析

给SpringBoot添加配置

在resources中添加application.properties配置文件

  1. 在配置文件中配置视图解析器
1
2
3
4
5
6
7
8
9
# SpringMVC的视图解析配置
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
# Tomcat的编码配置
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
  1. 添加maven依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    </dependency>
    <!--springboot的webstarter依赖-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--tomcat支持-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>true</scope>
    <optional>true</optional>
    </dependency>
  2. 添加插件,使用插件启动项目,意为编译JSP成Java

    1
    2
    3
    4
    5
    6
    7
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugins>
    </build>

    ==启动项目需要使用MAVEN插件启动==

6.Jar包发布

见文档

7.War包发布

见文档

8.SpringBoot集成Mybatis

  1. 导入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
    </dependency>
    <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.26</version>
    </dependency>
  1. 配置文件中配置spring.datasource的四大金刚

    1
    2
    3
    4
    5
    6
    spring:
    datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///test
    username: root
    password: 1
  1. 启动类加@MapperScan(“Mapper.java和Mapper.xml的包名”)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package group.yuehui;

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    /**
    * @author zhangxing
    * @date 2019/3/20
    */
    @SpringBootApplication
    @MapperScan("group.yuehui.mapper")
    public class Application {

    public static void main(String[] args) {
    SpringApplication.run(Application.class);
    }

    }
坚持学习发博,请支持我的原创!