【加精】Spring全家桶系列–【SpringBoot入门到跑路】

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> 【加精】Spring全家桶系列–【SpringBoot入门到跑路】

//本文作者:cuifuan

//本文将收录到菜单栏:《Spring全家桶》专栏中

对于之前的Spring框架的使用,各种配置文件XML、properties一旦出错之后错误难寻,这也是为什么SpringBoot被推上主流的原因,SpringBoot的配置简单,说5分钟能从框架的搭建到运行也不为过,现在更是微服务当道,所以在此总结下SpringBoot的一些知识,新手教程。

1.在官网快速创建SpringBoot项目

Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具,它使用一种基于Groovy语言来声明项目设置.也就是和Maven差不多的项目构建工具,为何要使用Gradle,举例:

maven要引入依赖 pom.xml

1234567891011
!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -- dependency    groupIdorg.springframework.boot/groupId    artifactIdspring-boot-starter-web/artifactId    version1.5.15.RELEASE/version /dependency

!– https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web

dependency

   groupIdorg.springframework.boot/groupId

   artifactIdspring-boot-starter-web/artifactId

   version1.5.15.RELEASE/version

/dependency

而Gradle引入 build.gradle

1
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.15.RELEASE'

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/ 生成一个初始项目

Spring全家桶系列--[SpringBoot入门到跑路]

这里会下载一个zip的项目压缩包

2. 使用Gradle导入SpringBoot项目

demo.zip解压之后记得复制下demo文件夹放的路径

在此用的开发工具是IntelliJ IDEA

下面是导入流程:

IDEA里点击File - Open - 粘贴刚刚的demo文件夹路径 - 找到build.gradle双击

  • Open as Peoject - 等待Gradle加载完就好,看不明白看下图

Spring全家桶系列--[SpringBoot入门到跑路] Spring全家桶系列--[SpringBoot入门到跑路]

打开之后Gradle加载下载的特别慢,要换成国内源,打开build.gradle配置文件用下面的替换

build.gradle

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
/** 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' }

/** 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’

}

Spring全家桶系列--[SpringBoot入门到跑路]

3. SpringBoot项目启动

启动前准备,依据下图把 DemoApplication 启动类移到 demo 文件夹的同级;

启动类相当于管理项目的负责人,你把他扔到与控制层同级肯定出错不是;

然后把demo包改名为controller并新建TestController类

Spring全家桶系列--[SpringBoot入门到跑路]

TestController.java

12345678910111213141516171819202122232425262728293031323334353637
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";    } }

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”;

   }

}

Spring全家桶系列--[SpringBoot入门到跑路]

启动成功之后访问 http://localhost:8080/hello

Spring全家桶系列--[SpringBoot入门到跑路]

上图成功代表项目可以访问了

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的配置文件,看的更直观,上代码吧还是

1234567891011
# 下述properties spring.resources.locations= classpath:/templates # 改为yml格式之后 spring:  resources:    static-locations: classpath:/templates

下述properties

spring.resources.locations= classpath:/templates

改为yml格式之后

spring:

 resources:

   static-locations: classpath:/templates

yml需要注意,冒号(:)后面要跟空格,第二级和第一级要在上下行用一个Tab的距离

application.yml

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
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

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

5.下期

mapper.xml、dao接口、实体类自动生成

集成一个很nice的开发模板

CRUD操作以及集成PageHelper分页

AOP全局的异常进行处理

 

点击图片加入Spring交流群

↓↓↓

看完本文有收获?请转发分享给更多人

Spring全家桶系列--[SpringBoot入门到跑路]

 

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> 【加精】Spring全家桶系列–【SpringBoot入门到跑路】


 上一篇
Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解) Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解)
本文为原创投稿文,作者:微笑面对生活 RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。 我之前的HTTP开发是用a
下一篇 
Springboot webSocket(一) Springboot webSocket(一)
在构建Spring boot项目时已经提供webSocket依赖的勾选。webSocket是TCP之上的一个非常薄的轻量级层 ,webSocket主要的应用场景离不开即时通讯与消息推送,但只要应用程序需要在浏览器和服务器之间来回发送消息,就