如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx

上传人:b****9 文档编号:26103634 上传时间:2023-06-17 格式:DOCX 页数:15 大小:176.93KB
下载 相关 举报
如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx_第1页
第1页 / 共15页
如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx_第2页
第2页 / 共15页
如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx_第3页
第3页 / 共15页
如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx_第4页
第4页 / 共15页
如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx

《如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx》由会员分享,可在线阅读,更多相关《如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx(15页珍藏版)》请在冰豆网上搜索。

如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分.docx

如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例第三部分

如何在MyEclipse开发工具中构建基于Hibenate框架的ORM应用系统实例(第三部分)

1.1.1创建PO类和编程DAO类的功能实现程序代码

1、创建出PO类examples.Book

下面为其代码,并对代码加以修改---请见黑体部分的程序代码

packageexamples;

publicclassBookimplementsjava.io.Serializable{

privateStringid;

privateStringbookName;

privateStringbookKind;

privatefloatbookPrice;

publicBook(){

}

publicBook(StringbookName,StringbookKind,floatbookPrice){

this.bookName=bookName;

this.bookKind=bookKind;

this.bookPrice=bookPrice;

}

publicStringgetId(){

returnthis.id;

}

privatevoidsetId(Stringid){

this.id=id;

}

publicStringgetBookName(){

returnthis.bookName;

}

publicvoidsetBookName(StringbookName){

this.bookName=bookName;

}

publicStringgetBookKind(){

returnthis.bookKind;

}

publicvoidsetBookKind(StringbookKind){

this.bookKind=bookKind;

}

publicfloatgetBookPrice(){

returnthis.bookPrice;

}

publicvoidsetBookPrice(floatbookPrice){

this.bookPrice=bookPrice;

}

publicbooleanequals(Objectother)//该方法为对对象进行比较判断用

{

if(this==other)

{

returntrue;

}

if(!

(otherinstanceofBook))

{

returnfalse;

}

returntrue;

}

}

注意:

上面的黑体是我们修改的部分

2、同时MyEclipser也对hibernate.cfg.xml文件进行修改

(1)内容为的下面的内容---请见黑体部分。

xmlversion='1.0'encoding='UTF-8'?

>

DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/HibernateConfigurationDTD3.0//EN"

"

--GeneratedbyMyEclipseHibernateTools.-->

sa

jdbc:

microsoft:

sqlserver:

//localhost:

1433;DatabaseName=DataBaseDB

org.hibernate.dialect.SQLServerDialect

1234

com.microsoft.jdbc.sqlserver.SQLServerDriver

true

(2)这可以通过其可视化界面来看到。

注意:

编译后,将自动将前面的各个*.xml拷贝到WEB-INF/classes所在的目录下。

3、编程DAO程序类UserHibernateBean的功能实现程序代码

(1)添加一个DAO组件类UserHibernateBean,包名称为examples

(2)编程该DAO类,该UserHibernateBean程序类的代码示例

packageexamples;

importorg.hibernate.*;

importorg.hibernate.cfg.*;

importjava.util.*;

importmons.logging.*;

publicclassUserHibernateBean

{

Sessionsession=null;

Transactiontx=null;

privatestaticLoglog=LogFactory.getLog(UserHibernateBean.class);

publicUserHibernateBean()

{

}

publicArrayListdoSelectBookDataFromDB(StringselectSQL)throws

HibernateException

{

ArrayListtotalBookList=newArrayList();

try

{

session=HibernateUtil.currentSession();//

(1)

tx=session.beginTransaction();

Listresult=session.createQuery(selectSQL).list();

for(intindex=0;index

{

BookoneBook=(Book)result.get(index);

totalBookList.add(oneBook);

}

mit();

}

catch(HibernateExceptionhe)

{

log.error("在doSelectBookDataFromDB方法中出现了HibernateException错误",he);

throwhe;

}

finally

{

/*

由于由Hibernate抛出的异常都视为不可以恢复的,因此应该确保在finally代码块中调用close()方法,以关闭掉Session。

*/

HibernateUtil.closeSession();

}

returntotalBookList;

}

publicbooleandoInsertBookDataToDB()throwsHibernateException,

java.io.UnsupportedEncodingException

{

try

{

session=HibernateUtil.currentSession();

/*

下面的代码相当于我们执行了以下SQL语句

insertintoBook(book_id,bookName,bookKind,bookPricfe)values(X,'J2EE应用开发','1',7.4f)

*/

tx=session.beginTransaction();

BookoneBook=newBook();

/*

对某些数据库系统如MSSQLServer2000必须进行字符的编码转换,否则会出现中文乱码

*/

oneBook.setBookName(newString("J2EE应用开发".getBytes("gb2312"),"ISO8859-1"));

oneBook.setBookKind('1');

oneBook.setBookPrice(7.4f);

session.save(oneBook);//保存该实体

mit();

}

catch(HibernateExceptionhe)

{

if(tx!

=null)

{

tx.rollback();

}

log.error("在doInsertBookDataToDB方法中出现了HibernateException错误",he);

throwhe;

}

catch(java.io.UnsupportedEncodingExceptionex)

{

log.error("在doInsertBookDataToDB方法中出现了UnsupportedEncodingException错误",ex);

throwex;

}

finally

{

/*

这样你就可以随心所欲的多次调用HibernateUtil.currentSession();,你每次都会得到同一个当前线程的Session。

不管是在你的servlet代码中,或者在servletfilter中还是在HTTP结果返回之前,你都必须确保这个Session在

你的数据库访问工作完成后关闭。

*/

HibernateUtil.closeSession();

}

returntrue;

}

publicbooleandoUpdateBookDataToDB(StringbookID)throwsHibernateException,java.io.UnsupportedEncodingException

{

try

{

session=HibernateUtil.currentSession();

tx=session.beginTransaction();

BookoneBook=(Book)session.load(Book.class,bookID);

/*

对某些数据库系统如MSSQLServer2000必须进行字符的编码转换,否则会出现中文乱码

*/

oneBook.setBookName(newString("Java应用开发".getBytes("gb2312"),"ISO8859-1"));

oneBook.setBookKind('1');

oneBook.setBookPrice(10.4f);

mit();

}

catch(HibernateExceptionhe)

{

if(tx!

=null)

{

tx.rollback();

}

log.error("在doUpdateBookDataToDB方法中出现了HibernateException错误",he);

throwhe;

}

catch(java.io.UnsupportedEncodingExceptionex)

{

log.error("在doUpdateBookDataToDB方法中出现了UnsupportedEncodingException错误",ex);

throwex;

}

finally

{

/*

这样你就可以随心所欲的多次调用HibernateUtil.currentSession();,你每次都会得到同一个当前线程的Session。

不管是在你的servlet代码中,或者在servletfilter中还是在HTTP结果返回之前,你都必须确保这个Session在

你的数据库访问工作完成后关闭。

*/

HibernateUtil.closeSession();

}

returntrue;

}

publicbooleandodeleteBookDataToDB(StringbookID)throwsHibernateException

{

try

{

session=HibernateUtil.currentSession();

tx=session.beginTransaction();

BookoneBook=(Book)session.load(Book.class,bookID);

session.delete(oneBook);

mit();

}

catch(HibernateExceptionhe)

{

if(tx!

=null)

{

tx.rollback();

}

log.error("在doUpdateBookDataToDB方法中出现了HibernateException错误",he);

throwhe;

}

finally

{

/*

这样你就可以随心所欲的多次调用HibernateUtil.currentSession();,你每次都会得到同一个当前线程的Session。

不管是在你的servlet代码中,或者在servletfilter中还是在HTTP结果返回之前,你都必须确保这个Session在

你的数据库访问工作完成后关闭。

*/

HibernateUtil.closeSession();

}

returntrue;

}

}

4、编程测试的主程序类J2SEAppORMapping的代码

packageexamples;

publicclassJ2SEAppORMapping

{

booleanpackFrame=false;

publicJ2SEAppORMapping()throwsjava.io.UnsupportedEncodingException

{

StringbookID=null;

UserHibernateBeanuserHibernateBean=newUserHibernateBean();

userHibernateBean.doInsertBookDataToDB();

java.util.ArrayListtotalBookList=userHibernateBean.

doSelectBookDataFromDB("fromBook");

for(intindex=0;index

{

examples.BookoneBook=(examples.Book)totalBookList.get(index);

bookID=oneBook.getId();

System.out.print("BookID="+bookID+"\t");

System.out.print("BookName="+oneBook.getBookName()+"\t");

System.out.print("BookKind="+oneBook.getBookKind()+"\t");

System.out.println("BookPrice="+oneBook.getBookPrice());

}

userHibernateBean.doUpdateBookDataToDB(bookID);

//userHibernateBean.dodeleteBookDataToDB(bookID);

}

publicstaticvoidmain(String[]args)throwsjava.io.UnsupportedEncodingException

{

newJ2SEAppORMapping();

}

}

5、执行J2SEAppORMapping程序类

(1)启动“运行方式”菜单项目

(2)将出现下面的结果

3、示例程序执行后的结果——在数据库表中的数据发生了变化

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1