DOC

Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0

By Sally Watson,2014-09-10 19:03
19 views 0
Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0

    Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations方式快速开发框架()

    一、准备

    1. Hibernate hibernate-distribution-3.3.2.GA, hibernate-annotations-3.4.0.GA

    2. Spring spring-framework-2.5.5-with-dependencies.zip

    3. Struts2: struts-2.1.6-all.zip

    4. Extjs: ext-3.0.0.zip

    另外!数据库使用Oracle XE!开发工具使用eclipse-jee-galileo-SR1-win32Web器使用apache-tomcat-6.0.14

    二、数据库建表

    新建表空间DEMO!新建用户demo、密码demo。在该表空间中建表HSSEA_DEMOOBJECT_IDOBJECT_ID用于存储主键值。

    CREATE TABLE HSSEA_DEMO

    (

     ID NUMBER(10) NOT NULL,

     CODE NVARCHAR2(200) NOT NULL,

     DESCRIPTION NVARCHAR2(200)

    );

ALTER TABLE HSSEA_DEMO ADD (

     CONSTRAINT HSSEA_DEMO_PK

     PRIMARY KEY

     (ID));

CREATE TABLE OBJECT_ID

    (

     NAME VARCHAR2(255 BYTE) NOT NULL,

VALUE NUMBER(10) NOT NULL

);

三、新建动态网站

    Project name : HSSEADemoTarget runtime单击New...!弹出框选择Apache Tomcat v6.0NextTomcat installation directory选择安装tomcat的根目录!Finish再一次Finish完成新建动态网站。

    四、整合HibernateSpring

    复制以下jar包到项目WebContent\lib

    hibernate-distribution-3.3.2.GA\hibernate3.jar

    hibernate-distribution-3.3.2.GA\lib\required目录下所有jar

    hibernate-distribution-3.3.2.GA\lib\optional\c3p0\c3p0-0.9.1.jar

    hibernate-annotations-3.4.0.GA\hibernate-annotations.jar

    hibernate-annotations-3.4.0.GA\lib目录下的

    hibernate-commons-annotations.jarejb3-persistence.jar

    hibernate-annotations-3.4.0.GA\lib\test目录下log4j.jarslf4j-log4j12.jar

    spring-framework-2.5.5\dist\spring.jar

    spring-framework-2.5.5\lib\aspectj下所有包

    其他还需引入包commons-logging-1.0.4.jarojdbc14.jar

    在项目Properties->Java Build Path->LibrariesAdd JARs... 加入所有包。

    增加基础服务抽象类!用以完成基本的增删改查操作

    package com.xy.service;

import java.io.Serializable;

    import java.lang.reflect.ParameterizedType; import java.util.List;

    import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

    import org.hibernate.SessionFactory;

    import org.hibernate.criterion.Criterion;

    import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Order;

    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**

     * 增删改查分页等基本功能抽象类!使用hibernatedao

     * @author xy

     * @date 2009-12-31

     * @param <T> entity

     */

    public abstract class BaseServiceForHibernate<T> extends HibernateDaoSupport{

     static Logger logger = Logger.getLogger(BaseServiceForHibernate.class.getName());

     //为父类HibernateDaoSupport注入sessionFactory的值

     @Resource(name="sessionFactory")

     public void setSuperSessionFactory(SessionFactory sessionFactory){

     logger.debug("为父类HibernateDaoSupport注入sessionFactory的值

    ["+sessionFactory+"]");

     super.setSessionFactory(sessionFactory);

     }

     private Class<T> entityClass;

     @SuppressWarnings("unchecked")

     public BaseServiceForHibernate(){

     entityClass =(Class<T>) ((ParameterizedType) getClass()

     .getGenericSuperclass()).getActualTypeArguments()[0];

     logger.debug("得到entity对象类实例["+entityClass+"]");

     }

     /**

     * 根据对象是否存在ID新增或更新记录

     * @param entity对象

     */

     public void save(T o){

     logger.debug("保存数据!对象,"+o);

     super.getHibernateTemplate().saveOrUpdate(o);

     }

     /**

     * 根据主键删除记录

     * @param 主键

     */

     public void delete(Serializable id){

     logger.debug("根据主键删除数据!主键,"+id);

     super.getHibernateTemplate().delete(super.getHibernateTemplate().load(en

    tityClass, id));

     }

     /**

     * 根据条件查询记录

     * @param 存储条件的容器

     * @return 数据列表

     */

     @SuppressWarnings("unchecked")

     public List<T> query(Set<Criterion> criterionSet){

     logger.debug("根据条件查询数据?条件数,"+criterionSet.size());

     DetachedCriteria detachedCriteria = DetachedCriteria.forClass(entityClass);

     for(Criterion o : criterionSet){

     detachedCriteria.add(o);

     }

     return super.getHibernateTemplate().findByCriteria(detachedCriteria);

     }

     /**

     * 根据条件查询记录

     * @param 存储条件的容器

     * @param 存储排序的容器

     * @return 数据列表

     */

     @SuppressWarnings("unchecked")

     public List<T> query(Set<Criterion> criterionSet, Set<Order> orderSet){

     logger.debug("根据条件和排序查询数据?条件数,"+criterionSet.size()+"!排序数,"+orderSet.size());

     DetachedCriteria detachedCriteria = DetachedCriteria.forClass(entityClass);

     for(Criterion o : criterionSet){

     detachedCriteria.add(o);

     }

     for(Order o : orderSet){

     detachedCriteria.addOrder(o);

     }

     return super.getHibernateTemplate().findByCriteria(detachedCriteria);

     }

     /**

     * 根据条件分页查询记录

     * @param 存储条件的容器

     * @param 数据开始位置?第一条记录为0

     * @param 最大数据数

     * @return 数据列表

     */

     @SuppressWarnings("unchecked")

     public List<T> query(Set<Criterion> criterionSet, int firstResult, int maxResult

s){

     logger.debug("根据条件分页查询数据?条件数,"+criterionSet.size()+"!记录开始序号,"+firstResult+"!最大记录数,"+maxResults);

     DetachedCriteria detachedCriteria = DetachedCriteria.forClass(entityClass);

     for(Criterion o : criterionSet){

     detachedCriteria.add(o);

     }

     return super.getHibernateTemplate().findByCriteria(detachedCriteria, firstRe

    sult, maxResults);

     }

     /**

     * 根据条件分页查询记录

     * @param 存储条件的容器

     * @param 存储排序的容器

     * @param 数据开始位置?第一条记录为0

     * @param 最大数据数

     * @return 数据列表

     */

     @SuppressWarnings("unchecked")

     public List<T> query(Set<Criterion> criterionSet, Set<Order> orderSet, int firstResult, int maxResults){

     logger.debug("根据条件和排序分页查询数据?条件数,"+criterionSet.size()+"!排序数,"+orderSet.size()+"!记录开始序号,"+firstResult+"!最大记录数,"+maxResults);

     DetachedCriteria detachedCriteria = DetachedCriteria.forClass(entityClass);

     for(Criterion o : criterionSet){

     detachedCriteria.add(o);

     }

     for(Order o : orderSet){

     detachedCriteria.addOrder(o);

     }

     return super.getHibernateTemplate().findByCriteria(detachedCriteria, firstRe

sult, maxResults);

     }

     /**

     * 根据条件取得记录总数[性能严重问题!需改]

     * @param 存储条件的容器

     * @return 记录总数

     */

     public int totalSize(Set<Criterion> criterionSet){

     logger.debug("根据条件取记录总数?条件数,"+criterionSet.size());

     List<T> list = query(criterionSet);

     return list!=null?list.size():0;

     }

     /**

     * 根据主键取得数据

     * @param 主键

     * @return entity对象

     */

     @SuppressWarnings("unchecked")

     public T get(Serializable id){

     logger.debug("根据主键删除数据!主键,"+id);

     return (T)super.getHibernateTemplate().get(entityClass, id);

     }

    }

    增加Demo实体类

    package com.xy.entity.demo;

    import javax.persistence.Column; import javax.persistence.Entity;

    import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id;

    import javax.persistence.SequenceGenerator; import javax.persistence.Table;

    import javax.persistence.TableGenerator;

@Entity

    @Table(name="HSSEA_DEMO")

    public class Demo {

     @Id

     @GeneratedValue(strategy=GenerationType.TABLE, generator="hssea_demo")

     @TableGenerator(name = "hssea_demo",

     table="OBJECT_ID",

     pkColumnName="NAME",

     valueColumnName="VALUE",

     pkColumnValue="HSSEA_DEMO_PK",

     initialValue=1,

     allocationSize=1

     )

     @SequenceGenerator(name="hssea_demo_seq", sequenceName="seq_hssea_

    demo", allocationSize=1)

     private long id;

     @Column(name="CODE")

     private String code;

     @Column(name="DESCRIPTION")

     private String description;

     public long getId() {

     return id;

     }

     public void setId(long id) {

     this.id = id;

     }

     public String getCode() {

     return code;

     }

     public void setCode(String code) {

     this.code = code;

     }

     public String getDescription() {

     return description;

     }

     public void setDescription(String description) {

     this.description = description;

     }

    }

    增加DemoService

    package com.xy.service.demo;

import java.util.HashSet;

    import java.util.List;

    import java.util.Set;

    import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Restrictions; import org.springframework.stereotype.Service;

import com.xy.entity.demo.Demo;

    import com.xy.service.BaseServiceForHibernate;

@Service("demoService")

    public class DemoService extends BaseServiceForHibernate<Demo> {

     /**

     * 根据code取对象

     * @param code

     * @return

     */

Report this document

For any questions or suggestions please email
cust-service@docsford.com