JPA入門
1.JPA認識
1.1什么是JPA
JPA(Java Persistence API)是Sun官方提出的Java持久化規范。它為Java開發人員提供了一種對象/關系映射工具來管理Java應用中的關系數據他的作用是簡化對mysql的操作
1.2JQP的優缺點
優點: 1.JPA的主要目標之一就是提供更加簡單的編程模型
2.JPA擁有可媲美JDBC的查詢能力
3.JPA的查詢語言是面向對象而非面向數據庫的,它以面向對象的自然語法構造查詢語句
缺點: 1.不能干預sql語句的生成
2.一個項目中,如果對sql語句的優化要求比較高,不適合用JPA
3.如果一張表的數據非常大的時候也不能用JPA
1.2 ORM :ORM框架采用元數據來描述對象一關系映射細節,元數據一般采用XML格式,並且存放在專門的對象一映射文件中。只要提供了持久化類與表的映射關系,ORM框架在運行時就能參照映射文件的信息,把對象持久化到數據庫中
1.3JPA的配置
1.首先導入依賴的包
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>wjx</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- hibernate的包 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
<!-- hibernate對於jpa的支持包 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
<!-- mysql的驅動包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- junit的測試包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<finalName>pss</finalName>
<plugins>
<plugin>
<!-- Maven的編譯插件-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后Ctrl +Shift+s大開頁面配置JPA
點擊加號在下面找到JPA文件
然后在點擊生成的JPA里面的加號找到加號直接配置生成文件 拖到你 工程里面
放在resource里面
在進行一下配置框起來的地方改成你數據庫的名字
1.4第一個JPA程序
1.1創建一個實體類對象
給於字段提供get set方法
提供一個測試類進行測試
然后就是完成一個用JPA對數據庫的CRUD(分層domain dao )
1.6 一級緩存和二級緩存
他就只會查詢一次因為第二次其實已經在一級緩存中了
但是,
Customer customer1 = entityManager.find(Customer.class, 1);
//開啟事務
transaction.begin();
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
//提交事務
transaction.commit();
//關閉事務
entityManager.close();
Customer customer2 = entityManager.find(Customer.class, 1);
這樣他就回打印兩次
二級緩存,就是要在不同的entityManager中,只查詢一次
1.7單表映射配置細節
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id; //要求:主鍵,使用數據庫的自動生成策略
@Column(length = 20,nullable = false,unique = true,name ="username")
private String name; //要求:varchar(20),數據庫名稱為username,不能為空,唯一
private String password;
@Column(insertable = false,columnDefinition = "int(10) default(25)")
private Integer age = 25; //要求:默認值是25,在插入數據時不允許覆蓋(添加數據時不操作該字段)
private Boolean sex;// 數據庫沒有布爾類型,bit對象
@Column(columnDefinition = "decimal (19,2)")
private BigDecimal salary;// 19,2
@Column(updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;//包含年月日時分秒,不允許修改
@Temporal(TemporalType.DATE)
private Date birthday;//包含年月日
@Temporal(TemporalType.TIME)
private Date time;//包含時分秒
@Lob
private String text;//這是一個大文本
@Transient
private String temp;//這一個字段不要同步到數據庫