跳转至

先創建一個Student class

@Table(name = "tableName") 我們要將這個 java class 去對應到資料庫的哪一個 table 上

@Id 用來表示這個id 變數 他對應到的是 Primary Key

@GeneratedValue(strategy = GenerationType.IDENTITY) 表示 id 的值是由資料庫自動去生成的

package com.example.demo;

import javax.persistence.*;

@Entity
@Table(name = "student") // 我們要將這個 java class 去對應到資料庫的哪一個 table 上
public class Student {
    @Id // 用來表示這個id 變數 他對應到的是 Primary Key
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 表示 id 的值是由資料庫自動去生成的
    @Column(name = "id")
    Integer id;
    @Column(name = "name")
    String name;

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

    public String getName() { return name; }
    public void setName(String name) { this.name = name;}
}

interface CrudRepository

繼承 Spring Data JPA 所提供的 interfate CrudRepository

package com.example.demo.JPA;

import org.springframework.data.repository.CrudRepository;

// 繼承 Spring Data JPA 所提供的 interfate CrudRepository<Student ,student Primary key 型別>
public interface StudentRepository extends CrudRepository<Student, Integer> {
}

Controller

package com.example.demo.JPA;

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

@RestController
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;
    @PostMapping("/students")
    public String insert(@RequestBody Student student){
        // 到時候就可以根據前端傳過來的參數就可以直接到資料庫中新增一筆 student 的數據
        studentRepository.save(student); 
        return "執行資料庫的 create 操作";
    }
}

Talend API Tester

POST http://localhost:8080/students

request body

{
    "name": "Jam"
}

實測幾種常用方法

update

  • 要先確認資料庫中是否有這筆數據 ,如果沒有確認 Spring Data JPA就自動去insert 創建一筆新的數據出來
package com.example.demo.JPA;

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

@RestController
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;
    @PostMapping("/students")
    public String insert(@RequestBody Student student){
        studentRepository.save(student); // 到時候就可以根據前端傳過來的參數就可以直接到資料庫中新增一筆 student 的數據
        return "執行資料庫的 create 操作";
    }

    @PutMapping("/students/{studentId}")
    public String update(@PathVariable Integer studentId ,
                         @RequestBody Student student) {
        student.setId(studentId);
        studentRepository.save(student); 
        return "執行資料庫的 Update 操作";
    }
}

Talend API Tester

PUT http://localhost:8080/students/1

request body

{
    "name": "Leo"
}

console:

測試沒有這個 id: 100

PUT http://localhost:8080/students/100

request body

{
    "name": "Mary"
}

console:

因為沒有去確認資料庫中是否有這筆數據

Spring Data JPA就自動去創建一筆新的數據出來

使用 save()方法時需要注意這點

delete

package com.example.demo.JPA;

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

@RestController
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;

    @DeleteMapping("/students/{studentId}")
    public String delete(@PathVariable Integer studentId) {
        studentRepository.deleteById(studentId);
        return "執行資料庫的 Delete 操作";
    }
}

Talend API Tester

DELETE http://localhost:8080/students/1

console:

Hibernate: delete from student where id=?

read

orElse(null) 如果在資料庫裡面找不到這筆student數據的話 ,那這個student object 的值就會是null

package com.example.demo.JPA;

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

@RestController
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/students/{studentId}")
    public Student read(@PathVariable Integer studentId) {
        // orElse(null) 如果在資料庫裡面找不到這筆student數據的話 ,那這個student object 的值就會是null
        Student student = studentRepository.findById(studentId).orElse(null);
        return student;
    }
}

Talend API Tester

GET http://localhost:8080/students/2

update + read

package com.example.demo.JPA;

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

@RestController
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;

    @PutMapping("/students/{studentId}")
    public String update(@PathVariable Integer studentId ,
                         @RequestBody Student student) {
        // 先確認數據是否存在 ,不存在會=null
        Student s = studentRepository.findById(studentId).orElse(null);
        if(s != null){
            s.setName(student.getName()); // 只針對name 去做update
            studentRepository.save(s);
            return "執行資料庫的 Update 操作";
        }else{
            return "Update 失敗 ,數據不存在";
        }
    }

}