ssh 整合 (spring + Struts2 + hibernate)
2016-01-17 10:23
746 查看
1.配置spring 初始化监听器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 修改request and response 字符 --> <filter> <filter-name>CharsetFilter</filter-name> <filter-class>com.book.manager.filter.CharsetFilter</filter-class> </filter> <filter-mapping> <filter-name>CharsetFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 验证登录过滤器 --> <filter> <filter-name>Verification</filter-name> <filter-class>com.book.manager.filter.Verification</filter-class> </filter> <filter-mapping> <filter-name>Verification</filter-name> <url-pattern>/interceptor-jsp/*</url-pattern> <!-- 过滤服务器url --> <dispatcher>INCLUDE</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> <!-- struts2 过滤器 --> <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> <!-- 配置全局参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-ssh.xml</param-value> </context-param> <!-- 配置spring 初始化监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2.配置jdbc.properties
user=bookmanager password=test driver=oracle.jdbc.driver.OracleDriver url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
3.配置spring-ssh.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" 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:instance = "http://www.w3.org/2001/XMLSchema-instance" instance:schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.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.2.xsd http://www.springframework.org/schema/tx 103a7 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd " > <!-- 配置组件扫描器 --> <context:component-scan base-package="com.book.manager.*"> </context:component-scan> <!-- 配置外部资源引用文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 --> <bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" > <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> <property name="driverClassName" value="${driver}"></property> </bean> <!-- 配置hibernate sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 注入关系映射文件 --> <property name="mappingResources"> <list> <value>com/book/manager/entity/bookEntityOrm.xml</value> <value>com/book/manager/entity/userEntityOrm.xml</value> </list> </property> <!-- 注入hibernate 的属性配置 --> <property name="hibernateProperties"> <props> <!-- console 显示 sql 语句 --> <prop key="show_sql">true</prop> <!-- 格式化sql 语句,可读性更直观 --> <prop key="format_sql">true</prop> <!-- 设置数据库要使用的方言 --> <prop key="dialect"> org.hibernate.dialect.Oracle10gDialect </prop> </props> </property> </bean> <!-- 配置hibernate 模板 --> <bean id = "hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate" > <!-- 注入sessionFactory --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置hibernate 事务管理器,让spring来管理事务 --> <bean id = "transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" > <property name="sessionFactory" ref="sessionFactory"></property> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 定义事务通知 --> <tx:advice id="txAdivse" transaction-manager="transactionManager" > <tx:attributes > <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" timeout="2" rollback-for="java.lang.Exception" /> <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" timeout="2" rollback-for="java.lang.Exception"/> <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" timeout="2" rollback-for="java.lang.Exception"/> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- aop 切面 --> <aop:config> <aop:pointcut expression ="execution(* com.book.manager.service.*.*(..))" id="pointCut"/> <aop:advisor advice-ref = "txAdivse" pointcut-ref="pointCut"/> </aop:config> </beans>
4.把管理权给予spring
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action,do"/> <!-- 让 sping 来管理控制 --> <constant name="struts.objectFactory" value="spring"></constant> </struts>
5.所有DaoImpl继承 hibernateDaoSupper
package com.book.manager.dao; import javax.annotation.Resource; import org.apache.log4j.Category; import org.apache.log4j.Logger; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate4.HibernateTemplate; import org.springframework.orm.hibernate4.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; // 所以 dao 层的实现类必须继承 hibernateDaoSupport //这里继承SuperDao 就 可以通过getSeeion 来获取session public class SuperDao extends HibernateDaoSupport { @Resource(name="hibernateTemplate") public void setChildHibernateTemplate(HibernateTemplate template){ logger.debug("----> " + template); //设置父类的hibernateTemplate super.setHibernateTemplate(template); } @Resource(name="sessionFactory") public void setChildSessionFactory(SessionFactory sessionFactory){ //设置父类的sessionFactory super.setSessionFactory(sessionFactory); } public Session getSession(){ logger.debug("----> "+this.getClass().getName()); logger.debug("----> 1"+getHibernateTemplate()); logger.debug("----> 2"+getHibernateTemplate().getSessionFactory()); logger.debug("----> 3"+getHibernateTemplate().getSessionFactory().getCurrentSession()); return getHibernateTemplate().getSessionFactory().getCurrentSession(); //return getSessionFactory().getCurrentSession(); } }
相关文章推荐
- Linux Generating SSH Keys
- perl脚本实现限制ssh最大登录次数(支持白名单)
- rsync ssh 数据同步分析
- 搭建SSH时的思考和遇到的几个问题的解决方法
- SSH 使用原理 与解释
- SSH整合中 hibernate托管给Spring得到SessionFactory
- Linux VPS利用SSH重置ROOT密码的方法
- ssh,scp自动登陆的实现方法
- linux下使用ssh远程执行命令批量导出数据库到本地
- shell脚本实现ssh自动登录功能分享
- linux下ssh安装与scp命令使用详解
- ssh项目环境搭建步骤(web项目)
- 脚本实现SSH登录邮件报警
- Linux SSH 安全策略 限制 IP 登录方法
- 解决SSH连接超时的2个配置方法
- 用shell脚本防ssh和vsftpd暴力破解的详解讲解
- 内网ssh/mysql登录缓慢的解决方法
- Java实现SSH模式加密
- Linux sshd_config配置手册中文版