从零开始学SpringBoot-如何开始使用

目的

    《从零开始学SpringBoot》,是小编打算通过写一系列的文章,让大家能够认识SpringBoot,通过对SpringBoot的入门学习后,小编会在通过一个示例Demo来让大家能够真正上手SpringBoot。

适合人群

    1、有一定Java基础的朋友

    2、适合初中级的朋友。

如果文章编写中存在问题或者对文章有疑问,都可以留言小编,和小编一起探讨,小编会虚心接受大家的建议并更正。      

1.什么是Spring Boot

来源官方文档:     

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

上面的意思是说:

        Spring Boot可以很简单的就创建一个你可以运行的独立的、生产级别的应用系统。

        我们可以使用Spring平台和第三方库快速的开始,很多的Spring Boot应用需要很少的配置。

        

2.如何搭建一个Spring Boot的环境

        对Spring Boot的定义,大家可能知道,但是还是有点晕头,现在我们直接上手,来搭建一个Spring Boot的项目,然后一步步的来讲解和实现,让大家更深一步来理解如何开始使用Spring Boot。

大家可以打开https://start.spring.io/,默认的选项,点击“Generate Project”按钮生成一个Maven的项目。


将生成的Maven项目进行解压,导入到Eclipse中。

导入的步骤:

    Import -> Existing Maven Projects -> Next -> 选择解压的Demo -> Finish

导入后如下图:



3.理解官网下载后的文件目录

如第2步中的上图所示:

    src/main/java

        --DemoApplication.java   主程序的入口,从这里执行运行,类似Java的Main函数入口

    src/main/resources  

        -- application.properties  配置文件,可以设置一些参数变量值,例如MyBatis数据库连接参数等等

    src/test/java   测试程序,可以在这里写测试用例


4.实现Hello World

    学习任何一门语句或者框架,第一个打印都是Hello World,因此,我们使用SpringBoot官网下载的Demo实现一下,如何显示Hello World

    在pom.xml中添加:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

    </dependency>

   个人建立一个com.example.demo.controller,实现一个HelloWorld类:

package com.example.demo.controller;

  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;

  @RestController
  public class HelloWorld {
	
	@RequestMapping("/hello")
	public String Hello(String name) {
		return "Hello " + name;
	}
 }

在浏览器中输入:http://localhost:8080/hello?name=World

5.通过Maven从无到有搭建Spring Boot环境

    有了上面的步骤,对SpringBoot有了一个基本的认识,现在我们通过Maven,从无到有的搭建一个和Demo一致的SpringBoot环境,也实现Hello World功能。如何创建一个Maven项目,小编在这里不做多的描述和截图,大家如果有问题可以留言给小编,我们一起来探讨学习,现在直接在一个Maven的项目基础上,实现SpringBoot的基本框架。

    如图为创建的一个Maven项目:

  

现在编写pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cyw</groupId>
  <artifactId>maven_sboot</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven_sboot Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.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</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

在src/main/java的目录下,创建com.chyanwu.demo包,创建一个Application.class的类

package com.chyanwu.demo;

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

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

直接可以运行,现在实现Hello World

创建com.chyanwu.demo.controller,在创建HelloWorld类

package com.chyanwu.demo.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloWorld {
	
	@RequestMapping("/hello")
	public String Hello(String name) {
		return "Hello " + name;
	}
}

 运行后,输入http://localhost:8080/hello?name=World



扫描关注:全栈工程师成长记

  • 60
    点赞
  • 406
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值