1. 程式人生 > 實用技巧 >springboot學習體會(二)構建工程

springboot學習體會(二)構建工程

一.簡介

spring boot 它的設計目的就是為例簡化開發,開啟了各種自動裝配,你不想寫各種配置檔案,引入相關的依賴就能迅速搭建起一個web工程。它採用的是建立生產就緒的應用程式觀點,優先於配置的慣例。

可能你有很多理由不放棄SSM,SSH,但是當你一旦使用了springboot ,你會覺得一切變得簡單了,配置變的簡單了、編碼變的簡單了,部署變的簡單了,感覺自己健步如飛,開發速度大大提高了。就好比,當你用了IDEA,你會覺得再也回不到Eclipse時代一樣。另,本系列教程全部用的IDEA作為開發工具。

二.建構工程

我們需要準備的有:

  • jdk1.8或以上
  • maven3.0或以上
  • Idea

開啟Idea-> new Project ->Spring Initializr ->填寫group、artifact ->鉤上web(開啟web功能)->點下一步就行了。

三.工程目錄

建立完工程,工程的目錄結構如下:

  • pom檔案為基本的依賴管理檔案
  • resouces 資原始檔
    • statics 靜態資源
    • templates 模板資源
    • application.yml 配置檔案
  • SpringbootApplication程式的入口

pom.xml的依賴:

<?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.forezp</groupId>
	<artifactId>springboot-first-application</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot-first-application</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.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-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>


其中spring-boot-starter-web不僅包含spring-boot-starter,還自動開啟了web功能。

四.啟動springboot

cd到專案主目錄:

  • mvn spring-boot: run 啟動
  • cd 到target目錄,java -jar 專案.jar