Struts+Hibernate+Sprint结合完美实现

实现接口类:
package com.wu.dao;
import java.util.List;
import hib.*;
import org.springframework.orm.hibernate3.support.*;
public class EmailDAOImp extends HibernateDaoSupport implements IEmailDAO {
org.springframework.orm.hibernate3.LocalSessionFactoryBean  sessionFactory;
/*
  * (non-Javadoc)
  * @see com.wu.dao.IEmailDAO#deleteE(int)
  * deleteE方法删除数据 按编号删除
  */
public  boolean deleteE(int emailId){
  try{
   /*
    * load方法加载类。 通过这个对象找到这个编号
    * 调用模版的delete方法 (删除的是个对象)
    */
   EmailInfo obj=(EmailInfo)this.getHibernateTemplate().load(EmailInfo.class, new Integer(emailId));
   this.getHibernateTemplate().delete(obj);
   return true;
  }catch(Exception ex){
   System.out.println(ex.getMessage());
   return false;
  }
}
/*
  * (non-Javadoc)
  * @see com.wu.dao.IEmailDAO#readAllEmail()
  * 读取方法 用模版的find方法
  */
public  List readAllEmail(){
  try{
   String  strSql="from hib.EmailInfo";
   return this.getHibernateTemplate().find(strSql);
  }catch(Exception ex){
   System.out.println(ex.getMessage());
   return null;
  }
}
/*
  * (non-Javadoc)
  * @see com.wu.dao.IEmailDAO#addEmail(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
  *  添加方法  调用用对象来添加
  *  添加的模版用Save方法来保存
  */
public boolean addEmail(String emailfrom, String emailto,
   String emailtitle, String emailcontent) {
  try{
   EmailInfo  email = new EmailInfo();
   email.setEmailfrom(emailfrom);
   email.setEmailto(emailto);
   email.setEmailtitle(emailtitle);
   email.setEmailcontent(emailcontent);
  
   FJInfo  fj = new FJInfo();
   fj.setFjfile("one");
   fj.setFjurl("one");
   FJInfo  fj1 = new FJInfo();
   fj1.setFjfile("two");
   fj1.setFjurl("two");
   email.getFjSet().add(fj);
   email.getFjSet().add(fj1);
   this.getHibernateTemplate().save(email);
   return true;
  }catch(Exception ex){
   System.out.println(ex.getMessage());
  }
  return false;
}

}
------------------------------------
Action类

public ActionForward insertEmail(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  EmaiForm emaiForm = (EmaiForm) form;// TODO Auto-generated method stub
  String emailfrom  = emaiForm.getEmailfrom();
  String emailto    = emaiForm.getEmailto();
  String emailtitle = emaiForm.getEmailtitle();
  String emailcontent = emaiForm.getEmailcontent();
  /*
   * addEmail 添加的方法
   * List   添加后要返回一个集合
   */
  if(emailDAO.addEmail(emailfrom, emailto, emailtitle, emailcontent)){
   List  email = emailDAO.readAllEmail();//把添加后的结果显示出来调用这个方法
    emaiForm.setList(email);
   return mapping.findForward("emailpage");
  }else{
   return mapping.findForward("errorpage");
  }
}
public ActionForward deleteEmail(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  EmaiForm emaiForm = (EmaiForm) form;// TODO Auto-generated method stub
  int emailId  =Integer.parseInt(request.getParameter("emailId"));
  /*
   * deleteE 删除的方法
   * List 删除后要返回一个集合
   */
  if(emailDAO.deleteE(emailId)){
   List  email = emailDAO.readAllEmail();//删除后要把删除后的结果显示出来
    emaiForm.setList(email);
   return mapping.findForward("emailpage");
  }else{
   return mapping.findForward("errorpage");
  }
 
}
--------------------------------------
配置文件
<beans>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName">
   <value>net.sourceforge.jtds.jdbc.Driver</value>
  </property>
  <property name="url">
   <value>jdbc:jtds:sqlserver://127.0.0.1:1433/pubs</value>
  </property>
  <property name="username">
   <value>sa</value>
  </property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>/hib/UserInfo.hbm.xml</value>
    <value>/hib/EmailInfo.hbm.xml</value>
    <value>/hib/FJInfo.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
</bean>
<bean id="userDAO" class="com.wu.dao.UserDAOImp">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
</bean>
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="target">
   <ref local="userDAO"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
</bean>
<bean id="emailDAO" class="com.wu.dao.EmailDAOImp">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
</bean>
<bean id="emailDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="target">
   <ref local="emailDAO"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="read*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
</bean>
<bean name="/login" class="com.wu.struts.action.LoginAction">
  <property name="userDAO">
   <ref bean="userDAOProxy"/>
  </property>
</bean>
<bean name="/emai" class="com.wu.struts.action.EmaiAction">
  <property name="emailDAO">
   <ref bean="emailDAOProxy"/>
  </property>
</bean>
</beans>
---------------------------
循环显示页面
<table width="80%" border="1" bgcolor="#ffcc88">
     <tr>
      <td>emailId</td>
      <td>emailfrom</td>
      <td>emailto</td>
      <td>emailtitle</td>
      <td>emailcontent</td>
      <td>delete</td>
     </tr>
        <logic:present name="emaiForm">
         <logic:iterate id="emai" name="emaiForm" property="list" type="hib.EmailInfo">
          <tr>
        <td><bean:write name="emai" property="emailId"/></td>
        <td><bean:write name="emai" property="emailfrom"/></td>
        <td><bean:write name="emai" property="emailto"/></td>
        <td><bean:write name="emai" property="emailtitle"/></td>
        <td><bean:write name="emai" property="emailcontent"/></td>
        <td><a href="/TestEmail/emai.do?method=deleteEmail&emailId=<%=emai.getEmailId()%>" onclick="return confirm('Y/N');">Delete</a></td>
       </tr>
         </logic:iterate>
        </logic:present>
</table>

你可能感兴趣的:(DAO,Hibernate,jdbc,orm,struts)