Hibernate第四课.docx
《Hibernate第四课.docx》由会员分享,可在线阅读,更多相关《Hibernate第四课.docx(31页珍藏版)》请在冰豆网上搜索。
![Hibernate第四课.docx](https://file1.bdocx.com/fileroot1/2022-11/16/e4a36151-be5d-4b34-a181-a5584b1f5d3b/e4a36151-be5d-4b34-a181-a5584b1f5d3b1.gif)
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