实现struts2与hibernate的整合

本文介绍了如何将Struts2与Hibernate进行整合,包括导入相关jar包,配置web.xml、struts.xml和hibernate.cfg.xml文件,创建bean、映射文件、BaseDao、StudentDao、StudentService以及SessionFactory工具类,最后通过一个具体的StudentAction来展示整合过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要思路

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

具体实现

  • 工程目录结构
    在这里插入图片描述
  • 在pom.xml中导入想关的jar包
  <!-- hibernate的和核心包 -->
   <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
       <version>3.6.8.Final</version>
   </dependency>

   <!-- 单元测试包 -->
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.7</version>
       <scope>test</scope>
   </dependency>
   
   <!-- 字节码包 -->
   <dependency>
   		<groupId>org.javassist</groupId>
   		<artifactId>javassist</artifactId>
   		<version>3.13.0-GA</version>
   </dependency>
   
   <!-- 并发访问处理端口的工具包 解决maven工程下的资源的访问问题 -->
   <dependency>
       <groupId>backport-util-concurrent</groupId>
       <artifactId>backport-util-concurrent</artifactId>
       <version>2.2</version>
   </dependency>
   
   <!-- 日志包 -->
   <dependency>
       <groupId>commons-logging</groupId>
       <artifactId>commons-logging</artifactId>
       <version>1.1.1</version>
   </dependency>
   
   <!-- 二级缓存ehcache包 -->
   <dependency>
       <groupId>net.sf.ehcache</groupId>
       <artifactId>ehcache</artifactId>
       <version>1.2.3</version>
   </dependency>
   
   <!-- struts2核心包 -->
   <dependency>
       <groupId>org.apache.struts</groupId>
       <artifactId>struts2-core</artifactId>
       <version>2.3.28</version>
   </dependency>

   <!-- struts2异步请求的包 -->
   <dependency>
       <groupId>org.apache.struts</groupId>
       <artifactId>struts2-json-plugin</artifactId>
       <version>2.3.28</version>
   </dependency>
   
   <!-- jstl+el表达式的包 -->
   <dependency>
   	<groupId>jstl</groupId>
   	<artifactId>jstl</artifactId>
   	<version>1.1.2</version>
   </dependency>
   <dependency>
   	<groupId>taglibs</groupId>
   	<artifactId>standard</artifactId>
   	<version>1.1.2</version>
   </dependency>
  • 在web.xml中配置
  <!-- struts2中央处理器 -->
 <filter>
     <filter-name>struts2</filter-name>  
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>
  • 配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 认识系统中的常量 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
	<constant name="struts.action.extension" value="action,do,song"></constant>
	<constant name="struts.devMode" value="true"></constant>
	
	<package name="stu" namespace="/stu" extends="struts-default">
		<action name="studentAction_*" class="com.it.action.StudentAction" method="{1}">
			<result name="show" type="redirectAction">studentAction_show.action</result>
			<result name="index" >/index.jsp</result>
		</action>
	</package>
	
</struts>
  • 配置hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<!--Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>


<property name="javax.persistence.validation.mode">none</property>
<!-- sql dialect方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!--Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!--Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="com/it/bean/Student.hbm.xml"/>

</session-factory>
</hibernate-configuration>

  • 创建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类在一个包下面,名字相同)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.it.bean">
	<class name="Student" table="stuinfo">
		<id name="stu_id" column="stu_id">
			<!-- 主键生成策略 -->
			<generator class="assigned"></generator>
		</id>
		<property name="stu_name" column="stu_name"></property>
		<property name="stu_sex" column="stu_sex"></property>
		<property name="stu_birth" column="stu_birth"></property>
		<property name="stu_addr" column="stu_addr"></property>
	</class>
</hibernate-mapping>
  • BaseDao
package com.it.dao;

import java.util.List;

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

public class BaseDao<E>{
	private Session session=null;
	public BaseDao(Session session){
		this.session=session;
	}
	public void add(E e){
		session.save(e);
	}
	public List<E> 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<Student>{

	public StudentDao(Session session) {
		super(session);
		// TODO Auto-generated constructor stub
	}
	//添加用户数据
	public void add(Student stu){
		super.add(stu);
	}
	public List<Student> 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<Student> findAll(){
		SessionFactory sessionfactory=null;
		Session session=null;
		Transaction tx=null;
		List<Student> 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<Student>{
	private Student stu=new Student();
	private List<Student> 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<Student> getList() {
		return list;
	}
	public void setList(List<Student> list) {
		this.list = list;
	}
	
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值