ssh框架整合

1、导包
   struts-2.3.15
      asm-3.3.jar、asm-commons-3.3.jar、asm-tree-3.3.jar、commons-fileupload-1.3.jar、commons-io-2.0.1.jar、commons-lang3-3.1.jar、commons-logging-1.1.3.jar、freemarker-2.3.19.jar
javassist-3.11.0.GA.jar、log4j-1.2.17.jar、ognl-3.0.6.jar、struts2-core-2.3.15.jar、xwork-core-2.3.15.jar
   struts与spring整合包:struts2-spring-plugin-2.3.15.jar
   
   hibernate-distribution-3.6.10.Final
hibernate3.jar
lib\required\*
lib\jpa\*

   spring-framework-3.2.0.RELEASE
IOC:spring-beans-3.2.0.RELEASE.jar、spring-context-3.2.0.RELEASE.jar、spring-core-3.2.0.RELEASE.jar、spring-expression-3.2.0.RELEASE.jar
AOP:spring-aop-3.2.0.RELEASE.jar、spring-aspects-3.2.0.RELEASE.jar、com.springsource.org.aopalliance-1.0.0.jar、com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
整合包:spring-jdbc-3.2.0.RELEASE.jar、spring-orm-3.2.0.RELEASE.jar、spring-test-3.2.0.RELEASE.jar、spring-tx-3.2.0.RELEASE.jar、spring-web-3.2.0.RELEASE.jar

   
   日志包:com.springsource.org.apache.commons.logging-1.1.1.jar、com.springsource.org.apache.log4j-1.2.15.jar、slf4j-log4j12-1.7.12.jar  


   数据库包:c3p0-0.9.1.jar、mysql-connector-java-5.1.38-bin.jar
   因为我使用的是c3p0连接池,所以后面配置的数据源就是c3p0的。


2、配置文件
   web.xml


  
  
  	org.springframework.web.context.ContextLoaderListener
  
  
  
  	contextConfigLocation
  	classpath:applicationContext.xml
  
  
  
  
  	struts
  	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	
  
  	struts
  	/*
  	
 	
  
    index.jsp
  




    applicationContext.xml


	
	
	
	
	
		
		
		
		
	
	
	
	
		
			
		
		
			
				org.hibernate.dialect.MySQLDialect
				true
				true
				update
			
		
		
		
			
				com/imooc_ssh/pojo/Product.hbm.xml
			
		
	
	
	
	
		
	
	
	
	
		
		
	
	
	
	
		
			
		
	
	
	
	
    	
  	
  	
  	
  	
  	



   struts.xml

	
	
		
			
			
		

	


    我的Action是交给spring来创建的,所以我的class写的是productAction。当然Action也可以不交给spring来管理,就像普通的struts.xml配置就好了,applicationContext.xml中也就不用配置
Action了。


3、acton、service、dao代码以及pojo中的hbm.xml
   
package com.imooc_ssh.action;
	import com.imooc_ssh.pojo.Product;
	import com.imooc_ssh.service.ProductService;
	import com.opensymphony.xwork2.ActionSupport;
	import com.opensymphony.xwork2.ModelDriven;


public class ProductAction extends ActionSupport implements ModelDriven{
	
	private static final long serialVersionUID = 1L;
	private Product product=new Product();
	private ProductService productService;
	@Override
	public Product getModel() {
		// TODO Auto-generated method stub
		return product;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	public String save(){
		System.out.println("action开始执行save方法..");
		productService.save(product);
		return NONE;
	}
}


    
   
 package com.imooc_ssh.service;


import org.springframework.transaction.annotation.Transactional;


import com.imooc_ssh.dao.ProductDao;
import com.imooc_ssh.pojo.Product;


@Transactional
public class ProductService {
	private ProductDao productDao;


	
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}


	public void save(Product product) {
		System.out.println("service中的save方法开始执行..");
		productDao.save(product);
	}
	
}




 
  package com.imooc_ssh.dao;
	import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
	import com.imooc_ssh.pojo.Product;

public class ProductDao extends HibernateDaoSupport{
	
	public void save(Product product) {
		System.out.println("dao中save方法开支执行..");
		this.getHibernateTemplate().save(product);
	}
	
}



   
 


	
		
			
		
		
		
		




    dao层继承了HibernateDaoSupport这个类就可以直接使用this.getHibernateTemplate()来操作,并不用再把HibernateTemplate注入进来。
    这是HibernateTemplate中的方法,这也是我为什么要把sessionFactory注入到dao层。
    public final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
}

你可能感兴趣的:(Struts,spring,hibernate,框架)