hibernate增删查改

本文介绍了一个基于Struts框架的Java应用实例,演示了如何使用Struts进行学生信息的增删改查操作。该应用利用了Hibernate进行数据持久化,并通过反射机制简化了对象属性的复制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 OpenAction.java

/**//*
*GeneratedbyMyEclipseStruts
*Templatepath:templates/java/JavaClass.vtl
*/

packageorg.zhb.struts.action;

importjava.lang.reflect.InvocationTargetException;
importjava.util.ArrayList;

importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;

importorg.apache.commons.beanutils.BeanUtils;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.DynaActionForm;
importorg.apache.struts.actions.DispatchAction;
importorg.hibernate.Session;
importorg.hibernate.Transaction;

importutil.BaseHibernateDAO;
importutil.HibernateSessionFactory;
importutil.Student;
importutil.StudentDAO;

/***//**
*MyEclipseStruts
*Creationdate:05-29-2007
*
*XDocletdefinition:
*@struts.actionvalidate="true"
*/

publicclassOpenActionextendsDispatchAction...{
/**//*
*GeneratedMethods
*/


/***//**
*Methodexecute
*
@parammapping
*
@paramform
*
@paramrequest
*
@paramresponse
*
@returnActionForward
*/

publicActionForwardinsert(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
...{
DynaActionFormoperationForm
=(DynaActionForm)form;
Studentstu
=newStudent();
try...{
BeanUtils.copyProperties(stu,operationForm);
BaseHibernateDAObao
=newBaseHibernateDAO();
Sessionsession
=bao.getSession();
Transactiontr
=session.beginTransaction();
StudentDAOdao
=newStudentDAO();
dao.save(stu);
tr.commit();
session.close();
}
catch(IllegalAccessExceptione)...{
e.printStackTrace();
}
catch(InvocationTargetExceptione)...{
e.printStackTrace();
}

returnmapping.findForward("find");
}

publicActionForwardfind(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
...{
StudentDAOdao
=newStudentDAO();
ArrayListlist
=dao.findAll();
request.setAttribute(
"list",list);
returnmapping.findForward("display");
}



publicActionForwarddelete(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
...{
DynaActionFormoperationForm
=(DynaActionForm)form;
Longid
=(Long)operationForm.get("id");
Sessionsession
=HibernateSessionFactory.getSession();
Transactiontr
=session.beginTransaction();
StudentDAOdao
=newStudentDAO();
dao.deleteById(id);
tr.commit();
session.close();
returnmapping.findForward("find");
}



publicActionForwardmodify(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
...{
DynaActionFormoperationForm
=(DynaActionForm)form;
Studentstu
=newStudent();
try...{
BeanUtils.copyProperties(stu,operationForm);
BaseHibernateDAObao
=newBaseHibernateDAO();
Sessionsession
=bao.getSession();
Transactiontr
=session.beginTransaction();
StudentDAOdao
=newStudentDAO();
dao.modifybyID(stu);
tr.commit();
session.close();
}
catch(IllegalAccessExceptione)...{
e.printStackTrace();
}
catch(InvocationTargetExceptione)...{
e.printStackTrace();
}

returnmapping.findForward("find");
}


}

2 BaseHibernateDAO.java

packageutil;

importorg.hibernate.Session;


/***//**
*Dataaccessobject(DAO)fordomainmodel
*
@authorMyEclipse-HibernateTools
*/

publicclassBaseHibernateDAOimplementsIBaseHibernateDAO...{

publicSessiongetSession()...{
returnHibernateSessionFactory.getSession();
}


}

3 HibernateSessionFactory.java

packageutil;

importorg.hibernate.HibernateException;
importorg.hibernate.Session;
importorg.hibernate.cfg.Configuration;

/***//**
*ConfiguresandprovidesaccesstoHibernatesessions,tiedtothe
*currentthreadofexecution.FollowstheThreadLocalSession
*pattern,see{
@linkhttp://hibernate.org/42.html}.
*/

publicclassHibernateSessionFactory...{

/***//**
*Locationofhibernate.cfg.xmlfile.
*LocationshouldbeontheclasspathasHibernateuses
*#resourceAsStreamstylelookupforitsconfigurationfile.
*Thedefaultclasspathlocationofthehibernateconfigfileis
*inthedefaultpackage.Use#setConfigFile()toupdate
*thelocationoftheconfigurationfileforthecurrentsession.
*/

privatestaticStringCONFIG_FILE_LOCATION="/hibernate.cfg.xml";
privatestaticfinalThreadLocal<Session>threadLocal=newThreadLocal<Session>();
privatestaticConfigurationconfiguration=newConfiguration();
privatestaticorg.hibernate.SessionFactorysessionFactory;
privatestaticStringconfigFile=CONFIG_FILE_LOCATION;

privateHibernateSessionFactory()...{
}


/***//**
*ReturnstheThreadLocalSessioninstance.Lazyinitialize
*the<code>SessionFactory</code>ifneeded.
*
*
@returnSession
*
@throwsHibernateException
*/

publicstaticSessiongetSession()throwsHibernateException...{
Sessionsession
=(Session)threadLocal.get();

if(session==null||!session.isOpen())...{
if(sessionFactory==null)...{
rebuildSessionFactory();
}

session
=(sessionFactory!=null)?sessionFactory.openSession()
:
null;
threadLocal.set(session);
}


returnsession;
}


/***//**
*Rebuildhibernatesessionfactory
*
*/

publicstaticvoidrebuildSessionFactory()...{
try...{
configuration.configure(configFile);
sessionFactory
=configuration.buildSessionFactory();
}
catch(Exceptione)...{
System.err
.println(
"%%%%ErrorCreatingSessionFactory%%%%");
e.printStackTrace();
}

}


/***//**
*Closethesinglehibernatesessioninstance.
*
*
@throwsHibernateException
*/

publicstaticvoidcloseSession()throwsHibernateException...{
Sessionsession
=(Session)threadLocal.get();
threadLocal.set(
null);

if(session!=null)...{
session.close();
}

}


/***//**
*returnsessionfactory
*
*/

publicstaticorg.hibernate.SessionFactorygetSessionFactory()...{
returnsessionFactory;
}


/***//**
*returnsessionfactory
*
*sessionfactorywillberebuildedinthenextcall
*/

publicstaticvoidsetConfigFile(StringconfigFile)...{
HibernateSessionFactory.configFile
=configFile;
sessionFactory
=null;
}


/***//**
*returnhibernateconfiguration
*
*/

publicstaticConfigurationgetConfiguration()...{
returnconfiguration;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值