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

1 实现spring+hibernate集成

1.1 新建web项目

建立项目的包结构(package)

1.2加入jar包

1.3 建立pojo类

 package org.guangsoft.pojo;
/***
* 定部门的pojo类
* **/
public class Dept
{
private Integer did;
private String dname;
private String ddesc;
public Integer getDid()
{
return did;
}
public void setDid(Integer did)
{
this.did = did;
}
public String getDname()
{
return dname;
}
public void setDname(String dname)
{
this.dname = dname;
}
public String getDdesc()
{
return ddesc;
}
public void setDdesc(String ddesc)
{
this.ddesc = ddesc;
}
}

1.4 建立pojo的映射文件

 <?xml version="1.0" encoding="UTF-8"?>
<!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.bjsxt.pojo">
<!-- 类 到 表 -->
<class name="Dept" table="t_dept">
<id name="did" column="did" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<!-- 其他简单属性 -->
<property name="dname" column="dname" type="java.lang.String"></property>
<property name="ddesc" column="ddesc" type="java.lang.String"></property>
</class>
</hibernate-mapping>

1.5建立Dao接口

 package org.guangsoft.dao;
import org.guangsoft.pojo.Dept;
/**
* 部门数据访问接口
* ***/
public interface DeptDao
{
public void addDept(Dept dept);
}

1.6建立Dao接口的实现类

 package org.guangsoft.dao.impl;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.guangsoft.dao.DeptDao;
import org.guangsoft.pojo.Dept;
/***
* 建立dao接口实现类:
* extends HibernateDaoSupport :
* 完成在dao类中获得session对象,hibernateTempldate对象
* ***/
@Repository
public class DeptDaoImpl extends HibernateDaoSupport
implements DeptDao
{
/***
* 给父类注入sessionFactory通过自动装配
* ***/
// private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory01(SessionFactory sessionFactory)
{
super.setSessionFactory(sessionFactory);
}
@Override
public void addDept(Dept dept)
{
super.getHibernateTemplate().save(dept);
}
}

1.7建立业务service接口

 package org.guangsoft.service;
import org.guangsoft.pojo.Dept;
/**
* 部门的业务接口
* ***/
public interface DeptService
{
public void saveDeptService(Dept dept);
}

1.8 建立service接口实现类

 package org.guangsoft.service.impl;
import org.guangsoft.dao.DeptDao;
import org.guangsoft.pojo.Dept;
import org.guangsoft.service.DeptService;
/***
* 部门业务接口实现类
* ***/
public class DeptServiceImpl implements DeptService
{
// 声明dao对象
private DeptDao deptDao;
@Override
public void saveDeptService(Dept dept)
{
deptDao.addDept(dept);
}
}

1.9配置spring容器

applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 加载db.xml -->
<import resource="db.xml" />
<!-- 加载transaction.xml -->
<import resource="transaction.xml" />
<!-- 开启注解扫描 -->
<context:component-scan
base-package="org.guangsoft.action,org.guangsoft.service.impl,
org.guangsoft.dao.impl"></context:component-scan>
</beans>

db.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 配置数据库连接池(DataSource) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 输入连接池的属性 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="1111"></property>
</bean>
<!-- 实例化sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--注入数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate的特性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载hibernate的映射文件 -->
<property name="mappingResources">
<list>
<value>org/guangsoft/pojo/Dept.hbm.xml</value>
</list>
</property>
</bean>
</beans>

Transaction.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">
<!-- 实例化事务管理器对象 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<!-- 声明事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" />
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"
read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 进行aop的配置 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
id="pc" />
<!-- 织入 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
</aop:config>
</beans>

1.10 建立测试类

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp
{
@Test
public void addDept()
{
// 加载spring的配置文件,获得bean容器
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
// 获得bean对象
DeptService deptService = (DeptService) ac.getBean("deptServiceImpl");
// 添加部门
Dept dept = new Dept();
dept.setDname("安慰部");
dept.setDdesc("逗你玩");
deptService.saveDeptService(dept);
}
}

2 spring+struts2集成

2.1加入jar

Struts2-spring-plugin.jar

2.2建立Action

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Scope("prototype")
// DeptAction在spring容器中是非单例的
public class DeptAction implements ModelDriven<Dept>
{
// 自动装配
@Autowired
private DeptService deptService;
// 声明部门对象
private Dept dept = new Dept();
@Override
public Dept getModel()
{
return dept;
}
/***
* 处理部门的添加请求
* **/
public String addDept()
{
deptService.saveDeptService(dept);
return Action.SUCCESS;
}
}

2.3进行struts2的配置

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="dept" namespace="/" extends="struts-default">
<!-- class:是Action在spring容器中对应的id -->
<action name="deptAction_*" class="deptAction" method="{1}">
<result>/index.jsp</result>
</action>
</package>
</struts>

3.4在web.xml中配置

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring的配置文件 -->
<!-- 配置上下的初始化参数application -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--通过监听器加载spring的配置iwenijan -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置open Session in view -->
<filter>
<filter-name>osiv</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>osiv</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置struts2的核心控制器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>

2.5建立UI页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@
taglib prefix="s" uri="/struts-tags"%>
<%
String path =
request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<s:form action="deptAction_addDept.action" method="post" theme="simple">
<div>
部门名称:
<s:textfield name="dname"></s:textfield>
</div>
<div>
部门描述:
<s:textfield name="ddesc"></s:textfield>
</div>
<div>
<s:submit value="提交"></s:submit>
</div>
</s:form>
</body>
</html>

整合Struts2、Hibernate、Spring的更多相关文章

  1. 重新学习之spring第四个程序,整合struts2+hibernate+spring

    第一步:导入三大框架的jar包(struts2.3.16.1+hibernate3.2+spring3.2.4) 第二步:编写web.xml 和struts.xml和applicationContex ...

  2. struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决

    最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myec ...

  3. Struts2+Hibernate+Spring 整合示例

    转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...

  4. 工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境

    上文中我们介绍<工作笔记2.软件开发经常使用工具> 从今天開始本文将教大家怎样进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个 ...

  5. 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效

    基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...

  6. Struts2+hibernate+spring 配置事物

    今天自信看了看hibernate的事物配置问题,转载了其他人的日志,仅用来学习. struts+hibernate+spring事务配置 (2009-01-14 21:49:47) 转载▼ 标签: i ...

  7. Struts2+Hibernate+Spring 整合示例[转]

    原文 http://blog.csdn.net/tkd03072010/article/details/7468769 Spring整合Struts2.Hibernate原理概述: 从用户角度来看,用 ...

  8. Spring整合Struts2,Hibernate的xml方式

    作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL. 写了一个简单的例子,用的工具是IntelliJ Id ...

  9. 整合Struts2与Spring

    一.需要的JAR文件为:Spring和Struts2框架本身需要的JAR文件以及他们所依赖的JAR文件

  10. cpj-swagger分别整合struts2、spring mvc、servlet

    cpj-swagger 原文地址:https://github.com/3cpj/swagger 1. Swagger是什么? 官方说法:Swagger是一个规范和完整的框架,用于生成.描述.调用和可 ...

随机推荐

  1. JQuery-EasyUI DataGrid CRUD

    ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页! 数据表: 学生表:学生编号.姓名.性别.班级编号.年龄 班级表:班级编号. ...

  2. 使用 Flexbox 的居中布局

  3. JSON格式转换(javascript)

    使用ajax从后台抓取数据后,如果有多个值,可以使用json传值. ajax例子如下,在返回的类型里面,可以是文本型(text),JSON格式(json),超文本类型(html),XML文件类型(xm ...

  4. 修改Ubuntu12.04 左侧启动器Launcher图标大小,以及如何隐藏启动器?

    在 VirtualBox 中安装了 Ubuntu 12,一直使用 2D 桌面,3D桌面没用上,估计是电脑配置低的问题. 左边启动器的图标特别大,占据了很多的桌面空间,打算调小点.奇怪的是,在“系统设置 ...

  5. Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框

    转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 今天给大家带来一个很实用的小控件ClearEditText,就是在Andr ...

  6. BZOJ4519——[cqoi2016]不同的最小割

    0.题意:求两点之间的最小割的不同的总量 1.分析:裸的分治+最小割,也叫最小割树或GH树,最后用set搞一下就好 #include <set> #include <queue> ...

  7. Android--多线程之Handler

    前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决Android应用中多线程的问题,在Android中不允许Activity新启动的线程访问该Activity里的U ...

  8. 本科小白学ROS 和 SLAM(一):杂谈

    本人最近才迷恋上ROS(Robot Operating System),准确的说应该是6月中旬,具体的记不清了(可能是年纪大了,容易健忘).对于一个电子DIY的狂热爱好者来说,我在校的梦想就是做一个属 ...

  9. PHP微信支付开发实例

    这篇文章主要为大家详细介绍了PHP微信支付开发过程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 PHP微信支付开发过程,分享给大家,供大家参考,具体内容如下 1.开发环境 Thinkphp 3. ...

  10. PictureCutting图片批量裁切(裁剪)工具

    PictureCutting图片批量裁切(裁剪)工具 写这个工具的原因是因为在获取一个软件的皮肤包中的图片的时候需要进行批量的裁切工作,而有没有找到在linux下简单好用的工具,干脆就用QT写了一个. ...