new97架构下单表操作从前台到后台全过程

来源:互联网 发布:婚礼感受 知乎 编辑:程序博客网 时间:2024/04/29 03:16

1.VO : 放在pub工程中,pub.vo

package com.cattsoft.demo4bss.pub.vo;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

public class CompanyVO {
  /** identifier field */
    private String id;

    /** nullable persistent field */
    private String name;

    /** full constructor */
    private String createDate;
    private int currentPageNo;
    public CompanyVO(String id, String name) {
        this.id = id;
        this.name = name;
    }

    /** default constructor */
    public CompanyVO() {
    }

    /** minimal constructor */
    public CompanyVO(String id) {
        this.id = id;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("id", getId())
            .toString();
    }

    public boolean equals(Object other) {
        if ( !(other instanceof CompanyVO) ) return false;
        CompanyVO castOther = (CompanyVO) other;
        return new EqualsBuilder()
            .append(this.getId(), castOther.getId())
            .isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder()
            .append(getId())
            .toHashCode();
    }
   
    public String getCreateDate() {
      return createDate;
    }
    public void setCreateDate(String createDate) {
      this.createDate = createDate;
    }
    public int getCurrentPageNo() {
      return currentPageNo;
    }
    public void setCurrentPageNo(int currentPageNo) {
      this.currentPageNo = currentPageNo;
    }
}

 

2.DAO: 由middagen生成,自己加以修改

package com.cattsoft.new97.ejb.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.Query;
import java.util.*;
import net.sf.hibernate.HibernateException;

import com.cattsoft.new97.ejb.pub.BaseDAO;
import com.cattsoft.new97.ejb.hibernate.*;
import com.cattsoft.new97.ejb.util.*;
import com.cattsoft.demo4bss.pub.util.*;
import com.cattsoft.demo4bss.pub.err.*;
import com.cattsoft.demo4bss.pub.vo.*;


public class CompanyDAO extends BaseDAO{

private static Log log = LogFactory.getLog(CompanyDAO.class);

  public CompanyDAO() throws SysException{
  }

  public void addCompany(CompanyVO vo)throws SysException, AppException {
    Company company = new Company();
    company.setId(vo.getId());
    company.setName(vo.getName());
    try {
      session.save(company);
      flushSession();
      log.info("flushSession");
      //日志应该在EJB层处理了,因为在DAO层已经没有事务
      //log.info("新建公司基本信息:公司号" + company.getId() + ",公司名称" + company.getName());
    }
    catch (HibernateException he) {
      throw new SysException("10009", he);
    }
    catch (SysException se) {
      se.appendMsg("CompanyDAO.addCompany");
      throw se;
    }
}


public void updateCompany(CompanyVO vo)throws SysException{
    Company company = null;
     try {
       company = (Company) session.load(Company.class, vo.getId());
       company.setId(vo.getId());
       company.setName(vo.getName());
     }
     catch (HibernateException he) {
       //抛出业务错误。不需要处理,仅给用户参考即可。
     //  throw new AppException("10009",he);
     }
     try {
       session.saveOrUpdate(company);
       flushSession();
      //log.info("修改公司基本信息:公司号" + company.getId() + ",公司名称" + company.getName());
   }
   catch (HibernateException he) {
     he.printStackTrace();
     throw new SysException("10010", he);
   }
}

 public void del(String companyId) throws SysException {
   Company v1 = null;
   try {
     v1 = (Company) session.load(Company.class, companyId);
   }
   catch (HibernateException he) {
     throw new SysException("10013", he);
   }
   try {
     session.delete(v1);
     flushSession();
   }
   catch (HibernateException he) {
     throw new SysException("10013", he);
   }
   catch (SysException se) {
     se.appendMsg("CompanyDAO.del");
     throw se;
   }


 }


public PageDisplay queryCompany(CompanyVO vo) throws SysException{
   PageDisplay page = new PageDisplay();

   List lst = null;
   page.setCurrentPageNo(vo.getCurrentPageNo());
   //get query total no;
   String queryString = "select count(cust) from Cust as cust ";
   String queryWhere = "where 1=1 ";
   Query query = null;
   if (vo.getId().length() > 0) {
     queryWhere += " and cust.custId= '" + vo.getId() + "'";
   }
   if (vo.getName().length() > 0) {
     queryWhere += " and cust.custName like '%" + vo.getName() + "%'";
   }
   Query query1 = null;
   queryString += queryWhere;

   try {
     query1 = session.createQuery(queryString);
   }
   catch (HibernateException he) {
     throw new SysException("findCust1", he);
   }
   catch (java.lang.NullPointerException ne) {
     throw new SysException("findCustNull", ne);
   }
   try {
     page.setTotalRecNo(HibernateUtil.getCount(query1));
   }
   catch (SysException he) {
     he.appendMsg("CustDAO.findCusts");
     throw he;
   }

   //get query accord page
   try {
     queryString = "select cust from Cust as cust " + queryWhere;
     query = session.createQuery(queryString);
   }
   catch (HibernateException he) {
     throw new SysException("findCust2", he);
   }

   query.setFirstResult(page.getFirstRecNo());
   query.setMaxResults(page.getMaxNo());
   //page.setCurrentRecNo(new HibernateUtil(query).getCount());
   try {
     lst = query.list();
     flushSession();
   }
   catch (HibernateException he) {
     he.printStackTrace();
     throw new SysException("findCust3", he);
   }
   page.setCurrentRecNo(lst.size());
   page.setLst(lst);

   return page;
}
}

3.ejb层(sessionBean): 调用DAO

package com.cattsoft.new97.ejb.company;

import javax.ejb.*;
//import com.cattsoft.demo4bss.form.CustForm;
import com.cattsoft.new97.ejb.dao.*;
import com.cattsoft.new97.ejb.util.*;
import com.cattsoft.new97.ejb.hibernate.*;
import java.util.Map;
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
import javax.transaction.UserTransaction;
import com.cattsoft.demo4bss.pub.err.*;
import com.cattsoft.new97.ejb.pub.*;
import com.cattsoft.new97.ejb.util.*;
import com.cattsoft.demo4bss.pub.util.*;
//import ejbcall.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.cattsoft.demo4bss.pub.vo.*;


public class McompanyEJBBean
    implements SessionBean {
  SessionContext sessionContext;
  private static Log log = LogFactory.getLog(McompanyEJBBean.class);
  public void ejbCreate() throws CreateException {
  }

  public void ejbRemove() {
  }

  public void ejbActivate() {
  }

  public void ejbPassivate() {
  }

  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }

  public void addCompany(CompanyVO vo) throws
      SysException, AppException {
    try {
      HibernateSession.openSession();
      CompanyDAO companyDAO = new CompanyDAO();
      companyDAO.addCompany(vo);
      //test 以下语句测试数据库链接情况。延长20秒,在控制台可以看到链接情况。
      //try{Thread.sleep(20000);}catch(Exception e){}
    }
    catch (SysException se) {
      this.sessionContext.setRollbackOnly();
      se.appendMsg("CompanyEJBBean.addCompany");
      throw se;
    }
    catch (AppException se) {
      this.sessionContext.setRollbackOnly();
      se.appendMsg("CompanyEJBBean.addCompany");
      throw se;
    }
    finally {
      HibernateSession.closeSession();
    }

  }

  public void updateCompany(CompanyVO vo) throws
      SysException, AppException {
    try {
      HibernateSession.openSession();
      CompanyDAO companyDAO = new CompanyDAO();
      companyDAO.updateCompany(vo);
    }
    catch (SysException e) {
      this.sessionContext.setRollbackOnly();
      e.appendMsg("CompanyEJBBean.updateCompany");
      throw e;
    }
    /**   catch (AppException ae) {
         this.sessionContext.setRollbackOnly();
         ae.appendMsg("CompanyEJBBean.updateCompany");
         throw ae;
       } */
    finally {
      HibernateSession.closeSession();
    }

  }

  public void delCompany(String id) throws
      SysException, AppException {
    /**@todo Complete this method*/
    try {
      HibernateSession.openSession();
      CompanyDAO companyDAO = new CompanyDAO();
      companyDAO.del(id);
    }
    catch (SysException se) {
      this.sessionContext.setRollbackOnly();
      se.appendMsg("CompanyEJBBean.del");
      throw se;
    }
    finally {
      HibernateSession.closeSession();
    }

  }

  public PageDisplay findCompany(CompanyVO vo) throws SysException {
    PageDisplay p = null;
    try {
      HibernateSession.openSession();
      CompanyDAO companyDAO = new CompanyDAO();
      p = companyDAO.queryCompany(vo);
      return p;
    }
    catch (SysException e) {
      log.debug(e.getMessage());
      this.sessionContext.setRollbackOnly();
      e.appendMsg("CompanyEJBBean.findcompany");
      throw e;
    }
    finally {
      HibernateSession.closeSession();
    }
  }
}

4.web层(struts):明天补充


 

原创粉丝点击