


版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、.JPA實(shí)體 bean 配置, jpa 增刪改 api, jpasql 增刪改1.ORM 框架必然發(fā)展趨勢(shì):jdbc-hibernate( 是產(chǎn)品,實(shí)現(xiàn)jpa 規(guī) )-jpa(是規(guī),不是產(chǎn)品)。ps:運(yùn)用 jpa 規(guī)的 API 進(jìn)行編程,不對(duì)Hiberbate , topLink 等 orm 框架構(gòu)成威脅。2.JPA環(huán)境搭建 hibernate-distribution-3.6.10.Final1.準(zhǔn)備 lib 包2.jar 包引入時(shí),千萬(wàn)注意目錄不能有中文或者空格3.開(kāi)發(fā)步驟:1.先建表,再編寫(xiě)配置文件和bean-(面向過(guò)程,傳統(tǒng)的數(shù)據(jù)庫(kù)建模思想)2.先編寫(xiě)配置文件和bean,在建表 (OO
2、P 思想 )-要求比較高4.demo 實(shí)例事務(wù)種類(lèi):1.本地事務(wù):支持對(duì)同一個(gè)數(shù)據(jù)庫(kù)的事務(wù)操作大部分應(yīng)用2.全局事務(wù):支持對(duì)多個(gè)數(shù)據(jù)庫(kù)的事務(wù)操作(銀行轉(zhuǎn)賬 )-兩次提交協(xié)議步驟:第一步:項(xiàng)目結(jié)構(gòu)2.持久化文件配置:html view plain copy print? .3.實(shí)體 bean知識(shí)點(diǎn): 字段的長(zhǎng)度, 是否為空, 關(guān)鍵字, 自增,字段名稱(chēng)的映射修改,表名稱(chēng)的映射修改 , 字段類(lèi)型 (Date 類(lèi)型 )-不同格式要求 ,枚舉類(lèi)的注釋 (索引,枚舉值 )-性別 ,大文本類(lèi)型數(shù)據(jù),二進(jìn)制數(shù)據(jù)映射, 不想某個(gè)字段跟表有映射關(guān)系,為了防止某個(gè)字段數(shù)據(jù)量過(guò)大而占用存過(guò)大因此對(duì)其進(jìn)行延遲加載(懶惰
3、加載,需要獲取數(shù)據(jù)時(shí)才得到數(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)文件很大時(shí),進(jìn)行懶惰加載 public Byte getFile() return file;/* param file the file to set*/public void setFile(Byte file) this.file = file;./* return the other*/Transient / 排除某個(gè)字段的映射public String getOther() return other;/* param other the other to set*/public void setOther(St
10、ring other) this.other = other;枚舉類(lèi):java view plain copy print?public enum Sex MAN,WORMAN4.單元測(cè)試類(lèi)知識(shí)點(diǎn):1.把握異常出現(xiàn)的時(shí)機(jī)。2.通過(guò) ID 得到實(shí)體bean(1.徹底查詢2.用到查詢 )3.保存實(shí)體bean 到數(shù)據(jù)庫(kù)4.更新實(shí)體bean 到數(shù)據(jù)庫(kù)中涉及到對(duì)象的狀態(tài):1.新建2.托管 (設(shè)置實(shí)體的字段值,并通過(guò)提交可以同步到數(shù)據(jù)庫(kù))3.游離 (無(wú)法更新到數(shù)據(jù)庫(kù)中,除非使用merge 方法重新可將其更新到數(shù)據(jù)庫(kù)中)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);/ 代理對(duì)象,用到才查詢System.out.println(p.getName();em.getTransaction().commit();em.close();/S
14、ystem.out.println(p.getName();出錯(cuò),事務(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();/ 將所有實(shí)體管理器中的所有實(shí)體變成游離狀態(tài),無(wú)法跟數(shù)據(jù)庫(kù)同步
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();/ 將所有實(shí)體管理器中的所有實(shí)體變成游離狀態(tài),無(wú)法跟數(shù)據(jù)庫(kù)同步p.setName(techbirds);em.merge(p);/ 此時(shí)又可以進(jìn)行同步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語(yǔ)句:面向?qū)ο蟮膕ql 語(yǔ)句, jpa 標(biāo)準(zhǔn)的 sql 語(yǔ)法查詢方法: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.唯一值 (對(duì)象 )查詢類(lèi)型:普通查詢,刪除查詢,更新查詢ps:進(jìn)行數(shù)據(jù)的更改必須啟動(dòng)事務(wù)。-刪除查詢和更新查詢必須開(kāi)啟事務(wù)java view plain copy print?Testpublic void querysql()EntityManagerFactory factory=Persistence.createEntityManagerFactory(MyJpa); EntityManager em=factory.createEntityManager();./ 面向?qū)ο蟮?sql 語(yǔ)句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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 公司撕名牌策劃方案
- 公司日常打卡小活動(dòng)方案
- 公司組織哪些活動(dòng)方案
- 公司美食節(jié)活動(dòng)策劃方案
- 公司沙龍如何做活動(dòng)方案
- 公司節(jié)能減排策劃方案
- 公司整年團(tuán)建活動(dòng)方案
- 公司消費(fèi)扶貧活動(dòng)方案
- 公司職工瑜伽活動(dòng)方案
- 公司環(huán)保創(chuàng)新活動(dòng)方案
- 荊州中學(xué)2024-2025學(xué)年高二下學(xué)期6月月考?xì)v史試題答案
- 外科換藥拆線技術(shù)規(guī)范
- 2025年中考考前最后一卷化學(xué)(武漢卷)(全解全析)
- 2026屆高考語(yǔ)文復(fù)習(xí):直擊2025年語(yǔ)文高考閱讀客觀題關(guān)鍵詞比對(duì)
- 江西中考語(yǔ)文試題及答案
- 公司收購(gòu)公司部分股權(quán)之可行性研究報(bào)告
- 曲靖一中2025屆高考決勝全真模擬卷(二)化學(xué)試題及答案
- T/CHES 43-2020水利水電工程白蟻實(shí)時(shí)自動(dòng)化監(jiān)測(cè)預(yù)警系統(tǒng)技術(shù)規(guī)范
- 稅務(wù)籌劃與稅務(wù)咨詢常年服務(wù)協(xié)議
- 學(xué)習(xí)給復(fù)旦大學(xué)建校120周年賀信心得體會(huì)
評(píng)論
0/150
提交評(píng)論