先给源码,建议大家对照着源码看下面的教程!
链接:SSH框架完美整合源码下载
另附一篇讲解SSH的博文
链接:SSH是怎么分工的
下面是教程
整合分为两个步骤
1.Spring集成Hibernate
2.Struts2集成Spring
注意
- 每整合一项都要进行针对性测试
- jar包导入不要导入两个版本不同功能重复的包
一.Spring集成Hibernate
导入Jar包
Hibernate相关Jar包
Spring相关Jar包
这里导入C3P0的jar包是因为要使用C3P0的JDBC连接池
Mysql
Beans.xml配置文件
配置aop和事务
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx=http://www.springframework.org/schema/tx http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
自动扫描方式加载对象
<context:component-scan base-package="cn.hzy"/>
配置c3p0数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="org.gjt.mm.mysql.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/> <property name="user" value="root"/> <property name="password" value="123456"/> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="1"/> <!--连接池中保留的最小连接数。--> <property name="minPoolSize" value="1"/> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="300"/> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="60"/> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="5"/> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60"/> </bean>
配置sessionFactory(单例)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>cn/hzy/domain/Employee.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false </value> </property> </bean>
配置事务
<!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 使用注解配置事务 --> <tx:annotation-driven transaction-manager="txManager"/>
JavaBean对象创建
创建Employee JavaBean类
这里故意提高难度,给对象设置一个枚举类型的属性,看看在Hibernate中怎么配置
枚举类型private Gender gender = Gender.MAN;
Employee.hbm.xml配置文件(映射枚举类型)
<property name="gender" not-null="true"> <type name="prg.hibernate.type.EnumType"> <param name="enumClass">cn.hzy.domain.Gender</param> <!-- 12代表varchar类型,即保存实际值,若不提供type参数,则保存枚举值的索引到数据库 --> <param name="type">12</param> </type> </property>
Service层编写
EmployeeService
public interface EmployeeService { public void save(Employee employee); public void update(Employee employee); public Employee find(String username); public void delete(String... usernames); public List<Employee> list(); }
EmployeeServiceImpl
//使用自动自动扫描方式加载对象 //事务 @Service @Transactional public class EmployeeServiceImpl implements EmployeeService{ //使用注解方式加载对象 @Resource SessionFactory factory; public void delete(String... usernames) { for(String username : usernames){ factory.getCurrentSession().delete(factory.getCurrentSession().load(Employee.class, username)); } } //取消事务 @Transactional(propagation=Propagation.NOT_SUPPORTED) public Employee find(String username) { return (Employee)factory.getCurrentSession().get(Employee.class, username); } @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.NOT_SUPPORTED) public List<Employee> list() { return factory.getCurrentSession().createQuery("from Employee").list(); } public void save(Employee employee) { factory.getCurrentSession().persist(employee); } public void update(Employee employee) { factory.getCurrentSession().merge(employee); } }
测试EmployeeServiceImplTest
看看一下代码能否获取employeeServiceImpl
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml"); employeeService = (EmployeeService)act.getBean("employeeServiceImpl");
二.Struts2集成Spring
导入Jar包
注意防止jar重复
另外
Web.xml实例化Spring容器
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 Spring容器放在application范围 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Web.xml配置Struts
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Struts.xml
<!-- 指定Action对象由谁创建 --> <constant name="struts.objectFactory" value="spring"></constant> 配置Action 不用写全名,因为已经交给Spring管理 <package name="employee" namespace="/employee" extends="struts-default"> <action name="list" class="employeeAction"> <result name="list">/WEB-INF/page/employee.jsp</result> </action> </package>
创建Action类EmployeeAction
@Service public class EmployeeAction { @Resource EmployeeService employeeService; public String execute(){ ActionContext.getContext().put("empList", employeeService.list()); return "list"; } }
测试
http://localhost:8080/MySSH/employee/list