IDEA整合Spring+Struts+Hibernate项目

新手使用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>

然后爽一波试试好不好使

原文地址:https://www.cnblogs.com/lijchah/p/13065659.html