SpringBoot 整合 JPA

SpringData 的特點

SpringData 爲我們提供使用統一的 API 來對數據訪問層進行操作。這主要是 SpringData Commons 項目來實現的。SpringData Commons 讓我們在使用關係型或者非關係型數據訪問技術時都基於 Spring 提供的統一標準,標準包含了 CRUD(創建、獲取、更新、刪除)、查詢、排序和分頁的相關操作

統一的 Repository

Repository<T, ID extends Serializable> 統一接口

RevisionRepository<T, ID extends Serializable, N extends Number & Comparable> 基於樂觀鎖機制

CurdRepository<T, ID extends Serializable> 基本 CRUD 操作

PagingAndSortingRepository<T, ID extends Serializable> 基本 CRUD 及分頁

編寫實體類

編寫一個實體類(bean)和數據表進行映射,並且配置好映射

package com.example.demo.entity;

import javax.persistence.*;

/**
 * @author Woo_home
 * create by 2020-05-16 20:30
 */

// 配置映射關係
@Entity  // 告訴 JPA 這是一個實體類(和數據表映射的類)
@Table(name = "tbl_user") // @Table 指定和哪個數據表對應,如果省略,那麼默認表名就是類名的小寫
public class User {

    @Id // 這是一個主鍵
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增主鍵
    private Integer id;

    @Column(name = "last_name", length = 50) // 這是和數據表對應的一個列
    private String lastName;

    @Column // 省略默認列名就是屬性名
    private String email;
}

編寫 DAO

編寫一個 Dao 接口來操作實體類對應的數據表(Repository)

package com.example.demo.repository;

import com.example.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @author Woo_home
 * create by 2020-05-16 20:37
 */

// 繼承 JpaRepository 來完成對數據庫的操作
public interface UserRepository extends JpaRepository<User, Integer> {


}

基本配置

application.properties 配置

# 更新或者創建數據表結構
spring.jpa.hibernate.ddl-auto=update
# 控制檯顯示 SQL
spring.jpa.show-sql=true

啓動應用

在這裏插入圖片描述
控制檯會輸出以下內容,這是創建了一個表
在這裏插入圖片描述
打開 Navicat 可以發現,將我們的 tbl_user 表創建成功了
在這裏插入圖片描述
字段也是根據我們設置的一樣
在這裏插入圖片描述

增刪改查

編寫 Controller

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Woo_home
 * create by 2020-05-16 21:32
 */

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    /**
     * 查詢數據
     * @param id 根據 ID 查詢
     * @return
     */
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id) {
        User user = userRepository.findById(id).get();
        return user;
    }

    /**
     * 插入數據
     * @param user
     * @return
     */
    @GetMapping("/user")
    public User insertUser(User user) {
        User save = userRepository.save(user);
        return save;
    }

    /**
     * 刪除數據
     * @param id 根據 id 刪除
     */
    @GetMapping("/delete/{id}")
    public void deleteUser(@PathVariable("id") Integer id) {
        userRepository.deleteById(id);
    }
}

新增

/**
  * 插入數據
  * @param user
  * @return
  */
@GetMapping("/user")
public User insertUser(User user) {
    User save = userRepository.save(user);
    return save;
}

在瀏覽器中輸入 http://localhost:8080/user?lastName=zhangsan&email=aa ,即可插入一條數據
在這裏插入圖片描述
刷新數據庫
在這裏插入圖片描述
控制檯輸出:
在這裏插入圖片描述

查詢

/**
  * 查詢數據
  * @param id 根據 ID 查詢
  * @return
  */
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Integer id) {
    User user = userRepository.findById(id).get();
    return user;
}

在瀏覽器中輸入 http://localhost:8080/user/1 即可獲得以下數據
在這裏插入圖片描述
控制檯輸出:
在這裏插入圖片描述

刪除

/**
 * 刪除數據
 * @param id 根據 id 刪除
 */
@GetMapping("/delete/{id}")
public void updateUser(@PathVariable("id") Integer id) {
    userRepository.deleteById(id);
}

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章