


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(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í),千萬注意目錄不能有中文或者空格3.開發(fā)步驟:1.先建表,再編寫配置文件和bean-(面向過程,傳統(tǒng)的數(shù)據(jù)庫建模思想)2.先編寫配置文件和bean,在建表 (OO
2、P 思想 )-要求比較高4.demo 實(shí)例事務(wù)種類:1.本地事務(wù):支持對(duì)同一個(gè)數(shù)據(jù)庫的事務(wù)操作大部分應(yīng)用2.全局事務(wù):支持對(duì)多個(gè)數(shù)據(jù)庫的事務(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)鍵字, 自增,字段名稱的映射修改,表名稱的映射修改 , 字段類型 (Date 類型 )-不同格式要求 ,枚舉類的注釋 (索引,枚舉值 )-性別 ,大文本類型數(shù)據(jù),二進(jìn)制數(shù)據(jù)映射, 不想某個(gè)字段跟表有映射關(guān)系,為了防止某個(gè)字段數(shù)據(jù)量過大而占用存過大因此對(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;枚舉類:java view plain copy print?public enum Sex MAN,WORMAN4.單元測(cè)試類知識(shí)點(diǎn):1.把握異常出現(xiàn)的時(shí)機(jī)。2.通過 ID 得到實(shí)體bean(1.徹底查詢2.用到查詢 )3.保存實(shí)體bean 到數(shù)據(jù)庫4.更新實(shí)體bean 到數(shù)據(jù)庫中涉及到對(duì)象的狀態(tài):1.新建2.托管 (設(shè)置實(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);/ 代理對(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),無法跟數(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();/ 將所有實(shí)體管理器中的所有實(shí)體變成游離狀態(tài),無法跟數(shù)據(jù)庫同步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語句:面向?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.唯一值 (對(duì)象 )查詢類型:普通查詢,刪除查詢,更新查詢ps:進(jìn)行數(shù)據(jù)的更改必須啟動(dòng)事務(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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 數(shù)字化轉(zhuǎn)型在提升中小企業(yè)效能中的重要作用
- 銀行業(yè)務(wù)模式創(chuàng)新對(duì)創(chuàng)業(yè)企業(yè)的影響
- 旅游+游戲模式對(duì)客戶體驗(yàn)的深遠(yuǎn)影響
- 鄉(xiāng)村教育家精神的實(shí)踐路徑與未來發(fā)展趨勢(shì)
- 2025年醫(yī)保支付改革對(duì)醫(yī)療行業(yè)監(jiān)管體系的影響報(bào)告
- 2025年養(yǎng)老機(jī)構(gòu)醫(yī)養(yǎng)結(jié)合模式下的護(hù)理人才培養(yǎng)策略報(bào)告
- 2025年養(yǎng)老服務(wù)體系建設(shè)社會(huì)穩(wěn)定風(fēng)險(xiǎn)評(píng)估報(bào)告
- 2025年休閑食品市場(chǎng)拓展健康化轉(zhuǎn)型下的產(chǎn)品創(chuàng)新與市場(chǎng)定位報(bào)告
- 2025年休閑農(nóng)業(yè)與鄉(xiāng)村旅游鄉(xiāng)村旅游產(chǎn)業(yè)信息化建設(shè)與應(yīng)用報(bào)告
- 2025年休閑農(nóng)業(yè)與鄉(xiāng)村旅游融合發(fā)展:文化創(chuàng)意產(chǎn)業(yè)融合創(chuàng)新報(bào)告
- 2025年貴州六盤水市燃?xì)饧瘓F(tuán)六盤水燃?xì)庥邢薰菊衅腹P試參考題庫含答案解析
- 妊娠期子宮蛻膜息肉診治中國專家共識(shí)(2024年版)解讀課件
- 病毒性心肌炎病例分析與治療
- 幼兒園教育懲戒的邊界與藝術(shù)
- 交通安全與一氧化碳安全教育
- 2025年出版:全球市場(chǎng)工程機(jī)械多路換向閥總體規(guī)模、主要生產(chǎn)商、主要地區(qū)、產(chǎn)品和應(yīng)用細(xì)分調(diào)研報(bào)告
- 桶裝飲用水質(zhì)量檢查報(bào)告
- 寵物托運(yùn)協(xié)議合同書
- 《2024 3610-T-339 可配置汽車信息娛樂服務(wù) 第 2 部分:要求》知識(shí)培訓(xùn)
- 2023年浙江省杭州市建德市國有糧食收儲(chǔ)有限公司公開招聘工作人員8人筆試參考題庫附帶答案詳解
- 科學(xué)上海會(huì)考試卷及答案
評(píng)論
0/150
提交評(píng)論