<strong>
一共有6步(文章底部附有源码下载地址,刚学完ssh的可以借鉴)</strong> 1 写一个Hibernate应用,完成用户的增加
1) User实体
2)UserDao接口
save(User u)
3)UserDaoImpl实现UserDao
save(User u){
//用hibernate api
} 2 让spring注入SessionFactory 1)在上一个应用中导入Spring的jar包
除了基础的6个包,还得导入2个jar包
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar 2) 增加配置文件beans.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean> <bean id="userDao" class="包名.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory">
</bean> 3) 在测试方法中,获得userDao,然后测试save() UserDao userDao= (UserDao)cxt.getBean("userDao"); 3 添加数据源,并注入到SessionFactory 1) 需要导入2个包
commons-dbcp.jar
commons-pool.jar 注:数据源的第3方实现有非常多,现我们用其中一种,commons-dbcp 2) 配置Datasource
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean> 3)在SessionFactory Bean中注入dataSource <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入DataSource Bean-->
<property name="dataSource" ref="myDataSource"/> <property name="mappingResources">
<list>
<value>com/toceansoft/entity/User.hbm.xml</value>
</list>
</property> <property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
</value>
</property>
</bean> 4) 测试结果 4 使用spring提供的HibernateTemplate 1) 在Dao中添加
private HibernateTemplate hibernateTemplate;
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
} 2)修改方法,改成使用hibernateTemplate
save(o)
hibernateTemplate.save(o); 5 使用spring在业务层切入事务 1)在业务层写一个接口UserService
public void add(User u);
2) 写一个实现类UserServiceImpl实现接口UserService
public void add(User u){}
3)实现类UserServiceImpl提供userDao的get/set方法 4)在beans.xml中配置UserServiceImpl 的Bean,并注入userDao 5)配置事务管理器Bean
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
6) 配置事务通知
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 包括隔离级别,事务传播属性。。。 -->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
7)配置aop切面
<aop:config>
<aop:pointcut id="serviceMethods"
expression="execution(* com.toceansoft.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
8)测试结果
9)在add方法内抛出异常,再测试观察结果
throw new RuntimeException(); 6 整合Spring与Struts
1)在把上面的java 应用变成web应用
--把上面应用Src下的所有东东复制到web应用的src 2)导入struts2开发的基础jar包,还需要一个特殊包
struts-spring-plugin.jar 导入与struts2整合的spring相应的包
org.springframework.web.struts-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar 3) 把beans.xml 复制到WEB-INF,并且改名为applicationContext.xml(可选的) 4) 在web.xml中配置spring启动的监听器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,classpath*:applicationContext2.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 5)在web.xml配置Struts2的启动
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 6)在Web包下写UserAction,并且提供业务层UserSerivce的set方法 7)在appplicationContext.xml中配置UserAction bean
<bean id="userAction" class="com.toceansoft.web.action.UserAction" scope="prototype">
<property name="userService" ref="userService" />
</bean>
注:scope="prototype" 8) 在struts.xml中配置UserAction
<!-- 跳转到添加产品页面 -->
<action name="User_toAdd" >
<result>/User_add.jsp</result>
</action> <!-- 添加产品 -->
<!-- 关键点:这里的class设置action bean在spring配置文件中bean的id -->
<action name="User_add" class="userAction" method="add">
<result name="success">/grobal_message.jsp</result>
</action> 9) 写2个页面
User_add.jsp
grobal_message.jsp 10)部署测试

SSH六部曲的更多相关文章

  1. JDBC编程六部曲

    今天初学jdbc,明白了大致的编程流程,在此总结一下: JDBC编程可以分为六步——六部曲: * 第一步:注册驱动. * 1.1 获取驱动对象 * 1.2 注册驱动 * 第二步:获取数据库连接 * 第 ...

  2. docker-compose下的java应用启动顺序两部曲之二:实战

    上篇回顾 本文是<docker-compose下的java应用启动顺序两部曲>的终篇,在上一篇<docker-compose下的java应用启动顺序两部曲之一:问题分析>中,我 ...

  3. c语言项目开发流程二部曲

    一.在第一部曲中我们介绍了电子词典项目开发的前5步,下面继续我们的步伐. 6.函数接口设计,这一步不是一蹴而就的,在项目进行中得不断修改,下面是我电子词典项目接口. /**************函数 ...

  4. docker-compose下的java应用启动顺序两部曲之一:问题分析

    在docker-compose编排多个容器时,需要按实际情况控制各容器的启动顺序,本文是<docker-compose下的java应用启动顺序两部曲>的第一篇,文中会分析启动顺序的重要性, ...

  5. python --- Socketserver N部曲(1)

    曲一 socketserver 是为了简化服务器端开发而产生的,是一个高级的标准库.(背景介绍完毕,开始干) 一些概念 来自源码的一张图片,简洁又FengSao +------------+ | Ba ...

  6. c# ado 连接数据库 六步曲

    建立连接分为六步:1.定义连接字符串,oracle 的连接字符串为: private static string connString = "Data Source=192.168.1.13 ...

  7. SSM框架入门和搭建 十部曲

    又快到毕业设计的时候了,有的学弟说想用ssm做毕业设计,在网上找到资料看不懂,基础差.我就帮他写了一个demo,顺便也整理一下. SSM框架,顾名思义,就是Spring+SpringMVC+mybat ...

  8. Struts2.3.34+Hibernate 4.x+Spring4.x 整合二部曲之上部曲

    1 导入jar包 可以复制jar包或maven导入,本文最后会给出github地址 2 导入log4j.properties文件 og4j.appender.stdout=org.apache.log ...

  9. 共分为六部完成根据模板导出excel操作

    第一步.设置excel模板路径(setSrcPath) 第二步.设置要生成excel文件路径(setDesPath) 第三步.设置模板中哪个Sheet列(setSheetName) 第四步.获取所读取 ...

随机推荐

  1. ZendFramework2学习笔记 json和ajax

    单程: View服务寄存器ViewJsonStrategy之后,有可能直接在控制器action是使用JsonViewModel输出json的数据. 注冊ViewJsonStrategy: //modu ...

  2. WPF学习(8)数据绑定

    说到数据绑定,其实这并不是一个新的玩意儿.了解asp.net的朋友都知道,在asp.net中已经用到了这个概念,例如Repeater等的数据绑定.那么,在WPF中的数据绑定相比较传统的asp.net中 ...

  3. gem 安装nokigiri

    在mac上安装nokogiri的时候各种报错,终于安装成功一次,备份命令. ➜ ~ sudo gem install nokogiri -- --use-system-libraries --with ...

  4. 必须掌握的八个cmd命令

    原文:必须掌握的八个cmd命令 一.ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的 ...

  5. Web 前端开发环境

    创建 Web 前端开发环境 Web 前端开发涉及多种工具,这里将常用工具的安装和配置进行说明,提供了详细的说明,为后继的开发创建一个坚实的基础. 本文介绍的工具有:NodeJS, NPM, Bower ...

  6. Rust这个新的语言

    Rust这个新的语言 Rust初步(七):格式化 摘要: 在Rust中,如果要进行屏幕输出,或者写入到文件中,需要对数据进行格式化.这一篇总结一下它所支持的几种格式化方式. 这篇文章参考了以下官方文档 ...

  7. iOS类别(Category)和扩展(Extension,匿名类)

    Category在iOS在开发常用. 特别是对于系统扩展上课时间.我们不能继承系统类.直接添加到系统类方法,最大程度上体现Objective-C动态语言特征. #import @interface N ...

  8. about greenplum collection tool

    three collection tool for greenplum:pstack.strace.gcore.                                            ...

  9. mysql三学习sql声明学习

    SQL 是一门 ANSI 的标准计算机语言,用来訪问和操作数据库系统.SQL 语句用于取回和更新数据库中的数据.SQL 可与数据库程序协同工作,比方MySQL. MS Access.DB2.Infor ...

  10. Mybatis基金会: 经常问的问题FAQ

    Mybatis基金会: #{...} 和 ${...} 差额 MyBatis将 #{-} 解释为JDBC prepared statement 参数标记.而将 ${-} 解释为一个字符串替换.非常实用 ...