submit/>cancel/>
form>
Register1Action.java
packagecom.yourcompany.struts.action;
importhibernateDao.Sstest;
importhibernateDao.SstestDAO;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importorg.hibernate.Transaction;
importcom.yourcompany.struts.form.Register1Form;
publicclassRegister1ActionextendsAction{
//数据dao
SstestDAOsd;
publicRegister1Action(){
//TODOAuto-generatedconstructorstub
setSd(newSstestDAO());
}
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
Register1Formregister1Form=(Register1Form)form;//TODOAuto-generatedmethodstub
//输出客户端的用户名
response.setCharacterEncoding("GB2312");
System.out.println("register1Form.name\t"+register1Form.getUsername());
/*
*数据库操作
**/
Sstestst=newSstest();
st.setUsername(register1Form.getUsername());
st.setPassword(register1Form.getPassword());
//dao对象
SstestDAOsd=getSd();
Transactiontran=sd.getSession().beginTransaction();//开始事务
sd.save(st);
mit();
//请求转发到success
returnmapping.findForward("success");
}
//数据dao,get,set方法
publicvoidsetSd(SstestDAOsd){
this.sd=sd;
}
publicSstestDAOgetSd(){
returnsd;
}
}
Register1Form.java
packagecom.yourcompany.struts.form;
importjavax.servlet.http.HttpServletRequest;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionMapping;
publicActionErrorsvalidate(ActionMappingmapping,
HttpServletRequestrequest){
//TODOAuto-generatedmethodstub
returnnull;
}
publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){
//TODOAuto-generatedmethodstub
this.username="qin";
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
}
如果你觉得你的代码自动生成有问题的话,你可以参考下列代码
BaseHibernateDAO.java
packagehibernateDao;
importorg.hibernate.Session;
publicclassBaseHibernateDAOimplementsIBaseHibernateDAO{
publicSessiongetSession(){
returnHibernateSessionFactory.getSession();
}
}
HibernateSessionFactory.java
packagehibernateDao;
importorg.hibernate.HibernateException;
importorg.hibernate.Session;
importorg.hibernate.cfg.Configuration;
publicclassHibernateSessionFactory{
privatestaticStringCONFIG_FILE_LOCATION="/hibernate.cfg.xml";
privatestaticfinalThreadLocalthreadLocal=newThreadLocal();
private staticConfigurationconfiguration=newConfiguration();
privatestaticorg.hibernate.SessionFactorysessionFactory;
privatestaticStringconfigFile=CONFIG_FILE_LOCATION;
static{
try{
configuration.configure(configFile);
sessionFactory=configuration.buildSessionFactory();
}catch(Exceptione){
System.err
.println("%%%%ErrorCreatingSessionFactory%%%%");
e.printStackTrace();
}
}
privateHibernateSessionFactory(){
}
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;
}
publicstaticvoidrebuildSessionFactory(){
try{
configuration.configure(configFile);
sessionFactory=configuration.buildSessionFactory();
}catch(Exceptione){
System.err
.println("%%%%ErrorCreatingSessionFactory%%%%");
e.printStackTrace();
}
}
publicstaticvoidcloseSession()throwsHibernateException{
Sessionsession=(Session)threadLocal.get();
threadLocal.set(null);
if(session!
=null){
session.close();
}
}
publicstaticorg.hibernate.SessionFactorygetSessionFactory(){
returnsessionFactory;
}
publicstaticvoidsetConfigFile(StringconfigFile){
HibernateSessionFactory.configFile=configFile;
sessionFactory=null;
}
publicstaticConfigurationgetConfiguration(){
returnconfiguration;
}
}
IBaseHibernateDAO.java
packagehibernateDao;
importorg.hibernate.Session;
/**
*Dataaccessinterfacefordomainmodel
*@authorMyEclipsePersistenceTools
*/
publicinterfaceIBaseHibernateDAO{
publicSessiongetSession();
}
Sstest.java
packagehibernateDao;
ublicclassSstestimplementsjava.io.Serializable{
//Fields
privateIntegerid;
privateStringusername;
privateStringpassword;
//Constructors
/**defaultconstructor*/
publicSstest(){
}
/**fullconstructor*/
publicSstest(Stringusername,Stringpassword){
this.username=username;
this.password=password;
}
//Propertyaccessors
publicIntegergetId(){
returnthis.id;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicStringgetUsername(){
returnthis.username;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnthis.password;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
}
SstestDAO.java
packagehibernateDao;
importjava.util.List;
importmons.logging.Log;
importmons.logging.LogFactory;
importorg.hibernate.LockMode;
importorg.hibernate.Query;
importorg.hibernate.criterion.Example;
publicclassSstestDAOextendsBaseHibernateDAO{
privatestaticfinalLoglog=LogFactory.getLog(SstestDAO.class);
//propertyconstants
publicstaticfinalStringUSERNAME="username";
publicstaticfinalStringPASSWORD="password";
publicvoidsave(SstesttransientInstance){
log.debug("savingSstestinstance");
try{
getSession().save(transientInstance);
log.debug("savesuccessful");
}catch(RuntimeExceptionre){
log.error("savefailed",re);
throwre;
}
}
publicvoiddelete(SstestpersistentInstance){
log.debug("deletingSstestinstance");
try{
getSession().delete(persistentInstance);
log.debug("deletesuccessful");
}catch(RuntimeExceptionre){
log.error("deletefailed",re);
throwre;
}
}
publicSstestfindById(java.lang.Integerid){
log.debug("gettingSstestinstancewithid:
"+id);
try{
Sstestinstance=(Sstest)getSession().get("hibernateDao.Sstest",
id);
returninstance;
}catch(RuntimeExceptionre){
log.error("getfailed",re);
throwre;
}
}
publicListfindByExample(Sstestinstance){
log.debug("findingSstestinstancebyexample");
try{
Listresults=getSession().createCriteria("hibernateDao.Sstest")
.add(Example.create(instance)).list();
log.debug("findbyexamplesuccessful,resultsize:
"
+results.size());
returnresults;
}catch(RuntimeExceptionre){
log.error("findbyexamplefailed",re);
throwre;
}
}
publicListfindByProperty(StringpropertyName,Objectvalue){
log.debug("findingSstestinstancewithproperty:
"+propertyName
+",value:
"+value);
try{
StringqueryString="fromSstestasmodelwheremodel."
+propertyName+"=?
";
QueryqueryObject=getSession().createQuery(queryString);
queryObject.setParameter(0,value);
returnqueryObject.list();
}catch(RuntimeExceptionre){
log.error("findbypropertynamefailed",re);
throwre;
}
}
publicListfindByUsername(Objectusername){
returnfindByProperty(USERNAME,username);
}
publicListfindByPassword(Objectpassword){
returnfindByProperty(PASSWORD,password);
}
publicListfindAll(){
log.debug("findingallSstestinstances");
try{
StringqueryString="fromSstest";
QueryqueryObject=getSession().createQuery(queryString);
returnqueryObject.list();
}c