整合Struts2、Hibernate、Spring

将项目中的对象和对象之间的管理,纳入spring容器,由spring管理

1 实现spring+hibernate集成

1.1 新建web项目

建立项目的包结构(package)

1.2加入jar包

1.3 建立pojo类

 1 package org.guangsoft.pojo;
 2 /***
 3  * 定部门的pojo类
 4  * **/
 5 public class Dept
 6 {
 7     private Integer did;
 8     private String dname;
 9     private String ddesc;
10     public Integer getDid()
11     {
12         return did;
13     }
14     public void setDid(Integer did)
15     {
16         this.did = did;
17     }
18     public String getDname()
19     {
20         return dname;
21     }
22     public void setDname(String dname)
23     {
24         this.dname = dname;
25     }
26     public String getDdesc()
27     {
28         return ddesc;
29     }
30     public void setDdesc(String ddesc)
31     {
32         this.ddesc = ddesc;
33     }
34 }

1.4 建立pojo的映射文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping package="com.bjsxt.pojo">
 6     <!-- 类 到 表 -->
 7     <class name="Dept" table="t_dept">
 8         <id name="did" column="did" type="java.lang.Integer">
 9             <generator class="native"></generator>
10         </id>
11         <!-- 其他简单属性 -->
12         <property name="dname" column="dname" type="java.lang.String"></property>
13         <property name="ddesc" column="ddesc" type="java.lang.String"></property>
14     </class>
15 </hibernate-mapping>

1.5建立Dao接口

1 package org.guangsoft.dao;
2 import org.guangsoft.pojo.Dept;
3 /**
4  * 部门数据访问接口
5  * ***/
6 public interface DeptDao
7 {
8     public void addDept(Dept dept);
9 }

1.6建立Dao接口的实现类

 1 package org.guangsoft.dao.impl;
 2 import org.hibernate.SessionFactory;
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 5 import org.springframework.stereotype.Repository;
 6 import org.guangsoft.dao.DeptDao;
 7 import org.guangsoft.pojo.Dept;
 8 /***
 9  * 建立dao接口实现类:
10  * extends HibernateDaoSupport :
11  * 完成在dao类中获得session对象,hibernateTempldate对象
12  * ***/
13 @Repository
14 public class DeptDaoImpl extends HibernateDaoSupport
15 implements DeptDao
16 {
17     /***
18      * 给父类注入sessionFactory通过自动装配
19      * ***/
20     // private SessionFactory sessionFactory;
21     @Autowired
22     public void setSessionFactory01(SessionFactory sessionFactory)
23     {
24         super.setSessionFactory(sessionFactory);
25     }
26     @Override
27     public void addDept(Dept dept)
28     {
29         super.getHibernateTemplate().save(dept);
30     }
31 }

1.7建立业务service接口

1 package org.guangsoft.service;
2 import org.guangsoft.pojo.Dept;
3 /**
4  * 部门的业务接口
5  * ***/
6 public interface DeptService
7 {
8     public void saveDeptService(Dept dept);
9 }

1.8 建立service接口实现类

 1 package org.guangsoft.service.impl;
 2 import org.guangsoft.dao.DeptDao;
 3 import org.guangsoft.pojo.Dept;
 4 import org.guangsoft.service.DeptService;
 5 /***
 6  * 部门业务接口实现类
 7  * ***/
 8 public class DeptServiceImpl implements DeptService
 9 {
10     // 声明dao对象
11     private DeptDao deptDao;
12     @Override
13     public void saveDeptService(Dept dept)
14     {
15         deptDao.addDept(dept);
16     }
17 }

1.9配置spring容器

 applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的约束 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 9      http://www.springframework.org/schema/aop
10      http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
11      http://www.springframework.org/schema/context
12      http://www.springframework.org/schema/context/spring-context-4.1.xsd
13       http://www.springframework.org/schema/tx
14      http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
15      ">
16     <!-- 加载db.xml -->
17     <import resource="db.xml" />
18     <!-- 加载transaction.xml -->
19     <import resource="transaction.xml" />
20     <!-- 开启注解扫描 -->
21     <context:component-scan
22         base-package="org.guangsoft.action,org.guangsoft.service.impl,
23     org.guangsoft.dao.impl"></context:component-scan>
24 </beans>

db.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的约束 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 9      http://www.springframework.org/schema/aop
10      http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
11      http://www.springframework.org/schema/context
12      http://www.springframework.org/schema/context/spring-context-4.1.xsd
13       http://www.springframework.org/schema/tx
14      http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
15      ">
16     <!-- 配置数据库连接池(DataSource) -->
17     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
18         <!-- 输入连接池的属性 -->
19         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
20         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>
21         <property name="user" value="root"></property>
22         <property name="password" value="1111"></property>
23     </bean>
24     <!-- 实例化sessionFactory -->
25     <bean id="sessionFactory"
26         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
27         <!--注入数据库连接池 -->
28         <property name="dataSource" ref="dataSource"></property>
29         <!-- 配置hibernate的特性 -->
30         <property name="hibernateProperties">
31             <props>
32                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
33                 <prop key="hibernate.show_sql">true</prop>
34                 <prop key="hibernate.format_sql">true</prop>
35                 <prop key="hibernate.hbm2ddl.auto">update</prop>
36             </props>
37         </property>
38         <!-- 加载hibernate的映射文件 -->
39         <property name="mappingResources">
40             <list>
41                 <value>org/guangsoft/pojo/Dept.hbm.xml</value>
42             </list>
43         </property>
44     </bean>
45 </beans>
46  

 Transaction.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的约束 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 9      http://www.springframework.org/schema/aop
10      http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
11      http://www.springframework.org/schema/context
12      http://www.springframework.org/schema/context/spring-context-4.1.xsd
13       http://www.springframework.org/schema/tx
14      http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
15      ">
16     <!-- 实例化事务管理器对象 -->
17     <bean id="transactionManager"
18         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
19         <!-- 注入sessionFactory -->
20         <property name="sessionFactory" ref="sessionFactory">
21         </property>
22     </bean>
23     <!-- 声明事务特性 -->
24     <tx:advice id="txAdvice" transaction-manager="transactionManager">
25         <tx:attributes>
26             <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" />
27             <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" />
28             <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" />
29             <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" />
30             <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT"
31                 read-only="true" />
32             <tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT"
33                 read-only="true" />
34             <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"
35                 read-only="true" />
36         </tx:attributes>
37     </tx:advice>
38     <!-- 进行aop的配置 -->
39     <aop:config>
40         <!-- 声明切入点 -->
41         <aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
42             id="pc" />
43         <!-- 织入 -->
44         <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
45     </aop:config>
46 </beans>
47  

1.10 建立测试类

 1 import org.junit.Test;
 2 import org.springframework.context.ApplicationContext;
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 public class TestApp
 5 {
 6     @Test
 7     public void addDept()
 8     {
 9         // 加载spring的配置文件,获得bean容器
10         ApplicationContext ac = new
11         ClassPathXmlApplicationContext("applicationContext.xml");
12         // 获得bean对象
13         DeptService deptService = (DeptService) ac.getBean("deptServiceImpl");
14         // 添加部门
15         Dept dept = new Dept();
16         dept.setDname("安慰部");
17         dept.setDdesc("逗你玩");
18         deptService.saveDeptService(dept);
19     }
20 }

2 spring+struts2集成

2.1加入jar

Struts2-spring-plugin.jar

2.2建立Action

 1 import org.springframework.beans.factory.annotation.Autowired;
 2 import org.springframework.context.annotation.Scope;
 3 import org.springframework.stereotype.Controller;
 4 import com.opensymphony.xwork2.Action;
 5 import com.opensymphony.xwork2.ModelDriven;
 6 @Controller
 7 @Scope("prototype")
 8 // DeptAction在spring容器中是非单例的
 9 public class DeptAction implements ModelDriven<Dept>
10 {
11     // 自动装配
12     @Autowired
13     private DeptService deptService;
14     // 声明部门对象
15     private Dept dept = new Dept();
16     @Override
17     public Dept getModel()
18     {
19         return dept;
20     }
21     /***
22      * 处理部门的添加请求
23      * **/
24     public String addDept()
25     {
26         deptService.saveDeptService(dept);
27         return Action.SUCCESS;
28     }
29 }

2.3进行struts2的配置

 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 <struts>
 6     <package name="dept" namespace="/" extends="struts-default">
 7         <!-- class:是Action在spring容器中对应的id -->
 8         <action name="deptAction_*" class="deptAction" method="{1}">
 9             <result>/index.jsp</result>
10         </action>
11     </package>
12 </struts>

3.4在web.xml中配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6     <display-name></display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.jsp</welcome-file>
 9     </welcome-file-list>
10     <!-- 加载spring的配置文件 -->
11     <!-- 配置上下的初始化参数application -->
12     <context-param>
13         <param-name>contextConfigLocation</param-name>
14         <param-value>classpath:applicationContext.xml</param-value>
15     </context-param>
16     <!--通过监听器加载spring的配置iwenijan -->
17     <listener>
18         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
19     </listener>
20     <!-- 配置open Session in view -->
21     <filter>
22         <filter-name>osiv</filter-name>
23         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
24     </filter>
25     <filter-mapping>
26         <filter-name>osiv</filter-name>
27         <url-pattern>*.action</url-pattern>
28     </filter-mapping>
29     <!-- 配置struts2的核心控制器 -->
30     <filter>
31         <filter-name>struts</filter-name>
32         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
33     </filter>
34     <filter-mapping>
35         <filter-name>struts</filter-name>
36         <url-pattern>*.action</url-pattern>
37     </filter-mapping>
38     <filter-mapping>
39         <filter-name>struts</filter-name>
40         <url-pattern>*.jsp</url-pattern>
41     </filter-mapping>
42 </web-app>
43  

2.5建立UI页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@
 3 taglib prefix="s" uri="/struts-tags"%>
 4 <%
 5 String path =
 6 request.getContextPath();
 7 String basePath =
 8 request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 9 %>
10 <!DOCTYPE HTML>
11 <html>
12     <head>
13         <base href="<%=basePath%>">
14             <title>My JSP 'index.jsp' starting page</title>
15             <meta http-equiv="pragma" content="no-cache">
16             <meta http-equiv="cache-control" content="no-cache">
17             <meta http-equiv="expires" content="0">
18     </head>
19     <body>
20         <s:form action="deptAction_addDept.action" method="post" theme="simple">
21             <div>
22                 部门名称:
23                 <s:textfield name="dname"></s:textfield>
24             </div>
25             <div>
26                 部门描述:
27                 <s:textfield name="ddesc"></s:textfield>
28             </div>
29             <div>
30                 <s:submit value="提交"></s:submit>
31             </div>
32         </s:form>
33     </body>
34 </html>
原文地址:https://www.cnblogs.com/guanghe/p/6131979.html