JSF+Spring+Hibernate+范型DAO(1)

基本框架采用JSF – MyFaces(version 1.2.0) + Spring (version 2.5.1)  + Hibernate (version 3.2.5) and Hibernate Annotation (Version 3.3.0) 。

设计模式:MVC。
基本思路:根据MVC模式,将系统进行分层,将开发者进行分工(主要是前台和后台根据接口衔接联调)。JSF用于实现View和Controller。前台开发者只需要关心页面数据的显示(也就是VO属性的显示),页面的跳转和对数据更新操作的成功失败代码,他们操作的也应该只有VO或VO的集合,不应该脱离VO而只操作属性,这样会使得系统很散,逻辑性不强,给后期维护带来不必要的麻烦。他们也不应该关心数据是否准确与完整,不应该对得到的VO属性进行额外的完善。简单的说, 只需要获取--〉显示+页面跳转。Controller层需要调用对应的业务逻辑接口方法(业务逻辑层的接口方法按照业务功能进行命名)。使用Spring对业务逻辑层的事务进行控制。业务逻辑层可以根据需要对Controller层传递过来的参数进行相应的处理,调用DAO层接口方法(DAO层的接口方法按照针对一个业务逻辑方法需要对数据库进行的操作步骤来命名。例如,一个业务逻辑方法需要操作3次数据库,则需要定义三个DAO层接口方法)。在业务逻辑层中,可以对N个PO进行操作,但返回给Controller层的最好只是一个VO。业务逻辑类的划分,我是采用根据业务模块进行划分的,这种分法确实不好,没能很好体现Model层的价值。DAO层分两个包dao.common和dao.individual。 common里面放的是公用的范型DAO。当然,少不了相应的配置文件(faces-config.xml,applicationContext.xml和log4j.properties等)。至此,感觉好像少了Model层,其实是Model层被细化了,Model层分为业务Model(BO,DAO)和数据Model(VO,PO)。
并发控制:对于页面信息量较少的修改,采用Hibernate的乐观锁;对于页面信息量较大的修改,当一个用户进入可修改页面时,对该页面进行锁定,并记录该用户,当其他用户进入该页面时,页面可修改项和按钮被置灰并提示“此页面正在被用户XX锁定,您只能阅览”。
View层:不是很了解,暂不提。
Controller层:主要提下异常处理:

try {   
                       
} catch (Exception e) {   
facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR,   
        "ERROR:", ErrorMessage.getErrorString("processing_error"));   
FacesContext.getCurrentInstance().addMessage(null, facesMessage);   
logger.error(ExceptionUtils.getFullStackTrace(e));   
return SettlementConstants.FORWARD_ERROR;   
}  

 

业务逻辑层:
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
                       "http://www.springframework.org/dtd/spring-beans.dtd">  
<beans>  
 <!--  RESOURCE DEFINITIONS  -->  
 <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="dataSource">  
  <property name="jndiName" value=""/>  
 </bean>  
 <bean  
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" id="sessionFactory">  
  <property name="configurationClass">  
   <value>org.hibernate.cfg.AnnotationConfiguration</value>  
  </property>  
  <property name="dataSource">  
   <ref bean="dataSource"/>  
  </property>  
  <property name="annotatedPackages">  
   <list>  
    <value></value>  
   </list>  
  </property>  
  <property name="annotatedClasses">  
   <list>  
    <value></value>  
   </list>  
  </property>  
  <property name="hibernateProperties">  
   <props>  
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>  
    <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>  
    <prop key="hibernate.connection.release_mode">auto</prop>  
    <prop key="hibernate.transaction.auto_close_session">false</prop>  
    <prop key="hibernate.connection.autocommit">false</prop>  
    <prop key="hibernate.show_sql">false</prop>  
   </props>  
  </property>  
 </bean>  
 <bean  
  class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="sssTxManager">  
  <property name="sessionFactory">  
   <ref bean="sessionFactory"/>  
  </property>  
 </bean>  
 <bean abstract="true"  
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" id="baseTxBO">  
  <property name="transactionManager" ref="sssTxManager"/>  
  <property name="proxyTargetClass" value="true"/>  
  <property name="transactionAttributes">  
   <props>  
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>  
    <prop key="save*">PROPAGATION_REQUIRED</prop>  
    <prop key="update*">PROPAGATION_REQUIRED</prop>  
    <prop key="remove*">PROPAGATION_REQUIRED</prop>  
    <prop key="*">PROPAGATION_REQUIRED</prop>  
   </props>  
  </property>  
 </bean>  
 <!--  BO DEFINITIONS  -->  
  
 <bean id="managerMenuBO" parent="baseTxBO">  
  <property name="target">  
   <bean class="ManagerMenuBOImpl">  
    <property name="genericDao">  
     <ref bean="genericDao"/>  
    </property>  
    <property name="managerMenuDAO">  
     <ref bean="managerMenuDAO"/>  
    </property>  
    <property name="settleAppJDBCSupportDAO">  
     <ref bean="settleAppJDBCSupportDAO"/>  
    </property>  
   </bean>  
  </property>  
 </bean>  
  
 <bean id="dropdownListBO" parent="baseTxBO">  
  <property name="target">  
   <bean class="DropdownListBOImpl">  
    <property name="dropdownListDAO">  
     <ref bean="dropdownListDAO"/>  
    </property>  
   </bean>  
  </property>  
 </bean>  
 <!--  DAO DEFINITIONS  -->  
 <bean class="GenericDao" id="genericDao">  
  <property name="sessionFactory">  
   <ref bean="sessionFactory"/>  
  </property>  
 </bean>  
  
 <bean class="ManagerMenuDAOImpl" id="managerMenuDAO" parent="genericDao"/>     
 <bean class="DropdownListDAOImpl" id="dropdownListDAO" parent="genericDao"/>  
    
 <bean class="SettleAppJDBCSupportDAOImpl" id="settleAppJDBCSupportDAO">  
  <property name="dataSource">  
   <ref bean="dataSource"/>  
  </property>  
 </bean>  
</beans>  

  样例代码:

public interface ManagerMenuBO {   
    // User Authority Application Screen start   
    public List<UserAuthorityVO> getUserAuthorityList();   
    public boolean getUserInTemp(String soeid);   
    public boolean getUserInfo(String soeid);   
    public void saveUserAuthorityTemp(UserAuthorityTempVO userAuthorityTempVO);   
    // User Authority Application Screen end   
       
    // User Authority Update Review Screen start   
    public List<UserAuthorityTempVO> getUserAuthorityTempList(String appType, String updater);   
    public void updateUserAuthorityTemp(List<UserAuthorityTempVO> list, String appType, String reviewType);   
    // User Authority Update Review Screen end   
       
    // Worklist Set   
    public List<WorklistSetVO> getWorklistSet();   
    public void saveWorklistSet(List<WorklistSetVO> list);   
       
    // Contact Table Set   
    public List<ContactTableVO> getContactTableSet();   
    public void saveContactTableSet(List<ContactTableVO> list);   
       
    // Matrix Set   
    public List<MatrixSetVO> getMatrixSet();   
    public void saveMatrixSet(MatrixSetVO matrixSetVO);   
       
    // Data Extraction   
    public HashMap getDataExtraction(Date startDate, Date endDate);   
       
    // Manual Batch Last Update Date   
    public HashMap getMBLastUpdateDate();   
       
    // Manual Batch Execute   
    public String doMBExcete();   
  
}  

 

@SuppressWarnings("unchecked")   
public class ManagerMenuBOImpl implements ManagerMenuBO {   
       
    private IGenericDao genericDao;   
  
    public IGenericDao getGenericDao() {   
        return genericDao;   
    }   
  
    public void setGenericDao(IGenericDao genericDao) {   
        this.genericDao = genericDao;   
    }   
       
    private ManagerMenuDAO managerMenuDAO;   
           
    public ManagerMenuDAO getManagerMenuDAO() {   
        return managerMenuDAO;   
    }   
  
    public void setManagerMenuDAO(ManagerMenuDAO managerMenuDAO) {   
        this.managerMenuDAO = managerMenuDAO;   
    }   
       
    private SettleAppJDBCSupportDAO settleAppJDBCSupportDAO;   
       
    public SettleAppJDBCSupportDAO getSettleAppJDBCSupportDAO() {   
        return settleAppJDBCSupportDAO;   
    }   
  
    public void setSettleAppJDBCSupportDAO(   
            SettleAppJDBCSupportDAO settleAppJDBCSupportDAO) {   
        this.settleAppJDBCSupportDAO = settleAppJDBCSupportDAO;   
    }   
  
    // User Authority Application Screen start   
    public List<UserAuthorityVO> getUserAuthorityList(){   
        return genericDao.list(UserAuthorityVO.class);   
    }   
       
    public boolean getUserInTemp(String soeid){   
        UserAuthorityTempVO userAuthorityTempVO = (UserAuthorityTempVO)genericDao.get(UserAuthorityTempVO.class, soeid, LockMode.NONE);   
        if(userAuthorityTempVO == null){   
            return false;   
        }   
        else{   
            return true;   
        }   
    }   
       
    public boolean getUserInfo(String soeid){   
        UserAuthorityVO userAuthorityVO = (UserAuthorityVO)genericDao.get(UserAuthorityVO.class, soeid, LockMode.NONE);   
        if(userAuthorityVO == null){   
            return false;   
        }   
        else{   
            return true;   
        }   
    }   
       
    public void saveUserAuthorityTemp(UserAuthorityTempVO userAuthorityTempVO){   
        genericDao.saveOrUpdate(userAuthorityTempVO);   
    }   
    // User Authority Application Screen end   
       
    // User Authority Update Review Screen start   
    public List<UserAuthorityTempVO> getUserAuthorityTempList(String appType, String updater){   
        return managerMenuDAO.getUserAuthorityTempList(appType, updater);   
    }   
       
    public void updateUserAuthorityTemp(List<UserAuthorityTempVO> list, String appType, String reviewType){   
        managerMenuDAO.updateUserAuthorityTemp(list, appType, reviewType);     
    }   
    // User Authority Update Review Screen end   
       
    // Worklist Set   
    public List<WorklistSetVO> getWorklistSet(){     
        return genericDao.loadAll(WorklistSetVO.class);   
    }   
       
    public void saveWorklistSet(List<WorklistSetVO> list){   
        int j = list.size();   
        for(int i=0; i<j; i++){   
            WorklistSetVO worklistSetVO = list.get(i);   
            genericDao.saveOrUpdate(worklistSetVO);   
        }   
    }   
       
    //   Contact Table Set   
    public List<ContactTableVO> getContactTableSet(){   
        return genericDao.loadAll(ContactTableVO.class);   
    }   
       
    public void saveContactTableSet(List<ContactTableVO> list){   
        int j = list.size();   
        for(int i=0; i<j; i++){   
            ContactTableVO contactTableVO = list.get(i);   
            genericDao.saveOrUpdate(contactTableVO);   
        }   
    }   
       
    //   Matrix Set   
    public List<MatrixSetVO> getMatrixSet(){   
        return genericDao.loadAll(MatrixSetVO.class);   
    }   
       
    public void saveMatrixSet(MatrixSetVO matrixSetVO){   
        genericDao.saveOrUpdate(matrixSetVO);   
    }   
       
    //   Data Extraction   
    public HashMap getDataExtraction(Date startDate, Date endDate){   
        return managerMenuDAO.getDataExtraction(startDate, endDate);   
    }   
       
    // Manual Batch Last Update Date   
    public HashMap getMBLastUpdateDate(){   
        return settleAppJDBCSupportDAO.getMBLastUpdateDate();   
    }   
       
    // Manual Batch Execute   
    public String doMBExcete(){   
        return settleAppJDBCSupportDAO.doMBExcete();   
    }   
       
}  

 

你可能感兴趣的:(DAO,spring,Hibernate,bean,JSF)