电竞比分网-中国电竞赛事及体育赛事平台

分享

Spring Data JPA

 昵稱71011036 2020-08-10

一、JPA概述:

  JPA的全稱是Java Persistence API, 即Java 持久化API,是SUN公司推出的一套基于ORM的規(guī)范,內(nèi)部是由一系列的接口和抽象類構(gòu)成。JPA通過(guò)JDK 5.0注解描述對(duì)象-關(guān)系表的映射關(guān)系,并將運(yùn)行期的實(shí)體對(duì)象持久化到數(shù)據(jù)庫(kù)中。

  JPA的優(yōu)勢(shì):標(biāo)準(zhǔn)化、容器級(jí)特性的支持、簡(jiǎn)單方便、查詢能力、高級(jí)特性

二、JPA與Hibernate的關(guān)系:

  JPA規(guī)范本質(zhì)上就是一種ORM規(guī)范,注意不是ORM框架——因?yàn)镴PA并未提供ORM實(shí)現(xiàn),它只是制訂了一些規(guī)范,提供了一些編程的API接口,但具體實(shí)現(xiàn)則由服務(wù)廠商來(lái)提供實(shí)現(xiàn)。JPA和Hibernate的關(guān)系就像JDBC和JDBC驅(qū)動(dòng)的關(guān)系,JPA是規(guī)范,Hibernate除了作為ORM框架之外,它也是一種JPA實(shí)現(xiàn)。

 

 

 

 

三、JPA環(huán)境搭建:

1、創(chuàng)建一個(gè)maven工程,在pom.xml中導(dǎo)入對(duì)應(yīng)的坐標(biāo)

 1    <properties>
 2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3         <project.hibernate.version>5.0.7.Final</project.hibernate.version>
 4     </properties>
 5 
 6     <dependencies>
 7         <!-- junit -->
 8         <dependency>
 9             <groupId>junit</groupId>
10             <artifactId>junit</artifactId>
11             <version>4.12</version>
12             <scope>test</scope>
13         </dependency>
14 
15         <!-- hibernate對(duì)jpa的支持包 -->
16         <dependency>
17             <groupId>org.hibernate</groupId>
18             <artifactId>hibernate-entitymanager</artifactId>
19             <version>${project.hibernate.version}</version>
20         </dependency>
21 
22         <!-- c3p0 -->
23         <dependency>
24             <groupId>org.hibernate</groupId>
25             <artifactId>hibernate-c3p0</artifactId>
26             <version>${project.hibernate.version}</version>
27         </dependency>
28 
29         <!-- log日志 -->
30         <dependency>
31             <groupId>log4j</groupId>
32             <artifactId>log4j</artifactId>
33             <version>1.2.17</version>
34         </dependency>
35 
36         <!-- Mysql and MariaDB -->
37         <dependency>
38             <groupId>mysql</groupId>
39             <artifactId>mysql-connector-java</artifactId>
40             <version>5.1.6</version>
41         </dependency>
42     </dependencies>

2、編寫(xiě)實(shí)體類和數(shù)據(jù)表的映射配置,創(chuàng)建實(shí)體類以后,使用對(duì)應(yīng)的注釋配置映射關(guān)系

     @Entity
            作用:指定當(dāng)前類是實(shí)體類。
        @Table
            作用:指定實(shí)體類和表之間的對(duì)應(yīng)關(guān)系。
            屬性:
                name:指定數(shù)據(jù)庫(kù)表的名稱
        @Id
            作用:指定當(dāng)前字段是主鍵。
        @GeneratedValue
            作用:指定主鍵的生成方式。。
            屬性:
                strategy :指定主鍵生成策略。
        @Column
            作用:指定實(shí)體類屬性和數(shù)據(jù)庫(kù)表之間的對(duì)應(yīng)關(guān)系
            屬性:
                name:指定數(shù)據(jù)庫(kù)表的列名稱。
                unique:是否唯一  
                nullable:是否可以為空  
                inserttable:是否可以插入  
                updateable:是否可以更新  
                columnDefinition: 定義建表時(shí)創(chuàng)建此列的DDL  
                secondaryTable: 從表名。如果此列不建在主表上(默認(rèn)建在主表),該屬性定義該列所在從表的名字搭建開(kāi)發(fā)環(huán)境[重點(diǎn)]

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多