日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JPA(二):HellWord工程

發布時間:2025/3/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JPA(二):HellWord工程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用JPA持久化對象的操作步驟:

1)創建persistence.xml,在這個文件中配置持久化單元:

--- 需要指定跟哪個數據庫進行交互;

--- 需要指定JPA使用哪個持久化的框架以及配置該框架的基本屬性。

2)創建實體類,使用annotation來描述實體類跟數據庫表之間的映射關系。

3)使用JPA API完成數據增加、刪除、修改和查詢擦操作:

--- 創建EntityManagerFactory(對應hibernate中的SessionFactory)

--- 創建EntityManager(對應hibernate中的Session)

4)JPA規范要求在類路徑的MATA-INF目錄下放置persistence.xml,文件的名字是固定的。

演示HelloWord工程:

1)創建jpa project

之后點擊“Finish”按鈕完成工程創建。

工程創建好后,會發現在src下的META-INF文件夾下包含一個persistence.xml文件,該文件是用來配置jpa相關信息的。

2)導入依賴包:

a、從hibernate官網下載hibernate開發包:hibernate-release-5.3.0.Final.zip,把解壓后的文件中的數據拷貝到工程下新建的lib文件夾中:

備注:把上圖中選中的兩個文件夾下的所有包拷貝到lib中

b、導入mysql驅動包:

c、右鍵lib文件夾下所有jar文件,彈出菜單中選擇Build Path-> Add to Build Path

3)在src下添加Person實體類

package com.dxsoft.jpa.helloword;import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;@Entity @Table(name = "jpa_person") public class Person {private Integer id;private String fullName;private int age;public Person() {}@GeneratedValue(strategy = GenerationType.AUTO)@Idpublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Column(name = "full_name")public String getFullName() {return fullName;}public void setFullName(String fullName) {this.fullName = fullName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person [id=" + id + ", fullName=" + fullName + ", age=" + age + "]";}}

備注:

1)@Id注解為唯一主鍵;

2)@GeneratedValue(strategy = GenerationType.AUTO)注解自增,并制定自增方式。JPA提供了四種主鍵生成策略,其被定義在枚舉類GenerationType中,包括GenerationType.TABLE,GenerationType.SEQUENCE,GenerationType.IDENTITY和GenerationType.AUTO;

3)@Column(name="")可以重新定義數據庫對應字段的名字,如果類中定義屬性字段和數據不一樣時需要定義,否則可省略。

4)修改persistence.xml配置內容

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0"xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"><persistence-unit name="Jpa-helloword"transaction-type="RESOURCE_LOCAL"><!-- 配置使用什么 ORM 產品來作為 JPA 的實現 --><provider>org.hibernate.jpa.HibernatePersistenceProvider</provider><!-- 添加持久化類 --><class>com.dxsoft.jpa.helloword.Person</class><properties><!-- 數據庫的相關配置 --><property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /><property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/jpa" /><property name="javax.persistence.jdbc.user" value="root" /><property name="javax.persistence.jdbc.password" value="root" /><!-- 指定方言 MySQL org.hibernate.dialect.MySQLDialectMySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialectMySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialectMySQL5 org.hibernate.dialect.MySQL5DialectMySQL5 with InnoDB org.hibernate.dialect.MySQL5InnoDBDialect--><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /><property name="hibernate.show_sql" value="true" /><property name="hibernate.format_sql" value="true" /><!-- create :每次加載hibernate時都會刪除上一次的生成的表,然后根據你的model類再重新來生成新表,哪怕兩次沒有任何改變也要這樣執行,這就是導致數據庫表數據丟失的一個重要原因。<br>create-drop :每次加載hibernate時根據model類生成表,但是sessionFactory一關閉,表就自動刪除。<br>update :最常用的屬性,第一次加載hibernate時根據model類會自動建立起表的結構(前提是先建立好數據庫),以后加載hibernate時根據 model類自動更新表結構,即使表結構改變了但表中的行仍然存在不會刪除以前的行。要注意的是當部署到服務器后,表結構是不會被馬上建立起來的,是要等 應用第一次運行起來后才會。<br>validate :每次加載hibernate時,驗證創建數據庫表結構,只會和數據庫中的表進行比較,不會創建新表,但是會插入新值。 <br> --><property name="hibernate.hbm2ddl.auto" value="update" /> </properties></persistence-unit> </persistence>

5)添加測試類:

package com.dxsoft.jpa.helloword;import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence;public class Client {public static void main(String[] args) {// 1.創建EntityManagerFactoryString persistenceUnitName = "Jpa-helloword";EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);// 2.創建EntityManagerEntityManager entityManager = entityManagerFactory.createEntityManager();// 3.開始事務EntityTransaction entityTransaction = entityManager.getTransaction();entityTransaction.begin();// 4.進行持久化操作Person person = new Person();person.setAge(31);person.setFullName("tommmy duan");entityManager.persist(person);// 5.提交事務 entityTransaction.commit();// 6.關閉EntityManager entityManager.close();// 7.關閉EnityManagerFactory entityManagerFactory.close();System.out.println("complete..");} }

執行打印信息如下:

INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@1950e8a6] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. Hibernate: create table hibernate_sequence (next_val bigint) engine=InnoDB Hibernate: insert into hibernate_sequence values ( 1 ) Hibernate: create table jpa_person (id integer not null,age integer not null,full_name varchar(255),primary key (id)) engine=InnoDB Wed May 23 15:41:34 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. Hibernate: selectnext_val as id_val fromhibernate_sequence for updateHibernate: updatehibernate_sequence setnext_val= ? wherenext_val=? Hibernate: insert intojpa_person(age, full_name, id) values(?, ?, ?) 五月 23, 2018 3:41:34 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://127.0.0.1:3306/jpa] complete..

?

轉載于:https://www.cnblogs.com/yy3b2007com/p/9078123.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的JPA(二):HellWord工程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。