打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SSH框架整合开发详解(个人笔记)

一.创建数据库并设置编码。

A) create database oa default character set utf8

二.MyEclipse工程

A) 在Myeclipse里创建web工程,并设置编码为utf8.

B) 添加框架环境

1.添加Junit4 libraryMyeclipse自带)

2.添加Struts2环境

①所需Jar

②配置文件:拷贝一个struts.xml模版到src目录,进行适当修改,web.xml里配上需要的代码。

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <!-- 配置为开发模式 -->
  7. <constant name="struts.devMode" value="true" />
  8. <!-- 把扩展名配置为action -->
  9. <constant name="struts.action.extension" value="action" />
  10. <!-- 把主题配置为simple -->
  11. <constant name="struts.ui.theme" value="simple" />
  12. <package name="default" namespace="/" extends="struts-default">
  13. <action name="" class="">
  14. <result name=""></result>
  15. </action>
  16. </package>
  17. <!-- Add packages here -->
  18. </struts>

web.xml

  1. <!-- 配置Struts2的核心的过滤器 -->
  2. <filter>
  3. <filter-name>struts2</filter-name>
  4. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>struts2</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>



3.添加Hibernate环境 

①所需Jar

②配置文件:拷贝一个hibernate.cfg.xml和映射文件*.hbm.xml模版到src目录,并进行适当修改。

hibernate.cfg.xml

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- 1,数据库连接信息 -->
  8. <property name="dialect">
  9. org.hibernate.dialect.MySQL5InnoDBDialect
  10. </property>
  11. <property name="connection.url">jdbc:mysql:///oa</property>
  12. <property name="connection.driver_class">com.jdbc.mysql.Driver</property>
  13. <property name="connection.username">root</property>
  14. <property name="connection.password">root</property>
  15. <!-- 2,其他配置 -->
  16. <property name="show_sql">true</property>
  17. <property name="hbm2ddl.auto">update</property>
  18. <!-- 3,导入映射文件 -->
  19. <mapping resource="" />
  20. </session-factory>
  21. </hibernate-configuration>

User.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="cn.grace.oa.domain">
  6. <class name="User" table="grace_user">
  7. <id name="id">
  8. <generator class="native"/>
  9. </id>
  10. <property name="name" />
  11. </class>
  12. </hibernate-mapping>

4.添加Spring环境

①所需Jar

②配置文件:拷贝一个appicationContext.xml/beans.xml模版到src目录

appicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  8. <!-- 自动扫描与装配bean,包括子包 -->
  9. <context:component-scan base-package="cn.itcast.oa"></context:component-scan>
  10. <!-- 导入外部的properties文件 -->
  11. <context:property-placeholder location="classpath:jdbc.properties"/>
  12. <!-- 配置SessionFactory IOC-->
  13. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  14. <!-- 指定hibernate的配置文件位置 -->
  15. <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
  16. <!-- 配置c3p0数据库连接池 -->
  17. <property name="dataSource">
  18. <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
  19. <!-- 数据连接信息 -->
  20. <property name="jdbcUrl" value="${jdbcUrl}"></property>
  21. <property name="driverClass" value="${driverClass}"></property>
  22. <property name="user" value="${user}"></property>
  23. <property name="password" value="${password}"></property>
  24. <!-- 其他配置 -->
  25. <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
  26. <property name="initialPoolSize" value="3"></property>
  27. <!--连接池中保留的最小连接数。Default: 3 -->
  28. <property name="minPoolSize" value="3"></property>
  29. <!--连接池中保留的最大连接数。Default: 15 -->
  30. <property name="maxPoolSize" value="5"></property>
  31. <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
  32. <property name="acquireIncrement" value="3"></property>
  33. <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
  34. <property name="maxStatements" value="8"></property>
  35. <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
  36. <property name="maxStatementsPerConnection" value="5"></property>
  37. <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
  38. <property name="maxIdleTime" value="1800"></property>
  39. </bean>
  40. </property>
  41. </bean>
  42. <!-- 配置声明式事务管理(采用注解的方式) AOP-->
  43. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  44. <property name="sessionFactory" ref="sessionFactory"></property>
  45. </bean>
  46. <!-- 注解驱动-->
  47. <tx:annotation-driven transaction-manager="txManager"/>
  48. </beans>

5.整合SpringStruts2 (IOC:让struts2action交由容器管理)


src目录下建Junit测试包进行测试

①单独测试struts2,新建TestAction

a) TestAction.java

  1. public class TestAction extends ActionSupport{
  2. @Override
  3. public String execute() throws Exception{
  4. System.out.println("===>TestAction.execute()");
  5. return "success";
  6. }
  7. }

b)在struts.xml里配置相应action

  1. <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->
  2. <action name="test" class="cn.grace.oa.test.TestAction">
  3. <result name="success">/test.jsp</result>
  4. </action>

c)部署测试是否可以通过test.action正常访问test.jsp页面和是否输出"===>TestAction.execute()"


②单独测试Spring,新建SpringTest此时appicationContext.xml里面只需要一个装配和扫描bean的语句,否则可能报错

a) SpringTest.java

  1. public class SpringTest {
  2. private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  3. @Test
  4. public void testBean() throws Exception{
  5. TestAction testAction=(TestAction) ac.getBean("testAction");
  6. System.out.println(testAction);
  7. }
  8. }

b)此时Junit测试不能成功,必须在TestAction类上进行声明,并配置scope

  1. @Controller
  2. @Scope("prototype")
  3. public class TestAction extends ActionSupport{
  4. @Override
  5. public String execute() throws Exception{
  6. System.out.println("==>TestAction.execute");
  7. return "success";
  8. }
  9. }
c)此时再进行Junit测试成功输出testAction.


d)声明一个bean(注解方式,有四种方式,根据不同类用相对应的方式)

效果跟在bean.xml里面写<bean id="" class=""></bean>功能是一样的

@Component("beanName")//不写beanName默认使用类名首字母小写,即testAction

@Controller

@Service

@Repository

配置beanscope,默认为单例,修改为prototype

@Scope("prototype")


③测试Struts2Spring整合

a)在web.xml中配置一个监听器

  1. <!-- 配置Spring的用于初始化容器对象的监听器 -->
  2. <listener>
  3. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  4. </listener>
  5. <context-param>
  6. <param-name>contextConfigLocation</param-name>
  7. <param-value>classpath:applicationContext*.xml</param-value>
  8. </context-param>

b) 加一个jar包:struts2-spring-plugin-2.1.8.1.jar

struts.xml中的actionclass从路径改为bean名称,再进行测试,看是否整合成功

  1. <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->
  2. <!-- 当Struts2与Spring整合后,class属性可以写bean的名称 -->
  3. <action name="test" class="testAction">
  4. <result name="success">/test.jsp</result>
  5. </action>

c)部署测试是否可以通过test.action正常访问test.jsp页面正常访问test.jsp代表整合成功。


6.整合SpringHibernate1.管理SessionFactory实例(只需要一个)2.声明式事务管理)

SessionFactory

a)applicationContext.xml里配置sessionFactoryv(见上文appicationContext.xml),并在hibernate.cfg.xml去掉重复的连接数据库的信息

b)新建一个jdbc.properties用来存放dataSource用到的数据库连接信息,并在applicationContext.xml导入该文件

applicationContext.xml(上文appicationContext.xml已配置)

  1. <!-- 导入外部的properties文件 -->
  2. <context:property-placeholder location="classpath:jdbc.properties"/>

jdbc.properties

  1. jdbcUrl = jdbc:mysql:///oa?characterEncoding=utf8
  2. driverClass = com.mysql.jdbc.Driver
  3. user = root
  4. password = root

c)SpringTest类里写测试方法,进行Junit测试。

  1. @Test
  2. public void testSessionFactory() throws Exception{
  3. SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");
  4. System.out.println(sessionFactory);
  5. }

声明式事务管理

a)applicationContext.xml里配置声明式事务管理(上文appicationContext.xml已配置)

  1. <!-- 配置声明式事务管理(采用注解的方式) AOP-->
  2. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  3. <property name="sessionFactory" ref="sessionFactory"></property>
  4. </bean>
  5. <!-- 注解驱动-->
  6. <tx:annotation-driven transaction-manager="txManager"/>

b)测试声明式事务管理(测试回滚)

i.新建一个用于测试的实体类:User.java(只需定义idname及其构造方法即可)

ii.根据User写对应的User.hbm.xml映射文件

iii.hibernate.cfg.xml导入映射文件

<mapping resource="cn/grace/oa/domain/User.hbm.xml" />

iV.新建一个TestService类

  1. @Service("testService")
  2. public class TestService {
  3. @Resource
  4. private SessionFactory sessionFactory;
  5. @Transactional
  6. public void saveTwoUsers(){
  7. Session session=sessionFactory.getCurrentSession();
  8. session.save(new User());
  9. int a=1/0;//会抛异常,因为声明了事务,因此会回滚
  10. session.save(new User());
  11. }
  12. }

V.在SpringTest类里写测试方法,进行Junit测试。

  1. @Test
  2. public void testTransaction() throws Exception{
  3. TestService testService=(TestService)ac.getBean("testService");
  4. testService.saveTwoUsers();
  5. }

Vi如果数据库中有新创建的表,并且没有插入数据,则去掉异常int a=1/0;再进行正常插入,如果此时id 从2开始,则 测试成功,因为id为1的user在之前一次测试中,抛异常被回滚。至此,三大框架整合完毕。


7.对三大框架整合进行完整测试。

①修改TestAction.java为如下代码

  1. @Controller
  2. @Scope("prototype")
  3. public class TestAction extends ActionSupport{
  4. @Resource
  5. private TestService testService;
  6. @Override
  7. public String execute() throws Exception{
  8. //System.out.println("test");
  9. testService.saveTwoUsers();
  10. return "success";
  11. }
  12. }

②Spring管理对象(事务),action处理请求,hibernate处理对象的存储。

③如果此时通过test.action能正常显示test.jsp并且在数据库中的user表中增加了2条记录,则整合成功。(可以再加入异常进行反复测,如果还出现回滚现象,则代表成功)

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
struts2 hibernate spring配置管理(一)——配置文件 - Strut...
Struts Spring Hibernate整合笔记_TerrySpace
图解SSH框架配置步骤
开发环境搭建集合【笔记】-2016.6.14
SSH框架之Spring+Struts2+Hibernate整合篇
Struts Spring Hibernate整合
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服