Struts2、Spring4、Hibernate4整合 超详细教程
Struts2、Spring4、Hibernate4整合实例-下载
项目目的:
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4)搭建项目架构原型。
项目架构原型:Struts2 + Spring4.0+ Hibernate4.2.4。
项目特色:同时使用了Struts2、Spring4、Hibernate4、log4j等库或框架,搭建一个最基本的项目原型。
加入 Spring
-
加入Spring 所需 jar 包
-
配置 web.xml 文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:applicationContext*.xml</param-value> 10 </context-param> 11 12 <listener> 13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 14 </listener> 15 16 17 </web-app>
- 加入 Spring 的配置文件[ applicationContext.xml ]

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 11 12 </beans>
加入Hibernate
- 加入Hibernate所需jar包
- 加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本属性

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 配置 hibernate 的基本属性 --> 8 9 <!-- 方言 --> 10 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 11 12 <!-- 是否显示及格式化 SQL --> 13 <property name="hibernate.show_sql">true</property> 14 <property name="hibernate.format_sql">true</property> 15 16 <!-- 生成数据表的策略 --> 17 <property name="hibernate.hbm2ddl.auto">update</property> 18 19 <!-- 二级缓存相关 --> 20 <!-- ....... --> 21 </session-factory> 22 23 </hibernate-configuration>
- 和 Spring 进行整合
加入 c3p0 和 MySQL 的驱动
新建db.properties

1 jdbc.user=root 2 jdbc.password=1230 3 jdbc.driverClass=com.mysql.jdbc.Driver 4 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 5 6 jdbc.initPoolSize=5 7 jdbc.maxPoolSize=10
在 Spring 的配置文件中配置: 数据源, SessionFactory, 声明式事务

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 11 12 13 <context:annotation-config /> 14 <context:component-scan base-package="com" /> 15 16 <!-- 导入资源文件 --> 17 <context:property-placeholder location="classpath:db.properties"/> 18 19 <!-- 配置 C3P0 数据源 --> 20 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 21 <property name="user" value="${jdbc.user}"></property> 22 <property name="password" value="${jdbc.password}"></property> 23 <property name="driverClass" value="${jdbc.driverClass}"></property> 24 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> 25 26 <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> 27 <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> 28 </bean> 29 30 <!-- 配置 SessionFactory --> 31 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 32 <property name="dataSource" ref="dataSource"></property> 33 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 34 <property name="mappingLocations" value="classpath:com/entities/*.hbm.xml"></property> 35 </bean> 36 37 <!-- 配置 Spring 的声明式事务 --> 38 <!-- 1. 配置 hibernate 的事务管理器 --> 39 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 40 <property name="sessionFactory" ref="sessionFactory"></property> 41 </bean> 42 43 <!-- 2. 配置事务属性 --> 44 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 45 <tx:attributes> 46 <tx:method name="get*" read-only="true"/> 47 <tx:method name="lastNameIsValid" read-only="true"/> 48 <tx:method name="*"/> 49 </tx:attributes> 50 </tx:advice> 51 52 <!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 --> 53 <aop:config> 54 <aop:pointcut expression="execution(* com.service.*.*(..))" id="txPointcut"/> 55 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> 56 </aop:config> 57 58 </beans>
- 小测试
新建实体类Test.java

1 package com.entities; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.GenerationType; 6 import javax.persistence.Id; 7 import javax.persistence.Table; 8 9 @Entity 10 @Table(name = "test") 11 public class Test { 12 13 @Id 14 @GeneratedValue(strategy = GenerationType.IDENTITY) 15 private long id;//主键 16 private String name; 17 18 19 public long getId() { 20 return id; 21 } 22 public void setId(long id) { 23 this.id = id; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 }
hibernate.cfg.xml 添加

1 <session-factory> 2 <!-- 以上 ...--> 3 <mapping class="com.entities.Test"></mapping> 4 </session-factory>
如果成功,数据库内自动生成Test表
加入 Struts2
-
加入 jar 包: 若有重复的 jar 包, 则需要删除版本较低的.
- 在 web.xml 文件中配置 Struts2 的 Filter

1 <!-- 配置 Struts2 的 Filter --> 2 <filter> 3 <filter-name>struts2</filter-name> 4 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 5 </filter> 6 7 <filter-mapping> 8 <filter-name>struts2</filter-name> 9 <url-pattern>/*</url-pattern> 10 </filter-mapping>
- 加入 Struts2 的配置文件,新建struts.xml

1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <package name="default" namespace="/" extends="struts-default"> 9 10 <action name="test"> 11 <result>index.jsp</result> 12 </action> 13 14 </package> 15 16 </struts>
- 测试 在浏览器输入http://localhost:8080/S2S4H4/test 若无异常,整个框架搭建完毕
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)