您好,欢迎访问三七文档
Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0Annotations方式快速开发框架(上)一、准备1.Hibernate:hibernate-distribution-3.3.2.GA,hibernate-annotations-3.4.0.GA2.Spring:spring-framework-2.5.5-with-dependencies.zip3.Struts2:struts-2.1.6-all.zip4.Extjs:ext-3.0.0.zip另外,数据库使用OracleXE,开发工具使用eclipse-jee-galileo-SR1-win32,Web容器使用apache-tomcat-6.0.14。二、数据库建表新建表空间DEMO,新建用户demo、密码demo。在该表空间中建表HSSEA_DEMO和OBJECT_ID,OBJECT_ID用于存储主键值。CREATETABLEHSSEA_DEMO(IDNUMBER(10)NOTNULL,CODENVARCHAR2(200)NOTNULL,DESCRIPTIONNVARCHAR2(200));ALTERTABLEHSSEA_DEMOADD(CONSTRAINTHSSEA_DEMO_PKPRIMARYKEY(ID));CREATETABLEOBJECT_ID(NAMEVARCHAR2(255BYTE)NOTNULL,VALUENUMBER(10)NOTNULL);三、新建动态网站Projectname:HSSEADemo。Targetruntime单击New...,弹出框选择ApacheTomcatv6.0,Next,Tomcatinstallationdirectory选择安装tomcat的根目录,Finish。再一次Finish完成新建动态网站。四、整合Hibernate、Spring复制以下jar包到项目WebContent\lib下hibernate-distribution-3.3.2.GA\hibernate3.jarhibernate-distribution-3.3.2.GA\lib\required目录下所有jar包hibernate-distribution-3.3.2.GA\lib\optional\c3p0\c3p0-0.9.1.jarhibernate-annotations-3.4.0.GA\hibernate-annotations.jarhibernate-annotations-3.4.0.GA\lib目录下的hibernate-commons-annotations.jar、ejb3-persistence.jarhibernate-annotations-3.4.0.GA\lib\test目录下log4j.jar、slf4j-log4j12.jarspring-framework-2.5.5\dist\spring.jarspring-framework-2.5.5\lib\aspectj下所有包其他还需引入包commons-logging-1.0.4.jar,ojdbc14.jar。在项目Properties-JavaBuildPath-Libraries,AddJARs...加入所有包。增加基础服务抽象类,用以完成基本的增删改查操作packagecom.xy.service;importjava.io.Serializable;importjava.lang.reflect.ParameterizedType;importjava.util.List;importjava.util.Set;importjavax.annotation.Resource;importorg.apache.log4j.Logger;importorg.hibernate.SessionFactory;importorg.hibernate.criterion.Criterion;importorg.hibernate.criterion.DetachedCriteria;importorg.hibernate.criterion.Order;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;/***增删改查分页等基本功能抽象类,使用hibernate做dao层*@authorxy*@date2009-12-31*@paramTentity类*/publicabstractclassBaseServiceForHibernateTextendsHibernateDaoSupport{staticLoggerlogger=Logger.getLogger(BaseServiceForHibernate.class.getName());//为父类HibernateDaoSupport注入sessionFactory的值@Resource(name=sessionFactory)publicvoidsetSuperSessionFactory(SessionFactorysessionFactory){logger.debug(为父类HibernateDaoSupport注入sessionFactory的值[+sessionFactory+]);super.setSessionFactory(sessionFactory);}privateClassTentityClass;@SuppressWarnings(unchecked)publicBaseServiceForHibernate(){entityClass=(ClassT)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];logger.debug(得到entity对象类实例[+entityClass+]);}/***根据对象是否存在ID新增或更新记录*@paramentity对象*/publicvoidsave(To){logger.debug(保存数据,对象:+o);super.getHibernateTemplate().saveOrUpdate(o);}/***根据主键删除记录*@param主键*/publicvoiddelete(Serializableid){logger.debug(根据主键删除数据,主键:+id);super.getHibernateTemplate().delete(super.getHibernateTemplate().load(entityClass,id));}/***根据条件查询记录*@param存储条件的容器*@return数据列表*/@SuppressWarnings(unchecked)publicListTquery(SetCriterioncriterionSet){logger.debug(根据条件查询数据!条件数:+criterionSet.size());DetachedCriteriadetachedCriteria=DetachedCriteria.forClass(entityClass);for(Criteriono:criterionSet){detachedCriteria.add(o);}returnsuper.getHibernateTemplate().findByCriteria(detachedCriteria);}/***根据条件查询记录*@param存储条件的容器*@param存储排序的容器*@return数据列表*/@SuppressWarnings(unchecked)publicListTquery(SetCriterioncriterionSet,SetOrderorderSet){logger.debug(根据条件和排序查询数据!条件数:+criterionSet.size()+,排序数:+orderSet.size());DetachedCriteriadetachedCriteria=DetachedCriteria.forClass(entityClass);for(Criteriono:criterionSet){detachedCriteria.add(o);}for(Ordero:orderSet){detachedCriteria.addOrder(o);}returnsuper.getHibernateTemplate().findByCriteria(detachedCriteria);}/***根据条件分页查询记录*@param存储条件的容器*@param数据开始位置(第一条记录为0)*@param最大数据数*@return数据列表*/@SuppressWarnings(unchecked)publicListTquery(SetCriterioncriterionSet,intfirstResult,intmaxResults){logger.debug(根据条件分页查询数据!条件数:+criterionSet.size()+,记录开始序号:+firstResult+,最大记录数:+maxResults);DetachedCriteriadetachedCriteria=DetachedCriteria.forClass(entityClass);for(Criteriono:criterionSet){detachedCriteria.add(o);}returnsuper.getHibernateTemplate().findByCriteria(detachedCriteria,firstResult,maxResults);}/***根据条件分页查询记录*@param存储条件的容器*@param存储排序的容器*@param数据开始位置(第一条记录为0)*@param最大数据数*@return数据列表*/@SuppressWarnings(unchecked)publicListTquery(SetCriterioncriterionSet,SetOrderorderSet,intfirstResult,intmaxResults){logger.debug(根据条件和排序分页查询数据!条件数:+criterionSet.size()+,排序数:+orderSet.size()+,记录开始序号:+firstResult+,最大记录数:+maxResults);DetachedCriteriadetachedCriteria=DetachedCriteria.forClass(entityClass);for(Criteriono:criterionSet){detachedCriteria.add(o);}for(Ordero:orderSet){detachedCriteria.addOrder(o);}returnsuper.getHibernateTemplate().findByCriteria(detachedCriteria,firstResult,maxResults);}/***根据条件取得记录总数[性能严重问题,需改]*@param存储条件的容器*@return记录总数*/publicinttotalSize(SetCriterioncriterionSet){logger.debug(根据条件取记录总数!条件数:+criterionSet.size());ListTlist=query(criterionSet);returnlist!=null?list.size():0;}/***根据主键取得数据*@param主键*@
本文标题:Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0
链接地址:https://www.777doc.com/doc-4825827 .html