springboot入門

SpringBoot

編碼 配置 部署 監(jiān)控 -> 簡單

SpringBoot和SpringMVC的關系

SpringMVC->SpringBoot

SpringBoot的特點

1.化簡為簡旅敷,簡化配置
2.備受關注,是下一代框架

微服務->SpringCloud->SpringBoot

前置知識
利用maven構建項目 ->《項目管理利器》
Spring注解颤霎。 -> 《Spring入門篇》
RESTful API

課程學到
第一個SpringBoot程序
自定義屬性配置
Controller的使用
spring-data-jpa
事務管理

第一個SpringBoot應用

New Project -> Spring Initializr -> next -> Artifact:girl Group:com.binperson -> next -> Web web

<setting>
    <mirrors>
        <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>*</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/goups/public</url>
        <mirror>
    </mirrors>
</setting>

<artifactId>spring-boot-starter-web</artifactId>作為web項目必須引入的依賴
<artifactId>spring-boot-starter-test</artifactId>單元測試

啟動

方法一

GirlApplication run

http://localhost:8080/hello

package com.binperson;

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

/**
 * Created by binperson on 2017/5/14.
 */
@RestController
public class HelloController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return "hello Spring Boot!";
    }
}

方法二

cd gril
mvn spring-boot:run

方法三

mvn install
cd target
ll
java -jar girl-0.0.1-SNAPSHOT.jar

屬性配置

spring.datasource.url:jdbc:mysql://127.0.0.1:3306/
spring.datasoucce.username:root
spring.datasource.password:
spring.datasource.driver-class-name: com.mysql.jdbc

delete static templates

resources -> application.properties

server.port=8081
server.content-path=/girl


application.yml
server:
    port: 8081
    context-path: /girl
    
http://localhost:8081/girl/hello    
server:
    port: 8080
cupSize: A
age: 18
content: "supSize: ${cupSize}, age: ${age}"

package com.binperson;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RestController
public class HelloController {
    @Value("${cupSize}")
    private String cupSize;

    @Value("${age}")
    private Integer age;

    @Value("${content}")
    private String content;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return cupSize + age + content;
    }

}
server:
  port: 8080
girl:
  cupSize: B
  age: 18
  
新建一個類GirlProperties
package com.binperson;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by binperson on 2017/5/14.
 */
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
    private String cupSize;
    private Integer age;

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return girlProperties.getCupSize();
    }

}

application-dev.yml
server:
  port: 8080
girl:
  cupSize: B
  age: 18
application-prod.yml
server:
  port: 8080
girl:
  cupSize: F
  age: 18
application.yml
spring:
  profiles:
    active: dev

Controller的使用

@Controller 處理http請求
@RestController Spring4之后新加的注解媳谁,原來返回json需要@ResponseBody配合@Controller
@RequestMapping 配置url映射

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 * Created by binperson on 2017/5/14.
 */
@Controller
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return girlProperties.getCupSize();
    }

}

resources -> templates -> index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>hello sb</h1>
</body>
</html>

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by binperson on 2017/5/14.
 */
@Controller
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return "index";
    }

}
package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.POST)
    public String say() {
        return girlProperties.getCupSize();
    }

}

@PathVariable 獲取url中的數(shù)據
@RequestParam 獲取請求參數(shù)的值
@GetMapping 組合注解

http://localhost:8080/bin/say/13

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say/{id}"}, method = RequestMethod.GET)
    public String say(@PathVariable("id") Integer id) {
        return "id:" + id;
        //return girlProperties.getCupSize();
    }

}
http://localhost:8080/bin/say/11?id=111

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say/{id}"}, method = RequestMethod.GET)
    public String say(@RequestParam("id") Integer myid) {
        return "id:" + myid;
        //return girlProperties.getCupSize();
    }

}


http://localhost:8080/bin/say/11?id=

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say/{id}"}, method = RequestMethod.GET)
    public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer myid) {
        return "id:" + myid;
        //return girlProperties.getCupSize();
    }

}

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    //@RequestMapping(value = {"/say/{id}"}, method = RequestMethod.GET)
    @GetMapping(value = "/say")
    public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer myid) {
        return "id:" + myid;
        //return girlProperties.getCupSize();
    }

}

數(shù)據庫操作

Spring-Data-Jpa

JPA(Java Persistence API)定義了一系列對象持久化的標準,目前實現(xiàn)這一規(guī)劃的產品有Hibernate友酱、TopLink等晴音。

RESTful API設計

請求類型 請求路徑 功能
GET /girls 獲取女生列表
POST /girls 創(chuàng)建一個女生
GET /girl/id 通過id查詢一個女生
PUT /girl/id 通過id更新一個女生
DELETE /girls/id 通過id刪除一個女生

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

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

application.yml

spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/dbgirl
    username: root
    password:
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    
    
package com.binperson;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Created by binperson on 2017/5/14.
 */
@Entity
public class Girl {
    @Id
    @GeneratedValue
    private Integer id;
    private String cupSize;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Girl() {

    }
}


ddl-auto: update    

數(shù)據庫

package com.binperson;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * Created by binperson on 2017/5/16.
 */
public interface GirlRepository extends JpaRepository<Girl, Integer> {
    //通過年齡查詢
    public List<Girl> findByAge(Integer age);
}

事務管理

查詢的時候不需要事務

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by binperson on 2017/5/16.
 */
@Service
public class GirlService {
    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void insertTwo() {
        Girl girlA = new Girl();
        girlA.setCupSize("A");
        girlA.setAge(18);
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setCupSize("A");
        girlB.setAge(19);
        girlRepository.save(girlB);
    }
}


package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by binperson on 2017/5/16.
 */
@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    GirlService girlService;

    @GetMapping(value = "/girls")
    public List<Girl> girlList() {
        return girlRepository.findAll();
    }

    @PostMapping(value = "girls")
    public Girl girlAdd(@RequestParam("cupSize") String cupSize,
                          @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    @GetMapping(value = "/girls/{id}")
    public Girl girlFindOne(@RequestParam("id") Integer id) {

        return girlRepository.findOne(id);
    }

    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@RequestParam("id") Integer id,
                           @RequestParam("age") Integer age,
                           @RequestParam("cupSize") String cupSize){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return girlRepository.save(girl);
    }
    @DeleteMapping(value = "/girls/{id}")
    public void girlDelete(@RequestParam("id") Integer id){
        girlRepository.delete(id);
    }

    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }

    @PostMapping(value = "/girls/two")
    public void girlTwo() {
        girlService.insertTwo();
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市缔杉,隨后出現(xiàn)的幾起案子锤躁,更是在濱河造成了極大的恐慌,老刑警劉巖或详,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件系羞,死亡現(xiàn)場離奇詭異,居然都是意外死亡霸琴,警方通過查閱死者的電腦和手機椒振,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沈贝,“玉大人杠人,你說我怎么就攤上這事∷蜗拢” “怎么了嗡善?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長学歧。 經常有香客問我罩引,道長,這世上最難降的妖魔是什么枝笨? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任袁铐,我火速辦了婚禮揭蜒,結果婚禮上,老公的妹妹穿的比我還像新娘剔桨。我一直安慰自己屉更,他們只是感情好,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布洒缀。 她就那樣靜靜地躺著瑰谜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪树绩。 梳的紋絲不亂的頭發(fā)上萨脑,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機與錄音饺饭,去河邊找鬼渤早。 笑死,一個胖子當著我的面吹牛瘫俊,可吹牛的內容都是我干的鹊杖。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼军援,長吁一口氣:“原來是場噩夢啊……” “哼仅淑!你這毒婦竟也來了称勋?” 一聲冷哼從身側響起胸哥,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎赡鲜,沒想到半個月后空厌,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡银酬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年嘲更,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片揩瞪。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡赋朦,死狀恐怖,靈堂內的尸體忽然破棺而出李破,到底是詐尸還是另有隱情宠哄,我是刑警寧澤,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布嗤攻,位于F島的核電站毛嫉,受9級特大地震影響,放射性物質發(fā)生泄漏妇菱。R本人自食惡果不足惜承粤,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一暴区、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辛臊,春花似錦仙粱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至淹遵,卻和暖如春口猜,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背透揣。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工济炎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人辐真。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓须尚,卻偏偏與公主長得像,于是被迫代替她去往敵國和親侍咱。 傳聞我的和親對象是個殘疾皇子耐床,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內容