springboot入門

SpringBoot

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

SpringBoot和SpringMVC的關(guān)系

SpringMVC->SpringBoot

SpringBoot的特點(diǎn)

1.化簡(jiǎn)為簡(jiǎn),簡(jiǎn)化配置
2.備受關(guān)注,是下一代框架

微服務(wù)->SpringCloud->SpringBoot

前置知識(shí)
利用maven構(gòu)建項(xiàng)目 ->《項(xiàng)目管理利器》
Spring注解。 -> 《Spring入門篇》
RESTful API

課程學(xué)到
第一個(gè)SpringBoot程序
自定義屬性配置
Controller的使用
spring-data-jpa
事務(wù)管理

第一個(gè)SpringBoot應(yīng)用

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項(xiàng)目必須引入的依賴
<artifactId>spring-boot-starter-test</artifactId>單元測(cè)試

啟動(dòng)

方法一

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
  
新建一個(gè)類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請(qǐng)求
@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ù)據(jù)
@RequestParam 獲取請(qǐng)求參數(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ù)據(jù)庫(kù)操作

Spring-Data-Jpa

JPA(Java Persistence API)定義了一系列對(duì)象持久化的標(biāo)準(zhǔn),目前實(shí)現(xiàn)這一規(guī)劃的產(chǎn)品有Hibernate、TopLink等。

RESTful API設(shè)計(jì)

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

<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ù)據(jù)庫(kù)

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);
}

事務(wù)管理

查詢的時(shí)候不需要事務(wù)

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();
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容