Hibernate第四课.docx

上传人:b****6 文档编号:2920356 上传时间:2022-11-16 格式:DOCX 页数:31 大小:111.03KB
下载 相关 举报
Hibernate第四课.docx_第1页
第1页 / 共31页
Hibernate第四课.docx_第2页
第2页 / 共31页
Hibernate第四课.docx_第3页
第3页 / 共31页
Hibernate第四课.docx_第4页
第4页 / 共31页
Hibernate第四课.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

Hibernate第四课.docx

《Hibernate第四课.docx》由会员分享,可在线阅读,更多相关《Hibernate第四课.docx(31页珍藏版)》请在冰豆网上搜索。

Hibernate第四课.docx

Hibernate第四课

第四课Struts1与Hibernate的整合

数据库和表准备如下:

createdatabaseadmindb

go

useadmindb

go

createtableadmin(

aidintidentityprimarykey,

anamevarchar(50),

passwdvarchar(50)

--

select*fromadmin

一、建立Struts工程,引入struts组件包和Hibernate组件包。

二、生成POJO和Dao,将dao类进行改写,需要编写分页显示的方法和事务处理的方法。

1、AdminDAO类如下所示:

packagecom.dao;

importjava.util.List;

importorg.hibernate.LockMode;

importorg.hibernate.Query;

importorg.hibernate.Transaction;

importorg.hibernate.criterion.Example;

importorg.slf4j.Logger;

importorg.slf4j.LoggerFactory;

importcom.po.Admin;

importcom.util.HibernateSessionFactory;

 

publicclassAdminDAOextendsBaseHibernateDAO{

privatestaticfinalLoggerlog=LoggerFactory.getLogger(AdminDAO.class);

//propertyconstants

publicstaticfinalStringANAME="aname";

publicstaticfinalStringPASSWD="passwd";

publicvoidsave(AdmintransientInstance){

log.debug("savingAdmininstance");

Transactiontx=getSession().beginTransaction();

try{

getSession().save(transientInstance);

log.debug("savesuccessful");

mit();

}catch(RuntimeExceptionre){

log.error("savefailed",re);

tx.rollback();

throwre;

}finally{

HibernateSessionFactory.closeSession();

}

}

publicvoiddelete(AdminpersistentInstance){

log.debug("deletingAdmininstance");

Transactiontx=getSession().beginTransaction();

try{

getSession().delete(persistentInstance);

log.debug("deletesuccessful");

mit();

}catch(RuntimeExceptionre){

log.error("deletefailed",re);

tx.rollback();

throwre;

}finally{

HibernateSessionFactory.closeSession();

}

}

publicAdminfindById(java.lang.Integerid){

log.debug("gettingAdmininstancewithid:

"+id);

try{

Admininstance=(Admin)getSession().get("com.po.Admin",id);

returninstance;

}catch(RuntimeExceptionre){

log.error("getfailed",re);

throwre;

}finally{

HibernateSessionFactory.closeSession();

}

}

publicListfindByExample(Admininstance){

log.debug("findingAdmininstancebyexample");

try{

Listresults=getSession().createCriteria("com.po.Admin").add(

Example.create(instance)).list();

log.debug("findbyexamplesuccessful,resultsize:

"

+results.size());

returnresults;

}catch(RuntimeExceptionre){

log.error("findbyexamplefailed",re);

throwre;

}finally{

HibernateSessionFactory.closeSession();

}

}

publicListfindByProperty(StringpropertyName,Objectvalue){

log.debug("findingAdmininstancewithproperty:

"+propertyName

+",value:

"+value);

try{

StringqueryString="fromAdminasmodelwheremodel."

+propertyName+"=?

";

QueryqueryObject=getSession().createQuery(queryString);

queryObject.setParameter(0,value);

returnqueryObject.list();

}catch(RuntimeExceptionre){

log.error("findbypropertynamefailed",re);

throwre;

}finally{

HibernateSessionFactory.closeSession();

}

}

publicListfindByAname(Objectaname){

returnfindByProperty(ANAME,aname);

}

publicListfindByPasswd(Objectpasswd){

returnfindByProperty(PASSWD,passwd);

}

publicListfindAll(){

log.debug("findingallAdmininstances");

try{

StringqueryString="fromAdmin";

QueryqueryObject=getSession().createQuery(queryString);

returnqueryObject.list();

}catch(RuntimeExceptionre){

log.error("findallfailed",re);

throwre;

}finally{

HibernateSessionFactory.closeSession();

}

}

publicAdminmerge(AdmindetachedInstance){

log.debug("mergingAdmininstance");

Transactiontx=getSession().beginTransaction();

try{

Adminresult=(Admin)getSession().merge(detachedInstance);

log.debug("mergesuccessful");

mit();

returnresult;

}catch(RuntimeExceptionre){

log.error("mergefailed",re);

tx.rollback();

throwre;

}finally{

HibernateSessionFactory.closeSession();

}

}

publicvoidattachDirty(Admininstance){

log.debug("attachingdirtyAdmininstance");

Transactiontx=getSession().beginTransaction();

try{

getSession().saveOrUpdate(instance);

log.debug("attachsuccessful");

mit();

}catch(RuntimeExceptionre){

log.error("attachfailed",re);

tx.rollback();

throwre;

}finally{

HibernateSessionFactory.closeSession();

}

}

publicvoidattachClean(Admininstance){

log.debug("attachingcleanAdmininstance");

try{

getSession().lock(instance,LockMode.NONE);

log.debug("attachsuccessful");

}catch(RuntimeExceptionre){

log.error("attachfailed",re);

throwre;

}

}

/**

*获取最大页数

**/

publicintfindMaxpage(intpagesize){

if(pagesize<1)pagesize=5;

intmaxpage=0;

intmaxrow=0;

Stringsql="selectcount(aid)fromcom.po.Admin";

Queryqy=getSession().createQuery(sql

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

当前位置:首页 > 表格模板 > 调查报告

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

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