国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot快速搭建web項目詳細步驟總結

SpringBoot快速搭建web項目詳細步驟總結

2021-06-22 13:08CoderLcp Java教程

這篇文章主要介紹了SpringBoot快速搭建web項目詳細步驟總結 ,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近在學習spring boot 相關的技術,剛接觸就有種相見恨晚的感覺,因為用spring boot進行項目的搭建是在太方便了,我們往往只需要很簡單的幾步,便可完成一個spring mvc項目的搭建,感覺就是下圖:

SpringBoot快速搭建web項目詳細步驟總結

好,下面就本人搭建項目的過程簡單說說如何快速搭建一個spring mvc項目,相信我,spring-boot這趟車,你上了根本就停不下來了!

下面是這篇博客的主要內容:

  • spring boot 介紹
  • spring boot 項目快速搭建
  • spring-boot中單元測試
  • spring boot 與mybatis 的集成

一、初識spring boot

1、web項目傳統的搭建方式

導入jar包,配置tomcat 的context.xml 、web.xml,配置class path ,啟動。我靠,怎么一堆報錯?classnotfoundexception?尼瑪哥不是導入了jar包嗎,哦版本不對,那我使用maven管理jar包咯,千辛萬苦終于配置好maven 并學會了如何使用,啟動也沒報錯了,但是尼瑪已經過去打半天了我都還沒有開始正式代碼的書寫,趕緊開始寫業務代碼吧,又發現spring 中配置老是尼瑪的報錯,而我他媽只是想要搞個簡單的發郵件功能啊。

所以說,自己搭建web項目,很多步驟都是重復性的,有沒有什么工具可以一步達成啊?我只想快點投入到實際業務開發中去,我再也不想折騰jar包版本問題,再也不想折騰那繁瑣的配置過程(雖然這是一個很好的學習過程),那么,spring boot 估計是老鐵你不二的選擇!

2、spring boot 是什么?

什么是spring boot?直接copy官網的介紹:

SpringBoot快速搭建web項目詳細步驟總結

原諒我并不咋地的英文,官網說,spring boot 可以讓你以一種非常簡單的方式構建一個機遇application 的spring 項目,而你索要做的僅僅是run 這個項目,spring boot 集成了主流的框架,我們構建項目大概只需要兩三分鐘,spring boot 項目,只需要很少的配置即可。

用了spring boot 后,你會發現,什么配置問題,什么jar包問題統統沒有了,你的生產效力會提高很多,因為,spring boot 已經幫你弄好了一個項目原型,你只需要在這個原型上添加自己的業務代碼,自己的service 和dao就萬事大吉了!

spring boot 真的有那么厲害?好,下面就開始進入正題,說說怎么搭建利用spring boot搭建項目。

二、spring boot 項目快速搭建

1、快速搭建一個spring mvc項目

來開頭來個hello world先吧,下面緊跟我的步伐,利用spring boot 快速搭建一個mvc項目

第一步,上官網,進行項目原始jar 包的選取,官網在線搭建地址如下:https://start.spring.io/,看圖:

SpringBoot快速搭建web項目詳細步驟總結

特別說明下:在選擇你要的jar 包中,一般包括三部分:web部分--包含 了spring、springboot 等常用的web開發必須jar包;spring提供的工具類部分(devtool),這個用于熱部署效果;數據庫部分,spring boot自動可以幫你繼承各種數據庫框架,我這里以mybatis 為演示例子,最后選完jar包的效果如下:

SpringBoot快速搭建web項目詳細步驟總結

第二步、下載項目并導入ide,當然,需要版本管理工具的支持,這里推薦intellij idea 開發工具(它簡直就是web開發的標配!),具體看下面截圖:

  SpringBoot快速搭建web項目詳細步驟總結

解壓后得到項目文件SpringBoot快速搭建web項目詳細步驟總結,然后,就可以打開我們的ide,這里已我最愛的intellij idea為例子

打開idea,找到file-->open-->選擇項目的路徑,找打pom文件-->以project 形式打開

SpringBoot快速搭建web項目詳細步驟總結

SpringBoot快速搭建web項目詳細步驟總結

第三步、hello spring項目 大概長什么樣子?打開項目之后,maven 會加載必須的jar包,這段時間,你可以去喝杯香醇的龍井茶或者咖啡,當項目構建完成之后,你會看到下面這樣的典型maven目錄結構:

SpringBoot快速搭建web項目詳細步驟總結

具體各個目錄放什么,請參考截圖,這里補充說明的是,hellospringbootapplication是整個項目的入口,我們啟動項目不再是啟動tomcat,而是運行這個類,這個類的main方法就是整個項目的main方法即執行入口,

第四步,寫hello demo了。新建一個package demo,然后新建一個類hellospringboot,寫入以下代碼

?
1
2
3
4
5
6
7
8
@restcontroller
public class hellospringboot {
  @requestmapping(path = {"/hellospringboot"})
  public string hellospring (){
    system.out.println("hello spring boot");
    return "hello spring boot";
  }
}

我們先不解釋這個類里面的東西,先配置并啟動網站,初步感受下spring boot的魔力:

在啟動網站前,由于spring boot 是默認自動注冊加載數據庫相關的類文件的,所以為了不報錯,我們需要打開數據庫并在resource目錄下的application.property中加入數據庫配置相關文件,這里以mysql為例子,配置文件如下:

?
1
2
3
spring.datasource.url=jdbc:mysql://localhost:3306/wenda?useunicode=true&characterencoding=utf8&usessl=false
spring.datasource.username=root
spring.datasource.password=root

配置文件中各個字段含義估計讀者也能看懂,后面會再具體說明。

當然,如果沒有安裝數據庫,讀者也可以在hellospringbootapplication 類中利用exclude屬性聲明不自動注冊加載數據庫相關文件,具體參考下面的代碼:

?
1
2
3
4
5
6
7
@springbootapplication(exclude={datasourceautoconfiguration.class,mybatisautoconfiguration.class})//這里的mybatisautoconfiguration.class對應你的數據庫框架
public class hellospringbootapplication {
 
  public static void main(string[] args) {
    springapplication.run(hellospringbootapplication.class, args);
  }
}

然后,我們所有的工作就完成了,可以啟動網站了,見證奇跡的時候到了。

找到hellospringbootapplication類,雙擊打卡,右擊鼠標,run,然后項目就啟動了。

SpringBoot快速搭建web項目詳細步驟總結

啟動網站之后,輸入http://localhost:8080/hellospringboot即可訪問對應的方法。瀏覽器以及idea后臺效果如圖

SpringBoot快速搭建web項目詳細步驟總結

SpringBoot快速搭建web項目詳細步驟總結

這里簡單解釋下這個類里面的東西的含義:

首先是@restcontroller這個注解:用過spring的大佬估計知道是什么東西,當我們其他類要通過spring引用該類時,第一步就是要將該類注冊到spring中,@restcontroller就是相當于將這個類注冊到spring容器的意思當然,還有一種比較常用的就是@controller,具體他們兩區別后面再討論吧。

然后就是@requestmapping,通過設置這個注解里面面的path屬性,就可以聲明相對于項目根路徑(localhost:8080)的訪問路徑。

2、spring boot 項目中利用springmvc實現傳參

上面的demo中的方法只是一個非常簡單的實例,在實際生產中, 我們的應用不可能這么簡單的,前端還需要傳輸參數到后臺,所以,用spring boot 構造的項目中,又如何實現傳參呢?(感覺下面的都是在總結sprign mvc相關的內內容了,所以大神勿噴我脫離主題,因為感覺spring boot 如果單獨總結的話,貌似的確沒什么好總結的,而且spring boot 天生就不可能和其他spring 組件分割)具體請參開下面代碼例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@restcontroller
public class hellospringboot {/**
   * url傳參,訪問的路徑類似這樣:localhost:8080/getparamdemo1/1
   * 方法體中的參數要在前面加注釋,@pathvariable,代表url中的參數
   */
  @requestmapping(path = {"/getparamdemo1/{id}"})
  public string getparamdemo1 (@pathvariable("id") int userid){
    system.out.println("get param " + userid);
    return "success get param";
  }
  /**
   * 當然,你也可以通過這種傳參方式:localhost:8080/getparamdemo?param1=1或者直接表單提交參數
   * 當然,同時方法中參數聲明的注釋也要變成@requestparam,代表請求參數,required屬性說明了參數是否是必須的
   */
  @requestmapping(path = {"/getparamdemo2"})
  public string getparamdemo2 (@requestparam(value="param1",required = false) int param){
    system.out.println("get param " + param);
    return "success get param";
  }
}

3、構建restful編程風格

最近,resultful風格編程挺火的(雖然個人覺得不可盲目跟風),而spring boot中利用其內置的spring mvc等框架,可以非常簡單地實現這種編程風格。

例如說,我要請求查詢一個數據,在resultful類的編程風格中,查詢對應的是get請求,那么,spring boot(準確說是spring mvc) 可以針對不同的請求進行處理,請看下面代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@restcontroller
public class hellospringboot {
 
  /**
   * 通過設置requestmapping的method屬性便可以設置該方法可處理的對應請求了,例如下面的getrequestdemo方法只會處理get請求
   */
  @requestmapping(path = {"/getrequestdemo"},method = requestmethod.get)
  public string getrequestdemo (@requestparam(value="param1",required = false) int param){
    system.out.println("get request test ,get param " + param);
    return "success get param";
  }
  /**
   * 下面的deleterequestdemo方法只會處理delete請求
   */
  @requestmapping(path = {"/deleterequestdemo"},method = requestmethod.delete)
  public string deleterequestdemo (@requestparam(value="param1",required = false) int param){
    system.out.println("delete request test ,get param " + param);
    return "success get param";
  }
}

另外,利用上面說的url中的值(類似這個:path="/member/{userid}")進行資源定位,也非常符合resultful的風格要求,例如這path="/getparamdemo1/{userid}"的配置就是對應的就是對會員級別的某個用戶(由userid定位)進行某些操作,如果要刪除該用戶,則對應http請求的delete請求即可。

通過上面的那些步驟,估計讀者對于如何用spring boot 進行一個簡單的web項目快速搭建已經清楚了,下面在spring boot搭建的項目中如何進行單元測試。

三、spring boot 項目中的單元測試問題

spring boot 中集成了junite框架,對于單元測試,也不用寫各種繁瑣的類了,只需要對測試類進行一定的注釋,spring boot 便會幫你做好一切,就算是數據庫相關的操作,spring boot 也能很好地進行測試,具體還是看代碼例子,下面是一個簡單的測試業務類方法的例子:

首先,我們新建一個service 層,新增一個demo類,最后的demo代碼如下:

?
1
2
3
4
5
6
7
8
@component
public class servicedemo {
  public string testdemo(){
    string rtnafterdosomething = "我是返回結果";
    //這里是業務代碼
    return rtnafterdosomething;
  }
}

下面的代碼是測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
@runwith(springrunner.class)
@springboottest
public class servicedemotest {
  @autowired
  servicedemo servicedemo;
  @test
  public void testdemo() {
    string rtn = servicedemo.testdemo();
    assert.assertequals(rtn, "我是返回結果");
  }
 
}

如果要測試數據庫dao,也是類似的,但是特別注意的是,測試之前必須啟動項目(即hellospringbootapplication的run方法必須運行起來),否則將會包can not find applicationcontext的錯誤。

四、spring boot中配置數據庫框架(以mybatis為例子)

在spring boot中集成數據庫相關的開源框架也是很方便的(當然,記得在一開始新建項目的時候引用相關的jar包),當你上面的步驟都沒什么錯誤的時候,你只需要簡單地配置下applocation.properties文件即可

?
1
2
3
4
5
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useunicode=true&characterencoding=utf8&usessl=false
spring.datasource.username=root
spring.datasource.password=root
#下面這條配置聲明了mybatis的配置文件路徑,classpath對應的是和這個文件統計的resources
mybatis.config-location=classpath:mybatis-config.xml

目錄的文件結構參考下面截圖:

SpringBoot快速搭建web項目詳細步驟總結

ok,mybatis 的引用配置便弄好了,下面我們嘗試利用mybatis 這個數據庫框架,進行一個數據庫的簡單查詢操作,首先,看下面的mybatis-config.xml的配置內容,讀者可以以這個為模板進行配置,當然,想了解更具體的mybatis配置說明,建議去官網看看,這里不展開了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<!doctype configuration
    public "-//mybatis.org//dtd config 3.0//en"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 
  <settings>
    <!-- globally enables or disables any caches configured in any mapper under this configuration -->
    <setting name="cacheenabled" value="true"/>
    <!-- sets the number of seconds the driver will wait for a response from the database -->
    <setting name="defaultstatementtimeout" value="3000"/>
    <!-- enables automatic mapping from classic database column names a_column to camel case classic java property names acolumn -->
    <setting name="mapunderscoretocamelcase" value="true"/>
    <!-- allows jdbc support for generated keys. a compatible driver is required.
    this setting   forces generated keys to be used if set to true,
     as some drivers deny compatibility but still work -->
    <setting name="usegeneratedkeys" value="true"/>
  </settings>
 
  <!-- continue going here -->
 
</configuration>

恩,然后,就可以進行數據庫的操作了,我簡單用mysql建了個名字為springboot的數據庫,在庫里面建了一個簡單的demo表(具體怎么建表那些不用我說了吧?實在沒有接觸過數據庫的同學,可以自行百度或者谷歌去咯),然后,dao代碼如下:

?
1
2
3
4
5
@mapper
public interface demodao {
  @select({"select demo_param from demo "})
  string querydemo();
}

注意,mybatis 的dao是一個接口,關于mybatis更多的內容,請讀者參開官方文檔,這里不展開了。

然后,就在原來的servicedemo類中調用一下吧,代碼看下圖吧: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;
import org.springframework.stereotype.controller;
import springboot.hello.hellospringboot.dao.demodao;
 
@component
public class servicedemo {
  @autowired
  demodao demodao;
  public string testdemo(){
    string rtnafterdosomething = "我是返回結果";
    //這里是業務代碼
    return rtnafterdosomething;
  }
  public string querydemo(){
    return demodao.querydemo();
  }
}

ok,簡單寫個測試類,出現小綠條,測試成功。

然后,所有項目原型的工作完成了,然后,我們就可以進行我們核心業務開發了,很快有沒有?很爽有木有?所以說,spring boot 搭建項目的確飛快,可以讓我們省去很多機械的前期操作,讀者們趕緊去探索下吧,spring boot ,你值得捅有!

ok,今天總結到這里,這個項目的代碼在github上這個地址,需要的人自?。?a rel="external nofollow" target="_blank">https://github.com/lcpandcode/hellospringboot.git

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/lcplcpjava/p/7406253.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天久久综合网 | 黄在线观看 | 伦理午夜电影免费观看 | 精品天堂| 日韩国产欧美一区 | 一级全黄少妇性色生活片免费 | 91亚洲视频 | 偷拍做爰吃奶视频免费看 | 黄色一级毛片在线观看 | 欧美国产精品一区二区三区 | 久久久在线 | 欧美日韩一区二区三区不卡视频 | 国产一区二区三区在线 | 亚洲高清在线视频 | 色香阁99久久精品久久久 | 午夜私人影院在线观看 | 国产在线视频a | 欧美一区二区三区在线观看视频 | 狠狠ri| 亚洲精品无| 久久久成人精品 | 在线欧美视频 | 免费色视频 | 国产精品久久综合 | 日韩欧美精品 | 毛片av在线播放 | 日韩欧美综合 | 久色视频在线 | 久久久777 | 在线观看成人 | 在线免费观看黄色 | 依人在线观看 | 黄色在线免费观看 | 国产精品1区 | 国产脚交av在线一区二区 | 亚洲狠狠丁香婷婷综合久久久 | 精品久久久久久久久久久 | 欧美日韩一区二区三区在线观看 | 国产亚洲精品美女久久久久久久久久 | 久久久精品国产 | 天天澡天天狠天天天做 |