spring boot系列01--快速構建spring boot項目

最近的項目用spring boot 框架 藉此學習了一下 這裏作一下總結記錄
java

很是便利的一個框架 它的優缺點我就不在這背書了 想了解的能夠自行度娘谷歌git

說一下要寫什麼吧 其實還真不是很清楚,只是想記錄一下本身學習的過程github

願景是寫成一個小的spring boot系列而後能作一些迭代升級 也能夠對比不一樣技術的優劣 因此也會寫其一些周邊 好比 用到的技術(datatables,maven,thymeleaf,mybatis,jpa)web

和有(tong)趣(ku)的過程 廢話就說的到這吧。spring

能夠的話 每一個技術點也單獨開一個系列...sql

來點乾貨數據庫

 

方式一 spring boot initializer 快速構建應用apache


從spring boot(https://start.spring.io/)官網直接能夠獲取一個瀏覽器

一、這裏選擇是Maven with Java 來構建項目,springboot

Maven不少人應該是都用過或者聽過這裏就很少說了(打算單寫一個系列),

對於沒有用過的同窗能夠簡單理解是給你來管理jar包,使用方法後面一塊講

二、Spring Boot版本 如圖

三、groupId;項目組織惟一的標識符 → 就是java的包

   artifactId;項目的惟一的標識符 → 項目名

四、這裏能夠選你項目要用到的jar 好比 web項目基礎jar、數據庫鏈接jar 如圖

如上選完點 [Generate Project] Springboot 就會按照你選的給構建一個Maven Web項目

如今把項目導入IED

這裏你能夠用 Eclipse 也能夠用 使用 Spring 定製版 Spring Tools Suite (STS)(http://spring.io/tools/sts/)

Eclipse 也能夠安裝 sts 插件  這裏這些就不細說了...

看導入後的效果(演示用的是STS)

 

主要說幾個文件 DemoApplication.java

如今就能夠直接右鍵項目Run 選 Spring Boot Appliction 運行了

如今若是有畫面或者寫個Controller是能夠訪問了 想說的是 這也是spring boot項目的減輕配置和部署的一個體現

固然它也支持想普通web項目一下添加到server而後啓動訪問的方式 須要單獨配置以後另說

 1 package com.example.demo;  2 
 3 import org.springframework.boot.SpringApplication;  4 import org.springframework.boot.autoconfigure.SpringBootApplication;  5 
 6 @SpringBootApplication  7 public class DemoApplication {  8 
 9     public static void main(String[] args) { 10         SpringApplication.run(DemoApplication.class, args); 11  } 12 }

pom.xml經過pom 若是網絡沒問題的話 下圖依賴的jar包應該已經導入了

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.example</groupId>
 7     <artifactId>demo</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>demo</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.7.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-data-jpa</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-web</artifactId>
35         </dependency>
36 
37         <dependency>
38             <groupId>org.postgresql</groupId>
39             <artifactId>postgresql</artifactId>
40             <scope>runtime</scope>
41         </dependency>
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-test</artifactId>
45             <scope>test</scope>
46         </dependency>
47     </dependencies>
48 
49     <build>
50         <plugins>
51             <plugin>
52                 <groupId>org.springframework.boot</groupId>
53                 <artifactId>spring-boot-maven-plugin</artifactId>
54             </plugin>
55         </plugins>
56     </build>
57 
58 
59 </project>

 

 方式二 STS本身構建


先說一下 你能夠 File→New→Spring Starter Project 來建立一個 spring boot 項目 這種方式 和上面說的從網頁上選建立 基本沒什麼區別 因此這個就不想提了

打算新建一個Maven的項目而後配置spring boot

 

 先寫一個Hello world吧

 

先新建一個Maven項目

 

選擇工做目錄

 

 選擇一個建立項目的類型 如圖 maven-archetype-webapp

 

group id 和 artifact id 上面 說過了 Package = group id + artifact id 清空和不改動 均可以

 

 

 而後如今的狀態是有錯 且 JRE 是1.5的版本

 

向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.sts.springboot</groupId>
  <artifactId>spring-boot-hello</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-boot-hello Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
          <!-- 編碼方式指定 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Java版本指定 -->
        <java.version>1.8</java.version>
  </properties>
    <!-- 父/基礎 包指定 -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
  <dependencies>
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>spring-boot-hello</finalName>
  </build>
</project>

 

而後 右鍵項目→Maven→Update Project→OK

 

點而後 錯解消 JRE 變成1.8的版本 且能看Maven Dependencies 配下 再pom裏配置的jar 已經被加進來了如圖

如今就是項目缺乏Source Folder 以下圖 Maven Dependencies 和 JRE Sys Lib 選中而後  Apply  項目中 下圖 missing 的兩個文件夾 就能顯示了

 

 

 以下圖添加兩個類 寫一下簡單的hello world

 

而後 右鍵項目→Run→Spring Boot App 沒問題的話以下圖

如今能夠在瀏覽器 輸入 http://localhost:8080/helloWorld

至此Spring boot 的簡單hello world 就完了

其餘的問題 好比 註解、配置 如今篇幅已經有點長了 放到後面繼續講吧

 

GitHub:spring-boot-hello

相關文章
相關標籤/搜索