ssh整合

阅读更多

1.导入hibernate jar包,配置hibernate.cfg.xml,ehcache配置,利用hibernate逆向工程生成两个domain类





	true
	update
	Mysql
	
	
		org.hibernate.dialect.MySQLDialect
	
	
	true
	org.hibernate.cache.EhCacheProvider
	
	
	
	
	true

	
        
	
	
	
	
	
	

	

 测试hibernate,新建单元测试类,加入如下代码

	private static SessionFactory sessionFactory=null;
	static{
		Configuration cfg=new Configuration().configure();
//        hibernate 4.0的用法
//        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();  
//        sessionFactory = cfg.buildSessionFactory(serviceRegistry);  
		sessionFactory= cfg.buildSessionFactory();
		System.out.println("statickkkkkk");
	}
	
	//测试hibernate,
	@Test
	public void test(){
		System.out.println(sessionFactory.openSession());
	}

 2.导入spring jar包,配置applicationContext.xml 如下





	
	


	
	


	  
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	

	
	
	
		
		
	


	
	
		
	
	
	


 测试spring 和hibernate整合 代码如下:

	//测试spring整合hibernate
	@Test
	public void test2(){
	      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

	       SessionFactory sf = (SessionFactory) applicationContext.getBean("sessionFactory");
	       System.out.println(sf.openSession());
	       Session session= sf.openSession();
	       
	       Employee emp= (Employee) session.get(Employee.class, 5);
	       System.out.println(emp.getName());
	}

 3.spring 和struts2整合,导入struts2 jar包 struts.xml配置如下:





	
	
	
	
	

	
		
		
			/test.jsp
		

		

	

 测试:

package cn.web.test;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
//将action交给spring管理
@Controller
public class TestAction extends ActionSupport {
	@Resource
	private SessionFactory sessionFactory;
	@Override
	public String execute() throws Exception {

		Session session= sessionFactory.openSession();
		System.out.println(session);
		return "success";
	}
}

 注:spring 和struts整合要struts2-spring-pluging.jar

 

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