整合SSH测试项目
Posted Michael2397
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整合SSH测试项目相关的知识,希望对你有一定的参考价值。
整合struts 和 spring
预期:如果可以在action中能够正确调用service里面的方法执行并返回到一个页面中;那么我们认定struts和spring的整合是成功的。
编写JUnit测试类,测试spring加载是否正确:
1 public class TestMerge { 2 ClassPathXmlApplicationContext ctx; 3 @Before 4 public void loadCtx() { 5 ctx= new ClassPathXmlApplicationContext("applicationContext.xml"); 6 7 } 8 @Test 9 public void testSpring() { 10 TestService ts = (TestService)ctx.getBean("testService"); 11 ts.say(); 12 }
编写 TestService 接口 和实现类 TestServiceImpl
1 package cn.itcast.test.service; 2 3 public interface TestService { 4 public void say(); 5 6 }
1 package cn.itcast.test.service.impl; 2 3 import org.springframework.stereotype.Service; 4 5 import cn.itcast.test.service.TestService; 6 7 @Service("testService") 8 public class TestServiceImpl implements TestService { 9 10 @Override 11 public void say() { 12 System.out.println("service say haha "); 13 } 14 15 }
在applicationContext.xml中添加bean扫描配置信息;这边使用导入配置文件的方式配置。
①首先在cn.itcast.test.conf中建立test-spring.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:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 10 11 12 <!-- 扫描service --> 13 <context:component-scan base-package="cn.itcast.test.service.impl"></context:component-scan> 14 </beans>
②将test-spring.xml导入到applicationContext.xml中如下:
1 <!-- 引入外部spring配置文件 --> 2 <!-- <import resource="classpath:cn/itcast/test/conf/test-spring.xml"/>使用通配符 --> 3 <import resource="classpath:cn/itcast/*/conf/*-spring.xml"/>
编写TestAction类
1 public class TestAction extends ActionSupport { 2 @Resource 3 TestService testService; 4 5 public String execute(){ 6 testService.say(); 7 return SUCCESS; 8 } 9 }
在test的conf文件夹下新建test-struts.xml中配置TestAction :
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 6 <struts> 7 <package name="test-action" namespace="/" extends="struts-default"> 8 <action name="test_*" class="cn.itcast.test.action.TestAction" method="{1}"> 9 <result name="success">/WEB-INF/jsp/test/test.jsp</result> 10 </action> 11 </package> 12 13 </struts>
将test-struts.xml导入到struts.xml文件中。
1 <include file="cn/itcast/test/conf/test-struts.xml"></include>
在webRoot目录下新建test/test.jsp
在浏览器中输入:http://localhost:8080/itcastTax/test.action 查看后台是否能输入service中的打印信息。
整合hibernate 和 spring
在applicationContext.xml中配置如下原本在hibernate.cfg.xml中需要配置的信息,在spring中配置后hibernate.cfg.xml 可删除。
1、 配置c3p0数据库连接源:
1 <!-- 导入外部的properties配置文件 --> 2 <context:property-placeholder location="classpath:db.properties" /> 3 4 <!-- 配置c3p0数据源 --> 5 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 6 <property name="jdbcUrl" value="${jdbcUrl}"></property> 7 <property name="driverClass" value="${driverClass}"></property> 8 <property name="user" value="${user}"></property> 9 <property name="password" value="${password}"></property> 10 <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> 11 <property name="initialPoolSize" value="${initialPoolSize}"></property> 12 <!--连接池中保留的最小连接数。Default: 3 --> 13 <property name="minPoolSize" value="3"></property> 14 <!--连接池中保留的最大连接数。Default: 15 --> 15 <property name="maxPoolSize" value="${maxPoolSize}"></property> 16 <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> 17 <property name="acquireIncrement" value="3"></property> 18 <!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 --> 19 <property name="maxIdleTime" value="1800"></property> 20 </bean>
2、db.properties
1 jdbcUrl=jdbc:mysql://localhost:3306/itcastTax_0406?useUnicode=true&characterEncoding=utf8 2 driverClass=com.mysql.jdbc.Driver 3 user=root 4 password=123456 5 initialPoolSize=10 6 maxPoolSize=30
3、 配置sessionFactory,并将dataSource指向c3p0创建的dataSource:
1 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 2 <property name="dataSource" ref="dataSource"></property> 3 <property name="hibernateProperties"> 4 <props> 5 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 6 <prop key="hibernate.show_sql">true</prop> 7 <prop key="hibernate.hbm2ddl.auto">update</prop> 8 <prop key="javax.persistence.validation.mode">none</prop> 9 </props> 10 </property> 11 <property name="mappingLocations"> 12 <list> 13 <value>classpath:cn/itcast/nsfw/*/entity/*.hbm.xml</value> 14 <value>classpath:cn/itcast/test/entity/*.hbm.xml</value> 15 </list> 16 </property> 17 </bean>
编写实体类Person和对应的映射文件Person.hbm.xml:
1 public class Person implements Serializable { 2 private String id; 3 private String name; 4 5 public Person() { 6 7 } 8 9 public Person(String name) { 10 super(); 11 this.name = name; 12 } 13 14 public Person(String id, String name) { 15 this.id = id; 16 this.name = name; 17 } 18 public String getId() { 19 return id; 20 } 21 public void setId(String id) { 22 this.id = id; 23 } 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 31 }
映射文件的头部信息:
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 5 <hibernate-mapping> 6 <class name="cn.itcast.test.entity.Person" table="person"> 7 <id name="id" type="java.lang.String"> 8 <column name="id" length="32" /> 9 <generator class="uuid.hex" /> 10 </id> 11 <property name="name" type="java.lang.String"> 12 <column name="name" length="20" not-null="true" /> 13 </property> 14 </class> 15 </hibernate-mapping>
编写完实体映射文件后,用JUnit测试hibernate和spring的整合,在测试用例中启动spring容器的时候将扫描Person类根据其创建数据库表,并在测试时将向表插入一条数据。
测试hibernate,添加一个人员
1 @Test 2 public void testHibernate() { 3 SessionFactory sf = (SessionFactory) ctx.getBean("sessionFactory"); 4 5 Session session = sf.openSession(); 6 Transaction transaction = session.beginTransaction(); 7 8 session.save(new Person("人员1")); 9 transaction.commit(); 10 session.close(); 11 }
以上是关于整合SSH测试项目的主要内容,如果未能解决你的问题,请参考以下文章
免费下载全套最新013Spring Struts hibernate整合项目视频教程+教学资料+学习课件+源代码+软件开发工具