spring-boot兩小時入門筆記

初始化好工程以後java

入門

新建一個HelloController.javamysql

package com.example.girl1;

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

@RestController
public class HelloController {
  @RequestMapping(value = "/girl",method = RequestMethod.GET)
  public String say(){
    return "1111111";
  }
}

複製代碼

在瀏覽器中輸入localhost:8080/girl 內容就是1111111web

啓動方式

1. idea直接啓動

2. mvn

  • mvn spring-boot:run

3. 打包成jar包啓動

  • mvn install
  • cd target
  • java -jar jar包名

項目屬性配置

resource 目錄下 將application.xxx修改爲application.ymlspring

server:
 port: 8080
 servlet:
 context-path: /first
複製代碼

以後訪問就得 :http://localhost:8080/first/girlsql

配置文件屬性的使用

server:
  port: 8080
cupSize: A

複製代碼

HelloController.java中shell

@RestController
public class HelloController {
  // 經過註解把配置文件中的cupSize注入到變量中
  @Value("${cupSize}")
  private String cupSize;
  @RequestMapping(value = "/girl",method = RequestMethod.GET)
  public String say(){
    return cupSize;
  }
}
複製代碼

打開瀏覽器就能看到cupSize了數據庫

屬性多的時候就加個前綴來區分

新建一個類 GirlProperties.java

package com.example.girl1;

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

// 獲取前綴是girl的配置
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
  private  String cupSize;
  private  Integer age;

  public Integer getAge() {
    return age;
  }

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


  public String getCupSize() {
    return cupSize;
  }

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

複製代碼

配置文件json

server:
 port: 8080
girl:
 cupSize: A
 age: 18
複製代碼

HelloControllerapi

@RestController
public class HelloController {
  @Autowired
  private GirlProperties girlProperties;
  @RequestMapping(value = "/girl",method = RequestMethod.GET)
  public Integer say(){
    return girlProperties.getAge();
  }
}
複製代碼

這樣就能作到用前綴來區分,本身打開瀏覽器看看就明白了瀏覽器

配置的開發環境和生產環境

  • application.yml
// application.yml
 spring:
 profiles:
 active: dev   
複製代碼
  • application-dev.yml
server:
 port: 8080
girl:
 cupSize: B
 age: 18

複製代碼
  • application-prod.yml
server:
 port: 8081
girl:
 cupSize: A
 age: 20

複製代碼
  • 能夠在idea中啓動開發環境,
  • 在mvn install 後生成的jar包啓動開發環境
java -jar jar包名 --spring.profiles.active=prod
複製代碼

Controller的使用

  • @Controller 處理http請求
  • @RestController Spring4以後新加的註解,原來返回json須要@RequestBody配合@Controller(RestController至關於這兩個的組合RequestBody和Controller)
  • @RequestMapping 配置url映射

@RequestMapping

// 若是想訪問兩個
@RequestMapping(value = {"/girl","/hello"},method = RequestMethod.GET)
複製代碼
// 能夠給類加,訪問就得 :localhost:8080/hello/girl了
@RestController
@RequestMapping("/hello")
public class HelloController {
  @Autowired
  private GirlProperties girlProperties;
  @RequestMapping(value = {"/girl"},method = RequestMethod.GET)
  public Integer say(){
    return girlProperties.getAge();
  }
}
// RequestMethod方式有不少
能夠使用postman來測試api
複製代碼

處理url裏面的參數

  • @PathVariable 獲取url裏面的數據
  • @RequestParam 獲取請求參數的值
  • @GetMapping 組合註解

@PathVariable

@RestController
public class HelloController {
  @Autowired
  private GirlProperties girlProperties;
  @RequestMapping(value = {"/girl/{id}"},method = RequestMethod.GET)
  public Integer say(@PathVariable("id") Integer myId){
    return myId;
  }
}
// localhost:8080/girl/100
複製代碼

@RequestParam

//localhost:8080/girl?id=100
@RestController
public class HelloController {
  @Autowired
  private GirlProperties girlProperties;
  @RequestMapping(value = {"/girl"},method = RequestMethod.GET)
  public Integer say(@RequestParam("id") Integer myId){
    return myId;
  }
}
// 另外一種寫法
@RestController
public class HelloController {
  @Autowired
  private GirlProperties girlProperties;
  @RequestMapping(value = {"/girl"},method = RequestMethod.GET)
  // 默認值和是否必須傳入
  public Integer say(@RequestParam(value = "id",required = false,defaultValue = "10") Integer myId){
    return myId;
  }
}
// RequestMapping 能夠寫成GetMapping
複製代碼

數據庫操做

在pom.xml導入包

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
  </dependency>
複製代碼

結束後導入一下包

配置文件的配置

// 運行前先新建一個dbgirl的數據庫,utf8mb4
// 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: 123456
 jpa:
 hibernate:
 ddl-auto: update // create會把以前的表刪掉在新建一個
 show-sql: true
複製代碼

新建一個Girl類

package com.example.girl1;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
// 這個註解表示這個類就是對應數據庫中的表
@Entity
public class Girl {
  @Id
  @GeneratedValue
  private Integer id;

  private String cupSize;

  private Integer age;

  public Girl() {

  }


  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = 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;
  }
}
// 以後表中的那些字段就是girl類中對應過去的
複製代碼

接口編寫

get    /girls    獲取女生列表

post    /girls    建立一個女生

get    /girls/id   經過id查詢一個女生

put    /girls/id    經過id更新一個女生

delete    /girls/id    經過id刪除一個女生
複製代碼

獲取女生列表

新建一個GirlRepository.java的接口

package com.example.girl1;

import org.springframework.data.jpa.repository.JpaRepository;
// Girl類名,Integer是id的類型
public interface GirlRepository extends JpaRepository<Girl,Integer> {

}

複製代碼

新建GirlController.java

package com.example.girl1;

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

import java.util.List;
@RestController
public class GirlController {
  @Autowired
  private GirlRepository girlRepository;
  /* * 查詢女生列表 * */
  @GetMapping(value = "/girls")
  public List<Girl> girlList(){
    return girlRepository.findAll();
  }
}
// 使用postman測試
複製代碼

建立一個女生

/* * 新增一個女生 * */
  @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);
  }
複製代碼

經過id查詢一個女生

// 查詢一個女生
  @GetMapping(value = "/girls/{id}")
  public Girl girlFindOne(@PathVariable("id") Integer id){
    return  girlRepository.findById(id).orElse(null);
  }
複製代碼

經過id更新一個女生

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

  }
複製代碼

經過id刪除一個女生

// 刪除
    @DeleteMapping(value = "/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
         girlRepository.deleteById(id);
    }
複製代碼

經過年齡查詢女生列表

// 經過年齡查詢女生列表
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return  girlRepository.findByAge(age);
    }
複製代碼

GirlRepository.java中

package com.first.first;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface GirlRepository extends JpaRepository<Girl,Integer> {
    // 經過年齡查詢
    public List<Girl> findByAge(Integer age);
}

複製代碼

事務管理

需求是若是我有兩條數據,一條插入失敗的話另外一條就不能插入,這時候須要加一個事務

GirlService.java

package com.example.girl1;

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

@Service
public class GirlService {
  @Autowired
  private GirlRepository girlRepository;
  @Transactional
  public void insertTwo(){
    Girl girlA= new Girl();
    girlA.setAge(10);
    girlA.setCupSize("B");
    girlRepository.save(girlA);

    Girl girlB= new Girl();
    girlA.setAge(11);
    girlA.setCupSize("F");
    girlRepository.save(girlB);
  }
}

// 在Girlcontroller裏新增

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

複製代碼
相關文章
相關標籤/搜索