Spring全家桶系列–[SpringBoot入门到跑路]
对于之前的Spring框架的使用,各种配置文件XML、properties一旦出错之后错误难寻,这也是为什么SpringBoot被推上主流的原因,SpringBoot的配置简单,说5分钟能从框架的搭建到运行也不为过,现在更是微服务当道,所以在此总结下SpringBoot的一些知识,新手教程。
1.在官网快速创建SpringBoot项目
Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具,它使用一种基于Groovy语言来声明项目设置.也就是和Maven差不多的项目构建工具,为何要使用Gradle,举例:
maven要引入依赖 pom.xml
1 2 3 4 5 6 7 8 9 10 11 |
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.15.RELEASE</version> </dependency> |
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.15.RELEASE</version>
</dependency>
而Gradle引入 build.gradle
1 |
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.15.RELEASE' |
很清晰明了,依赖管理比maven强,脚本编写比Ant好用,Google都在使用了,赶紧上手吧!
Gradle本地安装教程
windows :
https://www.cnblogs.com/linkstar/p/7899191.html
Mac_OS :
https://www.jianshu.com/p/e9d035f30876
下面开始进入正题:
进入 https://start.spring.io/ 生成一个初始项目
这里会下载一个zip的项目压缩包
2. 使用Gradle导入SpringBoot项目
demo.zip解压之后记得复制下demo文件夹放的路径
在此用的开发工具是IntelliJ IDEA
下面是导入流程:
IDEA里点击File -> Open -> 粘贴刚刚的demo文件夹路径 -> 找到build.gradle双击
-> Open as Peoject -> 等待Gradle加载完就好,看不明白看下图
打开之后Gradle加载下载的特别慢,要换成国内源,打开build.gradle配置文件用下面的替换
build.gradle
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
/** buildscript中的声明是gradle脚本自身需要使用的资源。 * 可以声明的资源包括依赖项、第三方插件、maven仓库地址等 */ buildscript { ext { springBootVersion = '1.5.6.RELEASE' } repositories { //使用国内源下载依赖 maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } // 应用Java插件 apply plugin: 'java' //让工程支持IDEA的导入 apply plugin: 'idea' apply plugin: 'org.springframework.boot' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 //build.gradle文件中直接声明的依赖项、仓库地址等信息是项目自身需要的资源。 repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } /** * 在gradle里,对依赖的定义有6种 * compile, runtime, testCompile, testRuntime, providedCompile,providedRuntime * compile:需要引用这个库才能进行编译工作 * testRuntime : 测试依赖范围 * 其他的了解:http://shmilyaw-hotmail-com.iteye.com/blog/2345439 */ dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') compile 'com.alibaba:druid:1.0.29' } |
* 可以声明的资源包括依赖项、第三方插件、maven仓库地址等
*/
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
//使用国内源下载依赖
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// 应用Java插件
apply plugin: 'java'
//让工程支持IDEA的导入
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
//build.gradle文件中直接声明的依赖项、仓库地址等信息是项目自身需要的资源。
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
/**
* 在gradle里,对依赖的定义有6种
* compile, runtime, testCompile, testRuntime, providedCompile,providedRuntime
* compile:需要引用这个库才能进行编译工作
* testRuntime : 测试依赖范围
* 其他的了解:http://shmilyaw-hotmail-com.iteye.com/blog/2345439
*/
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile 'com.alibaba:druid:1.0.29'
}
3. SpringBoot项目启动
启动前准备,依据下图把 DemoApplication 启动类移到 demo 文件夹的同级;
启动类相当于管理项目的负责人,你把他扔到与控制层同级肯定出错不是;
然后把demo包改名为controller并新建TestController类
TestController.java
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 |
package com.example.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 这里的@RestController相当于 @ResponseBody+@Controller * 使用@RestController 相当于使每个方法都加上了 @ResponseBody 注解 * created by cfa 2018-11-06 下午 11:30 **/ @RestController public class TestController { /** * 这里的@GetMapping相当于@RequestMapping(value = "/hello", method = RequestMethod.GET) * created by cfa 2018-11-06 下午 11:29 **/ @GetMapping("hello") public String test(){ return "i love java"; } } |
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 这里的@RestController相当于 @ResponseBody+@Controller
* 使用@RestController 相当于使每个方法都加上了 @ResponseBody 注解
* created by cfa 2018-11-06 下午 11:30
**/
@RestController
public class TestController {
/**
* 这里的@GetMapping相当于@RequestMapping(value = "/hello", method = RequestMethod.GET)
* created by cfa 2018-11-06 下午 11:29
**/
@GetMapping("hello")
public String test(){
return "i love java";
}
}
启动成功之后访问 http://localhost:8080/hello
上图成功代表项目可以访问了
4.配置application.yml
什么是yml?
YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的, 可以被支持YAML库的不同的编程语言程序导入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。引自:https://www.cnblogs.com/hanson1/p/7105248.html
听不懂吧,其实我也看不明白
就是相当于xml,properties的配置文件,看的更直观,上代码吧还是
1 2 3 4 5 6 7 8 9 10 11 |
# 下述properties spring.resources.locations= classpath:/templates # 改为yml格式之后 spring: resources: static-locations: classpath:/templates |
spring.resources.locations= classpath:/templates
# 改为yml格式之后
spring:
resources:
static-locations: classpath:/templates
yml需要注意,冒号(:)后面要跟空格,第二级和第一级要在上下行用一个Tab的距离
application.yml
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 55 |
server: port: 8080 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/ceilan?characterEncoding=utf-8 username: root password: 123456 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 mvc: view: suffix: .html resources: static-locations: classpath:/templates |
port: 8080
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ceilan?characterEncoding=utf-8
username: root
password: 123456
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
mvc:
view:
suffix: .html
resources:
static-locations: classpath:/templates