下面为大家带来一个SSH整合的小列子。
目录结构:
一:加入spring框架
1.添加spring jar文件
a)spring 依赖的类库
spring-aop-4.1.1.RELEASE.jar
spring-aspects-4.1.1.RELEASE.jar
spring-beans-4.1.1.RELEASE.jar
spring-context-4.1.1.RELEASE.jar
spring-context-support-4.1.1.RELEASE.jar
spring-core-4.1.1.RELEASE.jar
spring-expression-4.1.1.RELEASE.jar
spring-jdbc-4.1.1.RELEASE.jar
spring-orm-4.1.1.RELEASE.jar
spring-test-4.1.1.RELEASE.jar
spring-tx-4.1.1.RELEASE.jar
spring-web-4.1.1.RELEASE.jar
b)spring需要的支持库
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
2.创建spring配置文件,添加自动扫描
<context:component-scan base-package="com.neu"></context:component-scan>
在beans节点中添加属性如下:
default-autowire="byName"
(用于自动按照对象名装载数据,为了能够自动装载sessionFactory)
二:加入hibernate框架
1.添加hibernatejar文件
a)required
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.6.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
b)optional/c3p0
c3p0-0.9.2.1.jar
hibernate-c3p0-4.3.6.Final.jar
mchange-commons-java-0.2.3.4.jar
c)数据库驱动
ojdbc14.jar
2.不创建hibernate.cfg.xml(hibernate配置信息统一,在spring 配置文件中配置)
3.创建实体类(采用注解的方式)
我这里实体类有2个:StuInfo和TeaInfo
StuInfo实体类:
package com.neu.entity;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="STU_INFO")
public class StuInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="STU_ID")
private Integer stuId;
@Column(name="STU_NAME")
private String stuName;
@Column(name="STU_PWD")
private String stuPwd;
@ManyToOne(targetEntity=TeaInfo.class,cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
@JoinColumn(name="STU_TEA_ID") //STU_INFO表的外鍵
private TeaInfo tea;
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuPwd() {
return stuPwd;
}
public void setStuPwd(String stuPwd) {
this.stuPwd = stuPwd;
}
public TeaInfo getTea() {
return tea;
}
public void setTea(TeaInfo tea) {
this.tea = tea;
}
}
TeaInfo实体类:
package com.neu.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="TEA_INFO")
public class TeaInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="TEA_ID")
private Integer teaId;
@Column(name="TEA_NAME")
private String teaName;
@Column(name="TEA_AGE")
private Integer teaAge;
@OneToMany(targetEntity=StuInfo.class,mappedBy="tea")
private Set<StuInfo> stus = new HashSet<StuInfo>();
public Integer getTeaId() {
return teaId;
}
public void setTeaId(Integer teaId) {
this.teaId = teaId;
}
public String getTeaName() {
return teaName;
}
public void setTeaName(String teaName) {
this.teaName = teaName;
}
public Integer getTeaAge() {
return teaAge;
}
public void setTeaAge(Integer teaAge) {
this.teaAge = teaAge;
}
public Set<StuInfo> getStus() {
return stus;
}
public void setStus(Set<StuInfo> stus) {
this.stus = stus;
}
}
三:在spring配置文件中配置数据库相关信息
1.在src下创建配置文件,db.properties,其中编写数据连接信息
jdbc.url= jdbc:mysql://127.0.0.1:3306/mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
jdbc.maxPoolSize=5
jdbc.minPoolSize=2
jdbc.initialPoolSize=2
2.在spring中配置
<!-- 读取src下数据配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
①.dataSource
<!-- 配置数据源 -->
<bean name="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
</bean>
②.sessionFactory
<bean name="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate需要的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 设置实体类所在的包 -->
<property name="packagesToScan"
value="com.neu.entity"></property>
<!-- <property name="mappingLocations"
value="classpath:com/neu/entity/*.hbm.xml"></property> -->
</bean>
四:测试代码(创建业务逻辑和数据访问层)
1.dao层
a)创建dao的接口
b)创建实现类,要继承HibernateDaoSupport
@Repository("stuInfoDao") 表示持久化层
2.biz层
a)创建service接口
b)创建实现类
要创建dao接口的对象,提供setter方法
@Service("stuInfoService")
StuInfoDao接口:
package com.neu.dao;
import com.neu.entity.StuInfo;
public interface StuInfoDao {
public void save(StuInfo stu);
}
StuInfoDaoImpl实现类:
package com.neu.dao.impl;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.neu.dao.StuInfoDao;
import com.neu.entity.StuInfo;
@Repository("stuInfoDao")
public class StuInfoDaoImpl extends HibernateDaoSupport implements StuInfoDao {
@Override
public void save(StuInfo stu) {
super.getHibernateTemplate().save(stu);
}
}
StuInfoService接口:
package com.neu.biz;
import com.neu.entity.StuInfo;
public interface StuInfoService {
public boolean doSave(StuInfo stu);
}
StuInfoServiceImpl实现类:
package com.neu.biz.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.neu.biz.StuInfoService;
import com.neu.dao.StuInfoDao;
import com.neu.entity.StuInfo;
@Service("stuInfoService")
public class StuInfoServiceImpl implements StuInfoService {
@Resource(name="stuInfoDao")
private StuInfoDao stuInfoDao;
public void setStuInfoDao(StuInfoDao stuInfoDao) {
this.stuInfoDao = stuInfoDao;
}
@Override
@Transactional
public boolean doSave(StuInfo stu) {
boolean flag = false;
try {
this.stuInfoDao.save(stu);
flag = true;
} catch (Exception e) {
throw new RuntimeException(e);
}
return flag;
}
}
五:配置声明式的事务:
1.配置事务的管理者
<!-- 配置事务的管理者 -->
<bean name="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
2.配置事务管理的类型(创建事务管理的通知)
<!-- 配置事务通知 -->
<tx:advice id="txAdvice"
transaction-manager="txManager">
<tx:attributes>
<tx:method name="do*"/>
<tx:method name="*" propagation="SUPPORTS"
read-only="true"/>
</tx:attributes>
</tx:advice>
3.通过aop把我们的事务与类管理(织入)
<!-- 织入 -->
<aop:config>
<aop:pointcut expression="execution(* com.neu.biz.impl.*.*(..))"
id="txCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txCut"/>
</aop:config>
可以通过注解完成事务的配置,必须添加以下内容(2,3两步可以省略)
<tx:annotation-driven transaction-manager="txManager"/>
在需要使用事务的方法上添加 注解
@Transactional
六:添加Struts2框架
1.添加jar
a)struts核心包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.jar
xwork-core-2.3.4.jar
b)struts-sprint-plugin.jar
struts2-spring-plugin-2.3.4.jar
c)支持注解的jar
struts2-convention-plugin-2.3.4.jar
d)支持junit的jar
struts2-junit-plugin-2.3.4.jar
2.创建struts配置文件,web.xml
3.在web.xml中配置
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>classpath:com.neu.action.impl</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.创建Action
@Controller
@Scope("prototype") //必须
@ParentPackage("struts-default")
@Namespace("/")
给每一个方法添加如下注解
@Action(value="reg",results={
@Result(name="success",location="/success.jsp")
})
BaseAction:要继承ActionSupport,实现SessionAware接口
package com.neu.action;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 1L;
protected Map<String, Object> session;
@Override
public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}
}
StuInfoAction:其要继承上面的BaseAction
package com.neu.action.impl;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.neu.action.BaseAction;
import com.neu.biz.StuInfoService;
import com.neu.entity.StuInfo;
@Controller
@ParentPackage("struts-default")
@Scope("prototype")
@Namespace("/")
@Results({
@Result(name="error",location="/error.jsp")
})
public class StuInfoAction extends BaseAction {
private static final long serialVersionUID = 1L;
private StuInfo stu;
@Resource(name="stuInfoService")
private StuInfoService stuInfoService;
public StuInfo getStu() {
return stu;
}
//通过setter方法注入,所以页面上要写成stu.stuName等。
public void setStu(StuInfo stuInfo) {
this.stu = stuInfo;
}
public void setStuInfoService(StuInfoService stuInfoService) {
this.stuInfoService = stuInfoService;
}
@Action(value="reg",results={
@Result(name="success",location="/success.jsp")
})
public String reg()throws Exception{
boolean flag = stuInfoService.doSave(stu);
if(flag){
session.put("stu", stu);
return SUCCESS;
}
return ERROR;
}
}
测试1:TestConf 第一个@Test是测试spring框架和hibernate框架整合是否成功,配置文件是否正确。第二个@Test是测试能否添加数据。
package conf;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.neu.biz.StuInfoService;
import com.neu.entity.StuInfo;
import com.neu.entity.TeaInfo;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:beans.xml"})
public class TestConfig extends AbstractJUnit4SpringContextTests {
@Resource(name="stuInfoService")
private StuInfoService stuInfoService;
@Test
public void testConfig(){
System.out.println("YYYYYYYY");
//这个是测配置的
}
//这个是添加方法
@Test
public void testDoSave(){
TeaInfo tea = new TeaInfo();
tea.setTeaName("teaaaaa1");
tea.setTeaAge(35);
StuInfo stu = new StuInfo();
stu.setStuName("stu1");
stu.setStuPwd("123445");
stu.setTea(tea);
stuInfoService.doSave(stu);
System.out.println("----------");
}
}
测试2:StuInfoActionTest 最终测试,注意测试1和测试2不一样。
package com.neu.action.impl;
import org.apache.struts2.StrutsSpringJUnit4TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.opensymphony.xwork2.ActionProxy;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:beans.xml"})
public class StuInfoActionTest extends StrutsSpringJUnit4TestCase<StuInfoAction> {
@Test(expected=java.lang.NullPointerException.class)
public void testReg()throws Exception{
request.setParameter("stu.stuName", "ddd");
request.setParameter("stu.tea.teaName", "aaa");
ActionProxy p =super.getActionProxy("/reg.action");
Assert.assertNotNull(p);
String result = p.execute();
}
}
测试结果:
数据库中:
数据确实加进数据库了。
其余的方法都差不多。
beans.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"
default-autowire="byName">
<!-- 配置自动扫描 扫描com.neu包下的所有的类,为注解做准备-->
<context:component-scan base-package="com.neu"></context:component-scan>
<!-- 读取db.properties的内容 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dataSource -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
</bean>
<!-- 配置sessionFactory -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate需要的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 设置实体类所在的包 -->
<property name="packagesToScan" value="com.neu.entity"></property>
</bean>
<!-- 设置声明式的事务 -->
<bean name="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>