Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合(一)

2014-11-24 00:14:16 · 作者: · 浏览: 0

最新版Struts2+Hibernate+Spring整合

目前为止三大框架最新版本是:

struts2.3.16.1

hibernate4.3.4

spring4.0.2

其中struts2和hibernate的下载方式比较简单,但是spring下载有点麻烦,可以直接复制下面链接下载最新版spring


\http://repo.springsource.org/libs-release-local/org/springframework/spring/4.0.2.RELEASE/spring-framework-4.0.2.RELEASE-dist.zip

一. 所需的jar包(其中aopaliance-1.0.jar,是spring所依赖的jar,直接复制粘贴到谷歌百度就有的下载)

框架

版本

所需jar包

Struts2

2.3.16.1

\

Hibernate

4.3.4

\

spring

4.0.2

\

其它

\


二. 创建一张表

CREATE TABLE `user` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`user_name` varchar(20) DEFAULT NULL,

`password` varchar(20) DEFAULT NULL,

`address` varchar(100) DEFAULT NULL,

`phone_number` varchar(20) DEFAULT NULL,

`create_time` datetime DEFAULT NULL,

`update_time` datetime DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULTCHARSET=utf8;

并插入一条数据

INSERT INTO `user` VALUES ("1', 'test','test', 'test', 'test', '2014-03-29 00:48:14', '2014-03-29 00:48:17');

三. 先看下myeclipse的目录结构

\

\

四. 配置文件

1. web.xml

  

  
  
   	
  
  
   
  
    
    
     contextConfigLocation
     
    
     classpath:applicationContext.xml
     
   
  
	
    
    
     org.springframework.web.context.ContextLoaderListener
     
   
	
  
   
  
    
    
     struts2
     
    
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     
    
  
   
   
    
    
     openSessionInViewFilter
     
    
     org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
     
     
     
      singleSession
      
     
      true
      
     
   
  
  
    
    
     struts2
     
    
     /*
     
   
   
    
    
     openSessionInViewFilter
     
    
     *.do,*.action
     
   
  
  
    
    
     index.jsp
     
   

  

2. applicationContext.xml

    

    

	
   
	
   

	
   
	
    
     
     
     
     
     
     
     
     
   
	
	
   
	
    
     
      
     
     
     
     
      
      
       com.bufoon.entity
       
      
     
   

	
   
	
    
     
   

	
   
	
    
     
     
      
      
      
      
      
      
      
      
      
      
      
      
      
      
     
   
	
    
   

	
   
	
    
     
     
   

	
   
	
   


  

3. db.properties

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test
db.username=root
db.password=root

4. hibernate.cfg.xml

  



  
	
    
    
     org.hibernate.dialect.MySQLDialect
     
    
     20
     
    
     true
     
     
    
     true
     
    
     true
     
    
     UTF-8
     
     
    
     /ehcache.xml
     
    
     org.hibernate.cache.ehcache.EhCacheRegionFactory
     
    
     true
     
   

  

5. struts.xml

  



  
	
    
    
     org.hibernate.dialect.MySQLDialect
     
    
     20
     
    
     true
     
     
    
     true
     
    
     true
     
    
     UTF-8
     
     
    
     /ehcache.xml
     
    
     org.hibernate.cache.ehcache.EhCacheRegionFactory
     
    
     true
     
   

  

6. ehcache.xml (可以到下载的hibernate文件目录(hibernate-release-4.3.4.Final\hibernate-release-4.3.4.Final\project\etc)下找

五. JAVA类

1.BaseDAO.java(网上找的一个)

package com.bufoon.dao;

import java.io.Serializable;
import java.util.List;

/**
 * 基础数据库操作类
 * 
 * @author ss
 * 
 */
public interface BaseDAO
  
    {

	/**
	 * 保存一个对象
	 * 
	 * @param o
	 * @return
	 */
	public Serializable save(T o);

	/**
	 * 删除一个对象
	 * 
	 * @param o
	 */
	public void delete(T o);

	/**
	 * 更新一个对象
	 * 
	 * @param o
	 */
	public void update(T o);

	/**
	 * 保存或更新对象
	 * 
	 * @param o
	 */
	public void saveOrUpdate(T o);

	/**
	 * 查询
	 * 
	 * @param hql
	 * @return
	 */
	public List
   
     find(String hql); /** * 查询集合 * * @param hql * @param param * @return */ public List
    
      find(String hql, Object[] param); /** * 查询集合 * * @param hql * @param param * @return */ public List
     
       find(String hql, List
       param); /** * 查询集合(带分页) * * @param hql * @param param * @param page * 查询第几页 * @param rows * 每页显示几条记录 * @return */ public List
       
         find(String hql, Object[] param, Integer page, Integer rows); /** * 查询集合(带分页) * * @param hql * @param param * @param page * @param rows * @return */ public List
        
          find(String hql, List
          pa