


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、.JPA實體 bean 配置, jpa 增刪改 api, jpasql 增刪改1.ORM 框架必然發(fā)展趨勢:jdbc-hibernate( 是產(chǎn)品,實現(xiàn)jpa 規(guī) )-jpa(是規(guī),不是產(chǎn)品)。ps:運用 jpa 規(guī)的 API 進行編程,不對Hiberbate , topLink 等 orm 框架構(gòu)成威脅。2.JPA環(huán)境搭建 hibernate-distribution-3.6.10.Final1.準(zhǔn)備 lib 包2.jar 包引入時,千萬注意目錄不能有中文或者空格3.開發(fā)步驟:1.先建表,再編寫配置文件和bean-(面向過程,傳統(tǒng)的數(shù)據(jù)庫建模思想)2.先編寫配置文件和bean,在建表 (OO
2、P 思想 )-要求比較高4.demo 實例事務(wù)種類:1.本地事務(wù):支持對同一個數(shù)據(jù)庫的事務(wù)操作大部分應(yīng)用2.全局事務(wù):支持對多個數(shù)據(jù)庫的事務(wù)操作(銀行轉(zhuǎn)賬 )-兩次提交協(xié)議步驟:第一步:項目結(jié)構(gòu)2.持久化文件配置:html view plain copy print? .3.實體 bean知識點: 字段的長度, 是否為空, 關(guān)鍵字, 自增,字段名稱的映射修改,表名稱的映射修改 , 字段類型 (Date 類型 )-不同格式要求 ,枚舉類的注釋 (索引,枚舉值 )-性別 ,大文本類型數(shù)據(jù),二進制數(shù)據(jù)映射, 不想某個字段跟表有映射關(guān)系,為了防止某個字段數(shù)據(jù)量過大而占用存過大因此對其進行延遲加載(懶惰
3、加載,需要獲取數(shù)據(jù)時才得到數(shù)據(jù))。java view plain copy print?import java.util.Date;import javax.persistence.Basic;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.EnumType;import javax.persistence.Enumerated;import javax.persistence.FetchType;import javax.persistence.Generated
4、Value;import javax.persistence.Id;import javax.persistence.Lob;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import javax.persistence.Transient;EntityTable(name=person)public class Person private Integer id;private String name;private Date bir
5、thday;private Sex sex;private String info;private Byte file;private String other;public Person() super();public Person(String name) super(); = name;public Person(String name, Date birthday) super(); = name;.this.birthday = birthday;public Person(String name, Date birthday, Sex sex)
6、 super(); = name;this.birthday = birthday;this.sex = sex;/* 主鍵并自增* return the id*/Id GeneratedValuepublic Integer getId() return id;/* param id the id to set*/public void setId(Integer id) this.id = id;/* return the name*/Column(length=10,nullable=false,name=personName)public String getName
7、() return name;/* param name the name to set*/public void setName(String name) = name;/* return the birthday*/Temporal(TemporalType.DATE)public Date getBirthday() return birthday;/*.* param birthday the birthday to set*/public void setBirthday(Date birthday) this.birthday = birthday;/* ret
8、urn the sex*/Enumerated(EnumType.STRING)public Sex getSex() return sex;/* param sex the sex to set*/public void setSex(Sex sex) this.sex = sex;/* return the info*/Lobpublic String getInfo() return info;/* param info the info to set*/public void setInfo(String info) = info;/* return the fil
9、e*/Lob Basic(fetch=FetchType.LAZY) /當(dāng)文件很大時,進行懶惰加載 public Byte getFile() return file;/* param file the file to set*/public void setFile(Byte file) this.file = file;./* return the other*/Transient / 排除某個字段的映射public String getOther() return other;/* param other the other to set*/public void setOther(St
10、ring other) this.other = other;枚舉類:java view plain copy print?public enum Sex MAN,WORMAN4.單元測試類知識點:1.把握異常出現(xiàn)的時機。2.通過 ID 得到實體bean(1.徹底查詢2.用到查詢 )3.保存實體bean 到數(shù)據(jù)庫4.更新實體bean 到數(shù)據(jù)庫中涉及到對象的狀態(tài):1.新建2.托管 (設(shè)置實體的字段值,并通過提交可以同步到數(shù)據(jù)庫)3.游離 (無法更新到數(shù)據(jù)庫中,除非使用merge 方法重新可將其更新到數(shù)據(jù)庫中)4.刪除java view plain copy print?public class
11、PersonTest Testpublic void save()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa); EntityManager em=factory.createEntityManager(); em.getTransaction().begin();em.persist(new Person(techbirds,new Date(),Sex.MAN);em.getTransaction().commit();em.close();factory.close();.Testpu
12、blic void getPerson1()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa);EntityManager em=factory.createEntityManager();em.getTransaction().begin();Person p=em.find(Person.class, 1);em.getTransaction().commit();em.close();factory.close();System.out.println(p.getName();Testpub
13、lic void getPerson2()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa); EntityManager em=factory.createEntityManager(); em.getTransaction().begin();Person p=em.getReference(Person.class, 1);/ 代理對象,用到才查詢System.out.println(p.getName();em.getTransaction().commit();em.close();/S
14、ystem.out.println(p.getName();出錯,事務(wù)已經(jīng)關(guān)閉factory.close();Testpublic void updatePerson1()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa); EntityManager em=factory.createEntityManager(); em.getTransaction().begin();Person p=em.find(Person.class, 1);p.setName(bao);em.getTransac
15、tion().commit();em.close();factory.close();Testpublic void updatePerson2()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa);EntityManager em=factory.createEntityManager();em.getTransaction().begin();Person p=em.find(Person.class, 1);em.clear();/ 將所有實體管理器中的所有實體變成游離狀態(tài),無法跟數(shù)據(jù)庫同步
16、.p.setName(techbirds);em.getTransaction().commit();em.close();factory.close();public void updatePerson3()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa); EntityManager em=factory.createEntityManager();em.getTran .wang027. saction().begin();Person p=em.find(Person.class, 1)
17、;em.clear();/ 將所有實體管理器中的所有實體變成游離狀態(tài),無法跟數(shù)據(jù)庫同步p.setName(techbirds);em.merge(p);/ 此時又可以進行同步em.getTransaction().commit();em.close();factory.close();Testpublic void delPerson()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa); EntityManager em=factory.createEntityManager(); em.get
18、Transaction().begin();Person p=em.find(Person.class, 1);em.remove(p);em.getTransaction().commit();em.close();factory.close();5.jpa 的 (sql)查詢jpaSQL語句:面向?qū)ο蟮膕ql 語句, jpa 標(biāo)準(zhǔn)的 sql 語法查詢方法:1.位參數(shù)查詢select o from Person o where o.id=?1 query.setParameter(1,2);2.命名查詢select o from Person o where o.id=:id query.s
19、etParameter(id,2);查詢結(jié)果: 1.列表2.唯一值 (對象 )查詢類型:普通查詢,刪除查詢,更新查詢ps:進行數(shù)據(jù)的更改必須啟動事務(wù)。-刪除查詢和更新查詢必須開啟事務(wù)java view plain copy print?Testpublic void querysql()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa); EntityManager em=factory.createEntityManager();./ 面向?qū)ο蟮?sql 語句Query query=em.createQuery(select o from Person o where o.id=?); query.setParameter(1, 2);Person p=(Person) query.getSingleResult();System.out.println(p.getName();em.close();factory.close();Testpublic void deletesql()EntityManagerFactory factory=Persistence.createEntityManagerFactory(M
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 江西省南昌市三校聯(lián)考2025屆化學(xué)高一下期末達標(biāo)檢測模擬試題含解析
- 2025屆河北正定弘文中學(xué)高一化學(xué)第二學(xué)期期末考試模擬試題含解析
- 農(nóng)技項目資金管理辦法
- 公寓用品檔案管理辦法
- 公共收益處置管理辦法
- 民政救濟專戶管理辦法
- 出口毛巾加工管理辦法
- 視覺識別技術(shù)在串番茄采摘機器人設(shè)計與試驗中的應(yīng)用
- 十堰市總承包管理辦法
- 變電站設(shè)計與施工指導(dǎo)手冊
- 無創(chuàng)眶周抗衰規(guī)范
- 暑假假期安全教育(課件)-小學(xué)生主題班會
- 2024年1月黑龍江高中學(xué)業(yè)水平合格考政治試卷真題(含答案詳解)
- 供應(yīng)室護理進修匯報總結(jié)
- 儲糧害蟲與技術(shù)和化學(xué)防治
- 自適應(yīng)前照燈控制系統(tǒng)
- 電梯招標(biāo)文件示范文本
- 上海市安裝工程預(yù)算定額(2000)工程量計算規(guī)則
- 街道、鎮(zhèn)、區(qū)道路保潔及垃圾轉(zhuǎn)運服務(wù)采購項目服務(wù)方案(投標(biāo)方案)
- GB/T 16886.10-2024醫(yī)療器械生物學(xué)評價第10部分:皮膚致敏試驗
- 醫(yī)院感染管理制度制度匯編
評論
0/150
提交評論