SSH整合:我写的整合方式是xml配合注解的整合方式,按程序流程整合。
(一)导入jar包:导入SSH整合的基本jar包
(二)服务器启动:加载web.xml
在web.xml中配置Struts2的核心入口(核心过滤器)
配置 Spring创建ApplicationContext的监听器 ContextLoaderListener位于spring-web的jar包中
配置全局参数,监听器获取applicationContext.xml的路径,为了让ApplicationContext对象加载spring配置文件。
(三)配置applicationContext.xml:导入spring配置文件的约束 引入hibernate配置信息
1、导入约束
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
2、开启包扫描器和事务对注解的支持
3、配置数据源(连接池)我这里配置的c3p0连接池
4、配置hibernate配置信息:hibernate和spring的整合就是将sessionFactory的创建权交给spring 由spring管理sessionFactory 这里利用spring框架的hiernate模板中的一个LocalSessionFactoryBean类,对sessionFactory进行封装。(具体的一些封装细节和session封装细节,以后我会慢慢写出来)
一个要注意的问题:导包要一致,因为hibernate在spring中有3,4,5三个包
hibernate配置信息:
5、开启事务管理器
6、实例化hibernateTemplate(连接session,在dao层需要用它进行数据库操作)
(四) 将各层纳入spring管理,并持久化类
dao:
service:
action:
大致ssh框架就整合完毕,一些简单的流程分享给大家。