ssh代码

0.Emp.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.po.Emp" table="emp" schema="dbo" catalog="pubs">
        <id name="eid" type="java.lang.Integer">
            <column name="eid" />
            <generator class="identity" />
        </id>
        <many-to-one name="dep" class="com.po.Dep" fetch="select">
            <column name="depid" not-null="true" />
        </many-to-one>
        <property name="ename" type="java.lang.String">
            <column name="ename" length="50" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="sex" length="10" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="address" length="100" />
        </property>
        <property name="borthday" type="java.util.Date">
            <column name="borthday" length="23" />
        </property>
        <set name="emploves" inverse="true" cascade="all" lazy="false">
            <key>
                <column name="eid" not-null="true" />
            </key>
            <one-to-many class="com.po.Emplove" />
        </set>
    </class>
</hibernate-mapping>


1.struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="empForm" type="com.struts.form.EmpForm" />
    <form-bean name="depForm" type="com.struts.form.DepForm" />
    <form-bean name="loveForm" type="com.struts.form.LoveForm" />
    <form-bean name="emploveForm" type="com.struts.form.EmploveForm" />
    <form-bean name="pageForm" type="com.struts.form.PageForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="empForm"
      input="/empadd.jsp"
      name="empForm"
      path="/empadd"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy"
      validate="false">
      <forward
        name="saveok"
        path="/list.do"
        redirect="true" />
    </action>
    <action
      path="/list"
      type="org.springframework.web.struts.DelegatingActionProxy"
      validate="false">
      <forward
        name="list"
        path="/list.jsp"
        redirect="true" />
    </action>
    <action
      input="/list.jsp"
      path="/update"
      type="org.springframework.web.struts.DelegatingActionProxy"
      validate="false">
      <forward
        name="update"
        path="/update.jsp"
        contextRelative="true" />
    </action>
    <action
      attribute="empForm"
      input="/update.jsp"
      name="empForm"
      path="/updateok"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy"
      validate="false">
      <forward
        name="updateok"
        path="/list.do"
        redirect="true" />
    </action>
    <action
      input="/list.jsp"
      path="/del"
      type="org.springframework.web.struts.DelegatingActionProxy"
      validate="false">
      <forward
        name="delok"
        path="/list.do"
        redirect="true" />
    </action>

  </action-mappings>

  <message-resources parameter="com.struts.ApplicationResources" />
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/spring.xml" />
  </plug-in>
  <plug-in className="com.plugin.MyPlugin"></plug-in>
</struts-config>


2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="
http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


3.spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName"
   value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
  </property>
  <property name="url"
   value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs">
  </property>
  <property name="username" value="sa"></property>
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="show_sql">true</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/po/Dep.hbm.xml</value>
    <value>com/po/Emp.hbm.xml</value>
    <value>com/po/Emplove.hbm.xml</value>
    <value>com/po/Love.hbm.xml</value>
   </list>
  </property>
 </bean>

 
 <bean id="EmploveDAO" class="com.po.EmploveDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="DepDAO" class="com.po.DepDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="EmpDAO" class="com.po.EmpDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="LoveDAO" class="com.po.LoveDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
  
 
 <bean id="PageDAO" class="com.po.PageDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <!-- 配置daoservice -->
 <bean id="daoservice" class="com.service.DaoService">
  <property name="edao">
   <ref bean="EmpDAO" />
  </property>
  <property name="depdao">
   <ref bean="DepDAO" />
  </property>
  <property name="eldao">
   <ref bean="EmploveDAO"/>
  </property>
  <property name="ldao">
   <ref bean="LoveDAO"/>
  </property>
  <property name="pagedao">
   <ref bean="PageDAO"/>
  </property>
 </bean>
 <!-- daoservicie配置完成 -->
 <!-- 配置所有的business -->
 <bean id="depbuss" class="com.business.DepBusiness">
  <property name="daoservice">
   <ref bean="daoservice" />
  </property>
 </bean>
 
 <bean id="lovebuss" class="com.business.LoveBusiness">
  <property name="daoservice">
   <ref bean="daoservice" />
  </property>
 </bean>
 
 <bean id="empbuss" class="com.business.EmpBusiness">
  <property name="daoservice">
   <ref bean="daoservice" />
  </property>
 </bean> 
 
 <bean id="pagebuss" class="com.business.PageBusiness">
  <property name="daoservice">
   <ref bean="daoservice" />
  </property>
 </bean>
 <!-- business配置完成 -->
 <!-- 配置bussinessService也就是managerservice -->
 <bean id="businessservice" class="com.service.ManagerService">
  <property name="depbuss">
   <ref bean="depbuss" />
  </property>
  <property name="lvbuss">
   <ref bean="lovebuss" />
  </property>
  <property name="ebuss">
   <ref bean="empbuss" />
  </property>
  <property name="pagebuss">
   <ref bean="pagebuss" />
  </property>
 </bean>
 <!--managerservice配置完-->
 <!-- 配置所有的action -->
 <bean name="/empadd" class="com.struts.action.EmpaddAction">
  <property name="mservice">
   <ref bean="businessservice"/>
  </property>
 </bean>
 <bean name="/list" class="com.struts.action.ListAction">
  <property name="mservice">
   <ref bean="businessservice"/>
  </property>
 </bean>
 <bean name="/update" class="com.struts.action.UpdateAction">
  <property name="mservice">
   <ref bean="businessservice"/>
  </property>
 </bean>
 <bean name="/updateok" class="com.struts.action.UpdateokAction">
  <property name="mservice">
   <ref bean="businessservice"/>
  </property>
 </bean>
 <bean name="/del" class="com.struts.action.DelAction">
  <property name="mservice">
   <ref bean="businessservice"/>
  </property>
 </bean>
 <!-- action配置完成 -->
 
 </beans>


4.hibernate.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

 <session-factory>
  <property name="connection.username">sa</property>
  <property name="connection.url">
   jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs
  </property>
  <property name="dialect">
   org.hibernate.dialect.SQLServerDialect
  </property>
  <property name="myeclipse.connection.profile">dbconn</property>
  <property name="connection.driver_class">
   com.microsoft.jdbc.sqlserver.SQLServerDriver
  </property>
  <property name="show_sql">true</property>
  <mapping resource="com/po/Emplove.hbm.xml" />
  <mapping resource="com/po/Dep.hbm.xml" />
  <mapping resource="com/po/Emp.hbm.xml" />
  <mapping resource="com/po/Love.hbm.xml" />

 </session-factory>

</hibernate-configuration>


5.EmpForm.java
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import java.util.*;
/**
 * MyEclipse Struts
 * Creation date: 12-16-2007
 *
 * XDoclet definition:
 * @struts.form name="empForm"
 */
public class EmpForm extends ActionForm {
 /*
  * Generated fields
  */

 /** sex property */
 private String sex;

 /** borthday property */
 private String borthday;

 /** address property */
 private String address;

 /** loves property */
 private Vector loves;

 /** ename property */
 private String ename;

 /** eid property */
 private String eid;

 /** loveid property */
 private String[] loveid;

 /** depid property */
 private String depid;

 /** depname property */
 private String depname;

 /*
  * Generated Methods
  */

 /**
  * Method validate
  * @param mapping
  * @param request
  * @return ActionErrors
  */
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  return null;
 }

 /**
  * Method reset
  * @param mapping
  * @param request
  */
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  // TODO Auto-generated method stub
 }

 /**
  * Returns the sex.
  * @return String
  */
 public String getSex() {
  return sex;
 }

 /**
  * Set the sex.
  * @param sex The sex to set
  */
 public void setSex(String sex) {
  this.sex = sex;
 }

 /**
  * Returns the borthday.
  * @return String
  */
 public String getBorthday() {
  return borthday;
 }

 /**
  * Set the borthday.
  * @param borthday The borthday to set
  */
 public void setBorthday(String borthday) {
  this.borthday = borthday;
 }

 /**
  * Returns the address.
  * @return String
  */
 public String getAddress() {
  return address;
 }

 /**
  * Set the address.
  * @param address The address to set
  */
 public void setAddress(String address) {
  this.address = address;
 }

 /**
  * Returns the loves.
  * @return Vector
  */
 public Vector getLoves() {
  return loves;
 }

 /**
  * Set the loves.
  * @param loves The loves to set
  */
 public void setLoves(Vector loves) {
  this.loves = loves;
 }

 /**
  * Returns the ename.
  * @return String
  */
 public String getEname() {
  return ename;
 }

 /**
  * Set the ename.
  * @param ename The ename to set
  */
 public void setEname(String ename) {
  this.ename = ename;
 }

 /**
  * Returns the eid.
  * @return String
  */
 public String getEid() {
  return eid;
 }

 /**
  * Set the eid.
  * @param eid The eid to set
  */
 public void setEid(String eid) {
  this.eid = eid;
 }

 /**
  * Returns the loveid.
  * @return String[]
  */
 public String[] getLoveid() {
  return loveid;
 }

 /**
  * Set the loveid.
  * @param loveid The loveid to set
  */
 public void setLoveid(String[] loveid) {
  this.loveid = loveid;
 }

 /**
  * Returns the depid.
  * @return String
  */
 public String getDepid() {
  return depid;
 }

 /**
  * Set the depid.
  * @param depid The depid to set
  */
 public void setDepid(String depid) {
  this.depid = depid;
 }

 /**
  * Returns the depname.
  * @return String
  */
 public String getDepname() {
  return depname;
 }

 /**
  * Set the depname.
  * @param depname The depname to set
  */
 public void setDepname(String depname) {
  this.depname = depname;
 }
}


6.EmpAddAction.java
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.service.*;
import com.struts.form.*;
/**
 * MyEclipse Struts
 * Creation date: 12-16-2007
 *
 * XDoclet definition:
 * @struts.action path="/empadd" name="empForm" input="/empadd.jsp" scope="request"
 */
public class EmpaddAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 /**
  * 定义service属性
  * */
 private ManagerService mservice;
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  EmpForm empForm = (EmpForm) form;// TODO Auto-generated method stub
  boolean bl=mservice.getEbuss().save(empForm);
  if(bl){
   return mapping.findForward("saveok");
  }
  return null;
 }
 public ManagerService getMservice() {
  return mservice;
 }
 public void setMservice(ManagerService mservice) {
  this.mservice = mservice;
 }
}


7.EmpBusiness.java
package com.business;

import java.text.ParseException;
import java.util.*;
import com.po.*;
import com.struts.form.*;
import com.service.*;

public class EmpBusiness implements IBusiness {
 private DaoService daoservice;
 public boolean delete(String id) {
  // TODO Auto-generated method stub
  EmpDAO edao=daoservice.getEdao();
  Emp emp=edao.findById(new Integer(id));
  try {
   edao.delete(emp);
   return true;
  } catch (RuntimeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return false;
 }

 public Vector findAll(String currentpage) {
  // TODO Auto-generated method stub
  PageDao pdao=daoservice.getPagedao();
  List ls=pdao.getPageData(currentpage);
  Vector vcemp=new Vector<EmpForm>();
  for (int i = 0; i < ls.size(); i++) {
   Emp emp=(Emp) ls.get(i);
   EmpForm ef=new EmpForm();
   ef.setEid(emp.getEid().toString());
   ef.setEname(emp.getEname());
   ef.setSex(emp.getSex());
   ef.setAddress(emp.getAddress());
   ef.setBorthday(emp.getBorthday().toLocaleString());
   //设置部门数据
   ef.setDepid(emp.getDep().getDepid().toString());
   ef.setDepname(emp.getDep().getDepname());
   //设置爱好数据
   Set emploves=emp.getEmploves();
   Vector vclv=new Vector<LoveForm>();
   if(emploves!=null&&!emploves.isEmpty()){
    Iterator it=emploves.iterator();
    while(it.hasNext()){
     Emplove el=(Emplove) it.next();
     Love lv=el.getLove();
     LoveForm lf=new LoveForm();
     lf.setLid(lv.getLid().toString());
     lf.setLname(lv.getLname());
     vclv.add(lf);
    }    
   }
   ef.setLoves(vclv);
   vcemp.add(ef);
   
  }
  return vcemp;
 }
 
 

 public Object findById(String id) {
  // TODO Auto-generated method stub
  EmpDAO edao=daoservice.getEdao();
  Emp emp=edao.findById(new Integer(id));
  EmpForm ef=new EmpForm();
  ef.setEid(emp.getEid().toString());
  ef.setEname(emp.getEname());
  ef.setSex(emp.getSex());
  ef.setAddress(emp.getAddress());
  ef.setBorthday(emp.getBorthday().toLocaleString());
  //设置部门数据
  ef.setDepid(emp.getDep().getDepid().toString());
  //ef.setDepname(emp.getDep().getDepname());
  //设置爱好数据
  Set emploves=emp.getEmploves();
  int i=0;
  if(emploves!=null&&!emploves.isEmpty()){
   Iterator it=emploves.iterator();
   String [] loveid=new String[emploves.size()];
   while(it.hasNext()){
    Emplove el=(Emplove) it.next();
    Love lv=el.getLove();
    loveid[i]=lv.getLid().toString();
    i++;
   }
   ef.setLoveid(loveid);
  }
  
  
  return ef;
 }

 public boolean save(Object ob) {
  // TODO Auto-generated method stub
  EmpForm ef=(EmpForm) ob;
  EmpDAO edao=daoservice.getEdao();
  
  Emp emp=new Emp();
  emp.setEname(ef.getEname());
  emp.setSex(ef.getSex());
  emp.setAddress(ef.getAddress());
  try {
   emp.setBorthday(java.text.DateFormat.getDateInstance().parse(ef.getBorthday()));
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //设置部门数据
  Dep dep=daoservice.getDepdao().findById(new Integer(ef.getDepid()));
  emp.setDep(dep);
  //设置爱好数据
  String lid[]=ef.getLoveid();
  LoveDAO ldao=daoservice.getLdao();
  Set emploves =new HashSet();
  EmploveDAO eldao=daoservice.getEldao();
  if(lid!=null){
   for (int i = 0; i < lid.length; i++) {
    Love lv=ldao.findById(new Integer(lid[i]));
    Emplove el=new Emplove();
    el.setEmp(emp);
    el.setLove(lv);
    emploves.add(el);
   }   
  }
  emp.setEmploves(emploves);
  
  try {
   edao.save(emp);
   return true;
  } catch (RuntimeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return false;
 }

 public boolean update(Object ob) {
  // TODO Auto-generated method stub
  EmpForm ef=(EmpForm) ob;
  EmpDAO edao=daoservice.getEdao();
  
  Emp emp=edao.findById(new Integer(ef.getEid()));
  emp.setEname(ef.getEname());
  emp.setSex(ef.getSex());
  emp.setAddress(ef.getAddress());
  try {
   emp.setBorthday(java.text.DateFormat.getDateInstance().parse(ef.getBorthday()));
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //设置部门数据
  Dep dep=daoservice.getDepdao().findById(new Integer(ef.getDepid()));
  emp.setDep(dep);
  //判断以前的员工爱好表中是否有数据,有则删之
  LoveDAO ldao=daoservice.getLdao();
  EmploveDAO eldao=daoservice.getEldao();
  Set oldemploves=emp.getEmploves();
  if(oldemploves!=null&&!oldemploves.isEmpty()){
   Iterator it=oldemploves.iterator();
   while(it.hasNext()){
    Emplove elold=(Emplove) it.next();
    eldao.delete(elold);
   }
  }
  //设置爱好数据
  String lid[]=ef.getLoveid();
  
  Set emploves =new HashSet();
  
  if(lid!=null){
   for (int i = 0; i < lid.length; i++) {
    Love lv=ldao.findById(new Integer(lid[i]));
    Emplove el=new Emplove();
    el.setEmp(emp);
    el.setLove(lv);
    emploves.add(el);
   }   
  }
  emp.setEmploves(emploves);
  
  try {
   edao.attachDirty(emp);
   return true;
  } catch (RuntimeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return false;
 }

 public DaoService getDaoservice() {
  return daoservice;
 }

 public void setDaoservice(DaoService daoservice) {
  this.daoservice = daoservice;
 }

}


8.EmpDAO.java
package com.po;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Data access object (DAO) for domain model class Emp.
 *
 * @see com.po.Emp
 * @author MyEclipse Persistence Tools
 */

public class EmpDAO extends HibernateDaoSupport {
 private static final Log log = LogFactory.getLog(EmpDAO.class);

 // property constants
 public static final String ENAME = "ename";

 public static final String SEX = "sex";

 public static final String ADDRESS = "address";

 protected void initDao() {
  // do nothing
 }
 
 public void save(Emp transientInstance) {
  log.debug("saving Emp instance");
  try {
   getHibernateTemplate().save(transientInstance);
   log.debug("save successful");
  } catch (RuntimeException re) {
   log.error("save failed", re);
   throw re;
  }
 }

 public void delete(Emp persistentInstance) {
  log.debug("deleting Emp instance");
  try {
   getHibernateTemplate().delete(persistentInstance);
   log.debug("delete successful");
  } catch (RuntimeException re) {
   log.error("delete failed", re);
   throw re;
  }
 }

 public Emp findById(java.lang.Integer id) {
  log.debug("getting Emp instance with id: " + id);
  try {
   Emp instance = (Emp) getHibernateTemplate().get("com.po.Emp", id);
   return instance;
  } catch (RuntimeException re) {
   log.error("get failed", re);
   throw re;
  }
 }

 public List findByExample(Emp instance) {
  log.debug("finding Emp instance by example");
  try {
   List results = getHibernateTemplate().findByExample(instance);
   log.debug("find by example successful, result size: "
     + results.size());
   return results;
  } catch (RuntimeException re) {
   log.error("find by example failed", re);
   throw re;
  }
 }

 public List findByProperty(String propertyName, Object value) {
  log.debug("finding Emp instance with property: " + propertyName
    + ", value: " + value);
  try {
   String queryString = "from Emp as model where model."
     + propertyName + "= ?";
   return getHibernateTemplate().find(queryString, value);
  } catch (RuntimeException re) {
   log.error("find by property name failed", re);
   throw re;
  }
 }

 public List findByEname(Object ename) {
  return findByProperty(ENAME, ename);
 }

 public List findBySex(Object sex) {
  return findByProperty(SEX, sex);
 }

 public List findByAddress(Object address) {
  return findByProperty(ADDRESS, address);
 }

 public List findAll() {
  log.debug("finding all Emp instances");
  try {
   String queryString = "from Emp";
   return getHibernateTemplate().find(queryString);
  } catch (RuntimeException re) {
   log.error("find all failed", re);
   throw re;
  }
 }

 public Emp merge(Emp detachedInstance) {
  log.debug("merging Emp instance");
  try {
   Emp result = (Emp) getHibernateTemplate().merge(detachedInstance);
   log.debug("merge successful");
   return result;
  } catch (RuntimeException re) {
   log.error("merge failed", re);
   throw re;
  }
 }

 public void attachDirty(Emp instance) {
  log.debug("attaching dirty Emp instance");
  try {
   getHibernateTemplate().saveOrUpdate(instance);
   log.debug("attach successful");
  } catch (RuntimeException re) {
   log.error("attach failed", re);
   throw re;
  }
 }

 public void attachClean(Emp instance) {
  log.debug("attaching clean Emp instance");
  try {
   getHibernateTemplate().lock(instance, LockMode.NONE);
   log.debug("attach successful");
  } catch (RuntimeException re) {
   log.error("attach failed", re);
   throw re;
  }
 }

 public static EmpDAO getFromApplicationContext(ApplicationContext ctx) {
  return (EmpDAO) ctx.getBean("EmpDAO");
 }
}


9.MyPlugin.java
package com.plugin;

import javax.servlet.ServletException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import com.business.*;
import com.service.ManagerService;
import java.util.*;
import org.springframework.context.*;
import org.springframework.context.support.*;
public class MyPlugin implements PlugIn {

 public void destroy() {
  // TODO Auto-generated method stub

 }

 public void init(ActionServlet servlet, ModuleConfig arg1)
   throws ServletException {
  System.out.println("====插件开始启动======");
  //在插件中获取spring中bean的对象
  String path=servlet.getServletContext().getRealPath("");
  ApplicationContext ctx=new FileSystemXmlApplicationContext(path+"/WEB-INF/spring.xml");
  ManagerService mservice=(ManagerService) ctx.getBean("businessservice");
  
  Vector depvc=mservice.getDepbuss().findAll();
  Vector lovevc=mservice.getLvbuss().findAll();
  System.out.println(depvc.size());
  System.out.println(lovevc.size());
  servlet.getServletContext().setAttribute("vcdep", depvc);
  servlet.getServletContext().setAttribute("vclove", lovevc);
  System.out.println("===插件启动完成==========");
 }

}


10.DaoService.java
package com.service;
import com.po.*;
public class DaoService {
 private EmpDAO edao;
 private DepDAO depdao;
 private LoveDAO ldao;
 private EmploveDAO eldao;
 private PageDao pagedao;
 public DepDAO getDepdao() {
  return depdao;
 }
 public void setDepdao(DepDAO depdao) {
  this.depdao = depdao;
 }
 public EmpDAO getEdao() {
  return edao;
 }
 public void setEdao(EmpDAO edao) {
  this.edao = edao;
 }
 public EmploveDAO getEldao() {
  return eldao;
 }
 public void setEldao(EmploveDAO eldao) {
  this.eldao = eldao;
 }
 public LoveDAO getLdao() {
  return ldao;
 }
 public void setLdao(LoveDAO ldao) {
  this.ldao = ldao;
 }
 public PageDao getPagedao() {
  return pagedao;
 }
 public void setPagedao(PageDao pagedao) {
  this.pagedao = pagedao;
 }
}

11.ManagerService.java
package com.service;
import com.business.*;
public class ManagerService {
 private EmpBusiness ebuss;
 private DepBusiness depbuss;
 private LoveBusiness lvbuss;
 private EmpLoveBusiness elbuss;
 private PageBusiness pagebuss;
 public DepBusiness getDepbuss() {
  return depbuss;
 }
 public void setDepbuss(DepBusiness depbuss) {
  this.depbuss = depbuss;
 }
 public EmpBusiness getEbuss() {
  return ebuss;
 }
 public void setEbuss(EmpBusiness ebuss) {
  this.ebuss = ebuss;
 }
 public EmpLoveBusiness getElbuss() {
  return elbuss;
 }
 public void setElbuss(EmpLoveBusiness elbuss) {
  this.elbuss = elbuss;
 }
 public LoveBusiness getLvbuss() {
  return lvbuss;
 }
 public void setLvbuss(LoveBusiness lvbuss) {
  this.lvbuss = lvbuss;
 }
 public PageBusiness getPagebuss() {
  return pagebuss;
 }
 public void setPagebuss(PageBusiness pagebuss) {
  this.pagebuss = pagebuss;
 }
 
}


12.HibernateSessionFactory.java
package com.share;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see
{@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

    /**
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses 
     * #resourceAsStream style lookup for its configuration file.
     * The default classpath location of the hibernate config file is
     * in the default package. Use #setConfigFile() to update
     * the location of the configuration file for the current session.  
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
 private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

 static {
     try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err
     .println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
  }
    }
    private HibernateSessionFactory() {
    }
 
 /**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

  if (session == null || !session.isOpen()) {
   if (sessionFactory == null) {
    rebuildSessionFactory();
   }
   session = (sessionFactory != null) ? sessionFactory.openSession()
     : null;
   threadLocal.set(session);
  }

        return session;
    }

 /**
     *  Rebuild hibernate session factory
     *
     */
 public static void rebuildSessionFactory() {
  try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err
     .println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
  }
 }

 /**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

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

 /**
     *  return session factory
     *
     */
 public static org.hibernate.SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 /**
     *  return session factory
     *
     * session factory will be rebuilded in the next call
     */
 public static void setConfigFile(String configFile) {
  HibernateSessionFactory.configFile = configFile;
  sessionFactory = null;
 }

 /**
     *  return hibernate configuration
     *
     */
 public static Configuration getConfiguration() {
  return configuration;
 }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值