实现struts2与hibernate的整合

主要思路

  • 导入相关的jar包
  • web.xml中配置struts2拦截器
  • 配置struts.xml文件
  • 对应的action
  • 配置hibernate.cfg.xml文件
  • 配置映射文件*.hbm.xml文件

具体实现

  • 工程目录结构
    实现struts2与hibernate的整合_第1张图片
  • 在pom.xml中导入想关的jar包
  
   
       org.hibernate
       hibernate-core
       3.6.8.Final
   

   
   
       junit
       junit
       4.7
       test
   
   
   
   
   		org.javassist
   		javassist
   		3.13.0-GA
   
   
   
   
       backport-util-concurrent
       backport-util-concurrent
       2.2
   
   
   
   
       commons-logging
       commons-logging
       1.1.1
   
   
   
   
       net.sf.ehcache
       ehcache
       1.2.3
   
   
   
   
       org.apache.struts
       struts2-core
       2.3.28
   

   
   
       org.apache.struts
       struts2-json-plugin
       2.3.28
   
   
   
   
   	jstl
   	jstl
   	1.1.2
   
   
   	taglibs
   	standard
   	1.1.2
   
  • 在web.xml中配置
  
 
     struts2  
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
 
 
     struts2
     /*
 
  • 配置struts.xml文件



	
	
	
	
	
	
		
			studentAction_show.action
			/index.jsp
		
	
	

  • 配置hibernate.cfg.xml文件






oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:orcl
scott
tiger


none

org.hibernate.dialect.Oracle10gDialect

thread

true





  • 创建Student bean
package com.it.bean;

public class Student {
	private String stu_id;
	private String stu_name;
	private String stu_sex;
	private String stu_birth;
	private String stu_addr;
	public String getStu_id() {
		return stu_id;
	}
	public void setStu_id(String stu_id) {
		this.stu_id = stu_id;
	}
	public String getStu_name() {
		return stu_name;
	}
	public void setStu_name(String stu_name) {
		this.stu_name = stu_name;
	}
	public String getStu_sex() {
		return stu_sex;
	}
	public void setStu_sex(String stu_sex) {
		this.stu_sex = stu_sex;
	}
	public String getStu_birth() {
		return stu_birth;
	}
	public void setStu_birth(String stu_birth) {
		this.stu_birth = stu_birth;
	}
	public String getStu_addr() {
		return stu_addr;
	}
	public void setStu_addr(String stu_addr) {
		this.stu_addr = stu_addr;
	}
	public Student(String stu_id, String stu_name, String stu_sex, String stu_birth, String stu_addr) {
		super();
		this.stu_id = stu_id;
		this.stu_name = stu_name;
		this.stu_sex = stu_sex;
		this.stu_birth = stu_birth;
		this.stu_addr = stu_addr;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String stu_id, String stu_name) {
		super();
		this.stu_id = stu_id;
		this.stu_name = stu_name;
	}
	
}

  • 配置student类的映射文件(要和student类在一个包下面,名字相同)



	
		
			
			
		
		
		
		
		
	

  • BaseDao
package com.it.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class BaseDao{
	private Session session=null;
	public BaseDao(Session session){
		this.session=session;
	}
	public void add(E e){
		session.save(e);
	}
	public List findAll(String hql){
		Query query=session.createQuery(hql);
		return query.list();
	}

}	

  • StudentDao
package com.it.dao;

import java.util.List;

import org.hibernate.Session;

import com.it.bean.Student;

public class StudentDao extends BaseDao{

	public StudentDao(Session session) {
		super(session);
		// TODO Auto-generated constructor stub
	}
	//添加用户数据
	public void add(Student stu){
		super.add(stu);
	}
	public List findAll(){
		String hql="from Student";
		return super.findAll(hql);
	}
}

  • StudnentService
package com.it.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import com.it.bean.Student;
import com.it.dao.StudentDao;
import com.it.util.CreateSessionFactory;

public class StudentService {
	
	public void add(Student stu){
		SessionFactory sessionfactory=null;
		Session session=null;
		Transaction tx=null;
		try {
			sessionfactory=CreateSessionFactory.getSessionFactory();
			session=sessionfactory.getCurrentSession();
			tx=session.beginTransaction();
			new StudentDao(session).add(stu);
			
			tx.commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public List findAll(){
		SessionFactory sessionfactory=null;
		Session session=null;
		Transaction tx=null;
		List list=null;
		try {
			sessionfactory=CreateSessionFactory.getSessionFactory();
			session=sessionfactory.getCurrentSession();
			tx=session.beginTransaction();
			list=new StudentDao(session).findAll();
			
			tx.commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}
}
  • 创建sesionFactory工具类
package com.it.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CreateSessionFactory {
	private static SessionFactory sessionFactory;
	private CreateSessionFactory(){
		
		
	}
	public static SessionFactory getSessionFactory(){
		if(sessionFactory==null){
			sessionFactory=new Configuration().configure().buildSessionFactory();
		}
		return sessionFactory;
	}
}

  • StudentAction
package com.it.action;

import java.util.List;

import com.it.bean.Student;
import com.it.service.StudentService;
import com.opensymphony.xwork2.ModelDriven;

public class StudentAction implements ModelDriven{
	private Student stu=new Student();
	private List list=null;
	public String add(){
		new StudentService().add(stu);
		return "show";
	}
	public String show(){
		list=new StudentService().findAll();
		return "index";
	}

	@Override
	public Student getModel() {
		// TODO Auto-generated method stub
		return stu;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	
}

其实hibernate与struts2整合没啥关系,整合包都没有,我也就是做个笔记而已

你可能感兴趣的:(框架学习)