新手使用idea整合Spring+Struts+Hibernate项目

项目所需jar下载:

https://download.csdn.net/download/weixin_44906002/12505287

1、如图,使用idea新建一个struts的web项目,点击next,编辑项目名称,点击finish

2、在web/WEB-INF下创建lib文件夹,导入下载好的jar包,然后鼠标右键点击lib,选择添加为库

3、构建如下图的项目结构:

4、编辑beans.xml,该配置文件可以创建项目的时候选择spring项目,自动创建,路径随意,一般自动创建的applicationContext.xml在web/WEB-INF文件夹下面。

对beans.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:context="http://www.springframework.org/schema/context"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans.xsd
9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context-4.1.xsd
11 http://www.springframework.org/schema/cache
12 http://www.springframework.org/schema/cache/spring-cache.xsd
13 http://www.springframework.org/schema/aop
14 http://www.springframework.org/schema/aop/spring-aop.xsd
15 http://www.springframework.org/schema/tx
16 http://www.springframework.org/schema/tx/spring-tx.xsd">
17 <!--开启包扫描-->
18 <context:component-scan base-package="com.cheng.*"/>
19 <!--配置数据源,这里使用的是c3p0连接池-->
20 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
21 <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
22 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shape?userUnicode=true&amp;characterEncoding=utf-8"/>
23 <property name="user" value="root"/>
24 <property name="password" value="lijc19980218"/>
25 </bean>
26 <!--配置Hibernate的会话工厂bean-->
27 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
28 <!--注入数据源-->
29 <property name="dataSource" ref="dataSource"/>
30 <!--Hibernate配置文件位置-->
31 <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
32 </bean>
33
34 <!--配置事务管理器,将事务管理交给spring-->
35 <bean id="txManger" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
36 <property name="sessionFactory" ref="sessionFactory"/>
37 </bean>
38 <!--定义事务通知-->
39 <tx:advice id="txAdvice" transaction-manager="txManger">
40 <tx:attributes>
41 <!--事务方法-->
42 <tx:method name="save*" propagation="REQUIRED"/>
43 <tx:method name="add*" propagation="REQUIRED"/>
44 <!--...-->
45 </tx:attributes>
46 </tx:advice>
47
48 <!--配置AOP-->
49 <aop:config>
50 <aop:pointcut id="service" expression="execution(* com.cheng.service..*.*(..))"/>
51 <aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
52 </aop:config>
53
54 </beans>

5、配置hibernate.cfg.xml文件

 1 <?xml version='1.0' encoding='utf-8'?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD//EN"
4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5 <hibernate-configuration>
6 <session-factory>
7 <!--映射文件-->
8 <mapping resource="com/cheng/bean/circle.hbm.xml"/>
9 </session-factory>
10 </hibernate-configuration>

6、配置struts.xml,让spring融合struts

 1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE struts PUBLIC
4 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
5 "http://struts.apache.org/dtds/struts-2.5.dtd">
6
7 <struts>
8 <!--配置struts为开发模式-->
9 <constant name="struts.devMode" value="true"/>
10 <!--配置主题为简单主题-->
11 <constant name="struts.ui.theme" value="simple"/>
12 <!--配置请求后缀-->
13 <constant name="struts.action.extension" value="do"/>
14 <!--使用Spring的对象工厂-->
15 <constant name="struts.objectFactory" value="spring"/>
16 <package name="default" extends="struts-default" namespace="/">
17
18 </package>
19 </struts>

7、配置web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
5 version="4.0">
6 <context-param>
7 <param-name>contextConfigLocation</param-name>
8 <param-value>classpath:beans.xml</param-value>
9 </context-param>
10 <listener>
11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
12 </listener>
13 <filter>
14 <filter-name>struts2</filter-name>
15 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
16 </filter>
17 <filter-mapping>
18 <filter-name>struts2</filter-name>
19 <url-pattern>/*</url-pattern>
20 </filter-mapping>
21 </web-app>

<context-param></context-param>存放的是beans.xm配置文件路径,添加监听以便于在部署Web项目时,启动和初始化Spring容器。

8、hibernate映射文件,例如:

 1 <?xml version='1.0' encoding='UTF-8'?>
2 <!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5
6 <hibernate-mapping>
7 <class name="完全限定类名" table="映射数据库表名">
8 <id name="主键">
9 <generator class="assigned"></generator>
10 </id>
11
12 <property name="其他字段"></property>
13
14 </class>
15
16 </hibernate-mapping>

然后爽一波试试好不好使

IDEA整合Spring+Struts+Hibernate项目的更多相关文章

  1. 整合spring和hibernate框架

    一)整合spring和hibernate框架整合要点:(1)数据源配置在Spring的配置文件中,供Spring和Hibernate框架共同使用:(2)不再需要hibernate.hbm.xml配置文 ...

  2. Spring+Struts+Hibernate 简介(转)

    http://blog.csdn.net/slnqnd/article/details/1772910/ Struts2.0 +Hibernate 3.2 +Spring 2.0 一.         ...

  3. Spring + Spring MVC + Hibernate项目开发集成(注解)

    在自己从事的项目中都是使用xml配置的方式来进行的,随着项目的越来越大,会发现配置文件会相当的庞大,这个不利于项目的进行和后期的维护.于是考虑使用注解的方式来进行项目的开发,前些日子就抽空学习了一下. ...

  4. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-1]

    示例数据库test,用户信息表

  5. spring+struts+hibernate整合

    spring整合: 1:添加配置文件和相应的spring jar包(记得一定要加上commons-logging的jar包,有坑****) 2:创建date对象,如果成功则spring的环境ok

  6. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-5]

    测试项目 目录结构

  7. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-4]

    引入Hibernate 在pom.xml中加入jar包引用 <!-- hibernate4 --> <dependency> <groupId>org.hibern ...

  8. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-3]

    引入Struts2 在pom.xml中加入jar包 <!-- struts2 --> <dependency> <groupId>org.apache.struts ...

  9. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-2]

    引入Spring 修改 pox.xml 文件 添加jar包引用 <!-- spring3 --> <dependency> <groupId>org.springf ...

  10. 声明式事务-整合Spring、Hibernate

    编程式事务:通过编码的方式,让事务处理的代码侵入到核心的业务代码中. 声明式事务:完成了事务处理的代码和业务核心代码的解耦合.提供事务处理代码的复用性和降低维护成本. 声明式事务:aop最典型的应用. ...

随机推荐

  1. Android 12(S) MultiMedia(十一)从MPEG2TSExtractor到MPEG2-TS

    本节主要学习内容是看看MPEG2TSExtractor是如何处理TS流的. 相关代码位置: frameworks/av/media/extractors/mpeg2/MPEG2TSExtractor. ...

  2. .NETCore Nuget 发布包含静态文件 content file

    .NETCore 在.csproj引用资源中标记pack配置 <pack>true</pack>1例如 <ItemGroup> <Content Includ ...

  3. .NET 6+Semantic Kernel快速接入OpenAI接口

    大家好,我是Edison. 今天我们快速地使用Semantic Kernel来集成OpenAI,使用20来行代码快速实现一个简单的AIGC应用. 这里,我就不多介绍Semantic Kernel了,包 ...

  4. 恭喜PaddleOCRSharp开源项目通过PaddleOCR社区常规赛优秀项目首次评选

    PaddleOCR优秀社区项目推荐: PaddleOCR社区常规赛首次评选结果已于日前出炉,本次优秀项目推广为大家带来的是[部署篇]:️ PaddleOCR的.NET调用库:包含文本识别.文本检测.基 ...

  5. ch58x/ch59xADC差分采样NTC电阻获取当前温度

    前言:之前的文章中也有关于使用I2C器件进行温度的采集的文章 采集温度的方式不止使用传感器,也可以使用NTC温敏电阻进行采集,此方法的外围电路较为简单切成本较低,代码也较为容易实现. 实现原理:先通过 ...

  6. Pyinstaller打包exe的反编译——LitCTF 2024(公开赛道)ezpython!!!!!

    这个工具折磨了我很久,搭配题目记录一下... 题目 Die 打包工具: PyInstaller 建议下载GitHub的:GitHub - extremecoders-re/pyinstxtractor ...

  7. Yolov8和Yolov10的差异以及后处理实现

    Yolo模型可分为4个维度的概念 模型版本.数据集.模型变体(Variants).动态/静态模型. Yolo各模型版本进展历史 Yolov(2015年华盛顿大学的 Joseph Redmon 和 Al ...

  8. SRE 必备利器:域名 DNS 探测排障工具

    问题背景 访问某个 HTTP 域名接口,偶发性超时,原因可能多种多样,比如 DNS 解析问题.网络质量问题.对端服务负载问题等,在客户端没有良好埋点的情况下,排查起来比较费劲,只能挨个方向尝试,这里送 ...

  9. redis数据类型篇

    redis数据类型官网资料,https://redis.io/docs/manual/data-types/ 生产环境下的redis实况图 超哥这个redis实例里,db0库有140万个key. 1. ...

  10. 点击li 该li变色

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...