2018-8-2关于spring和springboot的初步学习

今天因为偶然的情况学习起了 springboot了解了一些不同

区别:

  1. 内部集成了tomcat
  2. maven文件pom简化
  3. 解决了依赖等的自行配置
  4. 热部署

阅读了http://tengj.top/2017/02/26/springboot1/#Spring-Boot%E7%B2%BE%E8%A6%81 大佬的博客后了解到

关于maven文件pom的简化:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>MySpringHelllo</groupId>
<artifactId>MySringTest</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>LATEST</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.18.RELEASE</version>
<!--<version>LATEST</version>-->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</project>

上面就是基于mysql和springmvc的最简单的数据库添加的例子

如下是springboot的pom页面例子

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.orochi</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>helloworld</name>
<description>Demo project for Spring Boot</description>

<!--这是父级依赖目的是简化查找version的操作该功能主要是通过本地的maven的.m2文件里的设置实现的-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>

可以不难看到导入的pom全部都简化了这个标签
具体实现原因是因为导入了

1
2
3
4
5
6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

这个标签之后除了自定义的java版本那一项其余的均没有使用version了
这个配置名为父级依赖 作用就是在导入后常用的大多数包都会免去version的标签
类似与本地cdn有木有o.o 具体包的maven默认依赖可以在.m2目录下的

.m2\repository\org\springframework\boot\spring-boot-dependencies\2.0.4.RELEASE

这个目录下查看到

此文还没写完持续更新溜ε=ε=ε=┏(゜ロ゜;)┛

1
@SpringBootApplication

这个注解的作用是开启自动配置是SpringBoot的核心注解


1
2
3
@RestController
@ResponseBody
@Controller

这三个注解的作用是注解类里面的方法都以json格式输出
RestController = ResponseBody + Controller

启动SpringBoot的三种方法

  1. main方法启动
  2. 使用命令

    mvn spring-boot:run启动

3.运行mvn package打包 然后 java- jar启动