原創|使用教程|編輯:龔雪|2018-03-16 10:47:05.000|閱讀 247 次
概述:本教程介紹了MyEclipse中的一些基于JPA的功能。 閱讀本教程時,了解JPA和實體映射如何與注釋一起工作的基本概念將會很有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
MyEclipse 3.15 Style——在線購買低至75折!
【】
本教程介紹了MyEclipse中的一些基于PA的功能。 閱讀本教程時,了解JPA和實體映射如何與注釋一起工作的基本概念將會很有幫助。 在本教程中,您將學習如何:
持續時間:30分鐘
沒有MyEclipse?
由于MyEclipse生成了大量的代碼,因此您可以快速專注于編寫“業務邏輯”,或者更具體地說,“實際執行的代碼”。在本節中,您將編寫一個帶有main方法的Java類,該方法將Productline插入到數據庫中,檢索并更新它,然后刪除。使用這段代碼,您應該能夠看到在自己的應用程序中使用JPA實體是多么容易,而無需編寫JDBC代碼或任何其他持久性代碼。
1. 右鍵單擊com.myeclipseide.jpa包,然后選擇New>Class。
2. 在Name字段中輸入RunJPA,選擇Public static void main復選框,然后單擊Finish。
在生成新的類和main方法后,您需要編寫代碼來成功處理Productline的實例。
注意:下面的代碼看起來很長很復雜,但這是因為我們試圖在一個代碼塊中顯示四個不同的示例。 如果您查看每個操作(保存,加載,更新,刪除),它們都不會包含多行代碼。
3. 將以下代碼添加到main方法中,然后按Ctrl + S進行保存。
/* 1. Create a reference to our ID */注意: 將事務數據庫的代碼片段換成事務是一個好主意,所以如果操作失敗(例如DB崩潰),那么試圖在事務中發生的所有更改都會回滾到它們的原始值,而不是只有一半 工作完成。
String productLineID = "Men's Shoes";
/* 2. Create a new Productline instance */
Productline newProductline = new Productline(
productLineID,
"Shoes for men.", "<strong>Men's Shoes</strong>", null);
/* 3. Create a DAO instance to use */
ProductlineDAO dao = new ProductlineDAO();
/* 4. Store our new product line in the DB */
EntityManagerHelper.beginTransaction();
dao.save(newProductline);
EntityManagerHelper.commit();
/* 5. Now retrieve the new product line, using the ID we created */
Productline loadedProductline = dao.findById(productLineID);
/* 6. Print out the product line information */
System.out.println("*NEW* Product Line [productLine="
+ loadedProductline.getProductline() + ", textDescription="
+ loadedProductline.getTextdescription() + ", image="
+ loadedProductline.getImage() + "]");
/* * 7. Now let's change same value on the product line, and save the
* change
*/
loadedProductline.setTextdescription("Product line for men's shoes.");
EntityManagerHelper.beginTransaction();
dao.save(loadedProductline);
EntityManagerHelper.commit();
/*
* 8. Now let's load the product line from the DB again, and make sure
* it text description changed
*/
Productline secondLoadedProductline = dao.findById(productLineID); System.out.println("*REVISED* Product Line ["
+ "productLine=" + secondLoadedProductline.getProductline()
+ ", textDescription=" + secondLoadedProductline.getTextdescription()
+ ", image=" + secondLoadedProductline.getImage() + "]");
/* 9. Now let's delete the product line from the DB */
EntityManagerHelper.beginTransaction();
dao.delete(secondLoadedProductline);
EntityManagerHelper.commit();
/* * 10. To confirm the deletion, try and load it again and make sure it
* fails
*/
Productline deletedProductline = dao.findById(productLineID);
/*
* We use a simple inlined IF clause to test for null and print
* SUCCESSFUL/FAILED
*/
System.out.println("Productline deletion: "
+ (deletedProductline == null ? "SUCCESSFUL" : "FAILED"));
上面的代碼看起來令人望而生畏,但它背靠背做了很多簡單的事情。 例如,如果您只想將新項目存儲在數據庫中,則只需要程序中步驟1-3的代碼,這些代碼將三行代碼相減(減去注釋)即可。 以下是每個編號部分的細目:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網