1. 程式人生 > >spring boot 尚桂谷學習筆記11 數據訪問03 JPA

spring boot 尚桂谷學習筆記11 數據訪問03 JPA

bsp ack hibernate use pan 實體 ans 和數 數據訪問

整合JPA

技術分享圖片

技術分享圖片

技術分享圖片

  SpringData 程序數據交互結構圖 (springdata jpa 默認使用 hibernate 進行封裝)

  使用之後就關註於 SpringData 不用再花多經歷關註具體各個交互框架實現

技術分享圖片

技術分享圖片

  JPA:ORM 關系型數據

  1.新建項目 使用springboot 快速構建工具 模塊選擇 web, sql模塊 選擇 jdbc, mysql, jpa

    創建項目pom文件為:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency
> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId
>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

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

package com.lixuchun.springboot.entity;

import javax.persistence.*;

// 使用jpa註解配置映射關系
@Entity // 告訴jpa這是一個實體類 數據庫和表的映射
@Table(name = "tbl_user") // table來指定和那個表對應 如果省略就是 user表
public class User {

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

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

    @Column // 省略默認列名就是屬性名稱
    private String email;
  
   get set ......

  3)編寫一個Dao接口來操作試題對應的數據表,對yml文件進行jpa配置(參照JpaProperties 配置類進行配置


package com.lixuchun.springboot.repository;

import com.lixuchun.springboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

// 集成 jpaRepository 來完成數據庫的操作
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
spring:
  datasource:
    url: jdbc:mysql://192.168.10.129/jpa
    username: root
    password: 101022li
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    hibernate:
      # 更新或者創建數據表
      ddl-auto: update
    show-sql: true

  4)編寫一個 測試 Controller

package com.lixuchun.springboot.controller;

import com.lixuchun.springboot.entity.User;
import com.lixuchun.springboot.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;

@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id") Integer id) {
        User user = userRepository.findOne(id);
        return user;
    }

    @GetMapping("/user")
    public User insertUser(User user) {
        User save = userRepository.save(user);
        return save;
    }

}

  瀏覽器進行測試:

    localhost:8080/user?lastName=jack&email=aa 進行數據插入

    localhost:8080/user/1 進行id查詢

  技術分享圖片

  https://www.bilibili.com/video/av23478787/?p=68 TODO

spring boot 尚桂谷學習筆記11 數據訪問03 JPA