ssh整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh整合相关的知识,希望对你有一定的参考价值。
ssh分别是:struts是表现层 hibernate是持久层 spring是容器工具,他们3个整合后的目标是:表现层和持久层所有对象实例交给ioc统一管理,降低层次间的依赖,包括持久层SessionFactory 以及事务管理 都给spring管理。
第一是在web应用中什么实际加载springioc容器以及如何加载?
这个时机选在Servlet加载的时候将ioc添加到ServletContent作用域中。
具体需要在web.xml加入ServletContext监听器
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> //默认spring的xml配置文件和Servlet-name相同 如果给xml取别名,则在此定义 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
第二是spring如何整合hibernate的SessionFactory以及事务管理的整合?
注入spring自带的LocalSessionFactory
具体的步骤应该在先配置数据源 再配置SessionFactory和事务
<?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"
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置 C3P0 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 配置 SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/atguigu/ssh/entities/*.hbm.xml"></property> </bean> <!-- 配置 Spring 的声明式事务 --> <!-- 1. 配置 hibernate 的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 2. 配置事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="lastNameIsValid" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.ssh.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>
第三是如何将strutsAction的实例交给spring管理?
引用strtus框架中的接入spring的插件【struts2-spring-plugin-vesion】
加入struts第一步向web.xml引入struts的独有StrutsPrepareAndExecuteFilter
<!-- 配置 Struts2 的 Filter --> <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>
第四是spring如何管理持久层和服务层的接口和实现?
有2各种方案,第一种使用spring的注解方式,配置使用扫描形式自动注入已注解的类的实例。
第二种是统一使用spring配置文件方式统一采用seter设值方式注入。
这里展示xml配置的方式,配置如下:
//设值注入dao层接口
<bean id="employeeDao" class="com.atguigu.ssh.dao.EmployeeDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="departmentDao" class="com.atguigu.ssh.dao.DepartmentDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> //注入服务 <bean id="employeeService" class="com.atguigu.ssh.service.EmployeeService"> <property name="employeeDao" ref="employeeDao"></property> </bean> <bean id="departmentService" class="com.atguigu.ssh.service.DepartmentService"> <property name="departmentDao" ref="departmentDao"></property> </bean> //注入action的服务 <bean id="employeeAction" class="com.atguigu.ssh.actions.EmployeeAction" scope="prototype"> //注意因为struts 每个action是新的请求 scope必须配成原型 不然默认是single <property name="employeeService" ref="employeeService"></property> <property name="departmentService" ref="departmentService"></property> </bean>
hibernate.cfg.xml---------------------------------------------------------------------------------------------
<session-factory>
<!-- 配置 hibernate 的基本属性 -->
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.mysql5InnoDBDialect</property>
<!-- 是否显示及格式化 SQL -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 生成数据表的策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 二级缓存相关 -->
</session-factory>
db.properties---------------------------------------------------------------------------------------------
jdbc.user=root
jdbc.password=
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.initPoolSize=2
jdbc.maxPoolSize=4
#...
struts.xml---------------------------------------------------------------------------------------------
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<!-- 定义新的拦截器栈, 配置 prepare 拦截器栈的 alwaysInvokePrepare 参数值为 false -->
<interceptors>
<interceptor-stack name="sshStack">
<interceptor-ref name="paramsPrepareParamsStack">
<param name="prepare.alwaysInvokePrepare">false</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 使用新的拦截器栈 -->
<default-interceptor-ref name="sshStack"></default-interceptor-ref>
<action name="emp-*" class="employeeAction"
method="{1}">
<result name="list">/WEB-INF/views/emp-list.jsp</result>
<result type="stream" name="ajax-success">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
<result name="input">/WEB-INF/views/emp-input.jsp</result>
<result name="success" type="redirect">/emp-list</result>
</action>
</package>
</struts>
Department.hbm.xml ---------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-7-22 11:21:48 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.atguigu.ssh.entities.Department" table="SSH_DEPARTMENT">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="departmentName" type="java.lang.String">
<column name="DEPARTMENT_NAME" />
</property>
</class>
</hibernate-mapping>
Employee.hbm.xml 多对一---------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-7-22 11:21:48 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.atguigu.ssh.entities.Employee" table="SSH_EMPLOYEE">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="lastName" type="java.lang.String">
<column name="LAST_NAME" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="birth" type="java.util.Date">
<column name="BIRTH" />
</property>
<property name="createTime" type="java.util.Date">
<column name="CREATE_TIME" />
</property>
<!-- 映射单向 n-1 的关联关系 -->
<many-to-one name="department" class="com.atguigu.ssh.entities.Department">
<column name="DEPARTMENT_ID" />
</many-to-one>
</class>
</hibernate-mapping>
以上是关于ssh整合的主要内容,如果未能解决你的问题,请参考以下文章
全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段
Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用