spring data jpa簡介

spring data jpa

首先要介紹一下JPA,JPA就是Java Persistence API翻譯過來就是java持久化API,它是在sun提出的java持久化規範,這些接口都在javax.persistence包下。JPA的出現最要是爲了整合ORM框架,從本質上來說,它其實算是一種規範,而它提供的基本都是接口,而非具體實現,Hibernate就是一個不錯的實現例子。它兩的關係可以類比jdbc與jdbc驅動.

那麼Spring Data JPA與JPA的關係?
Spring Data JPA的作用可以理解爲在JPA規範的基礎下提供了Repository層的實現,你還可以自由的選擇一種符合JPA規範的ORM框架。

雖然ORM框架都實現了JPA規範,但是在不同ORM框架之間切換是需要編寫的代碼有一些差異,而通過使用Spring Data Jpa能夠方便大家在不同的ORM框架中間進行切換而不要更改代碼。可以看下圖類比。在這裏插入圖片描述

springboot整合spring data JPA demo

首先是配置文件

spring:
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: none
    properties:
      hibernate.format_sql: true
    open-in-view: false
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true
    username: root
    password: root

選擇的orm框架是hibernate
一些屬性
show_sql :屬性用於指定是否在控制檯上輸出 SQL 語句

ddl-auto
create:每次運行程序時,都會重新創建表,故而數據會丟失
create-drop:每次運行程序時會先創建表結構,然後待程序結束時清空表
upadte:每次運行程序,沒有表時會創建表,如果對象發生改變會更新表結構,原有數據不會清空,只會更新(推薦使用)
validate:運行程序會校驗數據與數據庫的字段類型是否相同,字段不同會報錯
none: 禁用DDL處理

format_sql:格式化sql語句

datasource下就是一些配置數據庫的信息

定義實體類

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id",nullable = false)
    private Long id;

    @Basic
    @Column(name="username",nullable = false)
    private String username;
    
    @Basic
    @Column(name="userage",nullable = false)
    private String userage;

    @Basic
    @Column(name="userStatus",nullable = false)
    private Integer userStatus;

    @Basic
    @Column(name = "create_Time",nullable = false)
    private Date createTime;

}

@Id代表主鍵

@GeneratedValue用於標註主鍵的生成策略,通過strategy 屬性指定
@GeneratedValue註解的strategy屬性提供四種值:
 AUTO主鍵由程序控制, 是默認選項 ,不設置就是這個
IDENTITY 主鍵由數據庫生成, 採用數據庫自增長, Oracle不支持這種方式
 SEQUENCE 通過數據庫的序列產生主鍵, MYSQL 不支持
 Table 提供特定的數據庫產生主鍵, 該方式更有利於數據庫的移植

@Entity 註解表明這是一個實體bean類型

@Basic 默認添加,寫不寫都行

@table 代表對應映射的表

@column代表要映射的表中字段,nullable爲是否可爲空,在企業級開發中一般不可爲空

定義Repository層

public interface UserRepository extends JpaRepository<User,Long> {

    User findByUsername(String name);

}

我們只需要繼承JpaRepository接口就可以了,其他工作spring data JPA會幫我們完成,自動生成sql。那個類型第一個參數是實體類類類型,第二個參數爲主鍵類型

我們只需要按照spring data JPA的命名規則給方法命名即可!

關鍵字 方法命名 sql where 字句
And findByNameAndid where name= ? and id =?
Or findByNameOrSex where name= ? or sex=?
Is,Equals findById,findByIdEquals where id= ?
Between findByIdBetween where id between ? and ?
LessThan findByIdLessThan where id < ?
GreaterThan findByIdGreaterThan where id > ?
GreaterThanEquals findByIdGreaterThanEquals where id > = ?
After findByIdAfter where id > ?
Before findByIdBefore where id < ?
IsNull findByNameIsNull where name is null
isNotNull,NotNull findByNameNotNull where name is not null
Like findByNameLike where name like ?
NotLike findByNameNotLike where name not like ?
StartingWith findByNameStartingWith where name like ‘?%’
EndingWith findByNameEndingWith where name like ‘%?’
Containing findByNameContaining where name like ‘%?%’
OrderBy findByIdOrderByXDesc where id=? order by x desc
Not findByNameNot where name <> ?
IgnoreCase findByNameIgnoreCase where UPPER(name)=UPPER(?)

可以看到按照特定的命名方式,spring data JPA就可以自動生成sql語句

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