框架整合之--------spring2.5+struts2.1+hibernate3.6

一.导入jar包


 1.  jar包地址:http://pan.baidu.com/s/1o7RjVJk

框架整合之--------spring2.5+struts2.1+hibernate3.6_第1张图片


二。配置

框架整合之--------spring2.5+struts2.1+hibernate3.6_第2张图片

1.hibernate.cfg.xml





	
	
	
	
	
	true
	
	update
	
	


2.strtus.xml




	
	
	
	
	
		
			 test.jsp
		
	


3.applicationContext.xml




	
	
	
	
	
	
	
		
		
		
		
			
			
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
			
		
	
	
	
	
		
	
	
	
	


4.web.xml



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

三.测试


1.测试Spring

package com.kjst.oa.test;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {

	private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	 
        /**
         *
         *测试sessionFactory
         */ 
        @Test
	public void testSessionFactory() {
		SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
		System.out.println(sessionFactory.openSession());
	}

        //测试事务
	@Test
	public void testTransaction() {
		// TODO Auto-generated method stub
		TestService testService = (TestService) ac.getBean("testService");
		testService.addUsers();
	}
}


import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.kjst.oa.model.User;

@Component("testService")
public class TestService {

	@Resource
	private  SessionFactory sessionFactory;
	
	@Transactional
	public void addUsers(){
		Session session = sessionFactory.getCurrentSession();
		User user = new User();
		user.setName("lisi");
		user.setPassword("1111");
		User user2 = new User();
		user2.setName("zhangsan");
		user2.setPassword("2222");
		session.save(user);
		
//		int a = 1/0;
		
		session.save(user2);
	}
	
}


public class User{
	private int id;
	private String name;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}





	
		
			
		
		
		
	

四.源码:http://pan.baidu.com/s/1hs3VLWg

       整合的原因是最近心血来潮,想尝试整合Spring+struts+hibernate框架,后续还有ssm框架整合spring+struts+mybatis 或者spring+springmvc+mybatis

当然还有后续的JFinal框架整合的小Demo

效果:

框架整合之--------spring2.5+struts2.1+hibernate3.6_第3张图片

你可能感兴趣的:(框架整合)