系统程序架构:

    整合思路
        1.逆依赖方向而行,由Spring提供对象管理和服务
        2.依次实现Spring与Hibernate、Spring与Struts2的集成

配置web.xml文件:
    1.设置Spring配置文件
        Spring配置文件的存放位置,若文件放置在/WEB-INF/目录下且名称为applicationContext.xml,可以不配置此项
    范例:

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

2.指定何时启动Spring容器
        该监听器用于在web环境中启动Spring容器
    范例:

 <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

3.配置OpenSessionInVIew模式
        该过滤器把一个Hibernate Session和一次完整的请求过程相绑定解决了诸如延迟加载等问题。
    范例:

 <filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

注意:
    1. 该过滤器要配置在Struts 2核心控制器之前
    2. 若Spring中定义的SessionFactory Bean的id不是sessionFactory,需要为该过滤器配置sessionFactoryBeanName参数
        
在Spring中配置数据源和会话工厂:
    方法一:配置文件分离
        a.定义独立的Hibernate配置文件
        b.由Spring导入并创建会话工厂Bean
    方法二:在Spring配置文件中进行集中配置
        a.先配置数据源,再以此为基础配置会话工厂Bean
        b.持久化类的映射文件可以逐个配置,也可以按目录导入
        
使用HibernateDaoSupport基类:
    1.HibernateDaoSupport基类的setSessionFactory()方法
    范例:

  <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean> <bean id="userDao" class="com.xuetang9.demo.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

1.hibernate的配置文件自动生成时,没有userName 和 password?

范例:
1.hibernate.cfg.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property> <mapping resource="com/Elastic/SpringDemo3/ivy/entity/User.hbm.xml"/> </session-factory>
</hibernate-configuration>

2.struts.xml

 <?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> </struts>

3.web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringDemo3_ivy</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 配置Spring文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name><!-- 固定 -->
<!-- classPath:表示放在项目的根目录下 -->
<param-value>classPath:applicationContext.xml</param-value>
</context-param> <!-- 配置Spring的启动监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
</listener> <!-- 配置OpenSessionInView模式 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</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> <!-- 配置HttpSession过期时间(单位:分钟) -->
<session-config>
<session-timeout>20</session-timeout>
</session-config> </web-app>

4.applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 配置SessionFactory(方法1:独立hibernate配置文件) -->
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classPath:applicationContext.xml"></property>
</bean> --> <!-- 使用Spring集成Hibernate(方法2:不需要hibernate配置文件) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost/hibernatedb"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property> <!-- 连接池 -->
<property name="initialPoolSize" value="10"></property>
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="100"></property>
<property name="acquireIncrement" value="3"></property>
<!-- 请求获取连接的重试次数 -->
<property name="acquireRetryAttempts" value="3"></property>
<property name="acquireRetryDelay" value="5"></property>
<property name="idleConnectionTestPeriod" value="60"></property> </bean> <!-- 使用dataSource配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property> <!-- 通配 -->
<property name="mappingLocations" value="classPath:com/Elastic/SpringDemo3/ivy/entity/*.hbm.xml">
</property> <!-- 穷举 -->
<!-- <property name="mappingLocations">
<list>
<value>classPath:com/Elastic/SpringDemo3/ivy/entity/*.hbm.xml</value>
</list>
</property> --> </bean>
</beans>

SSH(一)的更多相关文章

  1. [linux]阿里云主机的免登陆安全SSH配置与思考

    公司服务器使用的第三方云端服务,即阿里云,而本地需要经常去登录到服务器做相应的配置工作,鉴于此,每次登录都要使用密码是比较烦躁的,本着极速思想,我们需要配置我们的免登陆. 一 理论概述 SSH介绍 S ...

  2. SSH实战 · 唯唯乐购项目(上)

    前台需求分析 一:用户模块 注册 前台JS校验 使用AJAX完成对用户名(邮箱)的异步校验 后台Struts2校验 验证码 发送激活邮件 将用户信息存入到数据库 激活 点击激活邮件中的链接完成激活 根 ...

  3. 记录一则Linux SSH的互信配置过程

    需求:四台Linux主机,IP地址为192.168.10.10/11/12/13,配置登录用户的互信 1.各节点ssh-keygen生成RSA密钥和公钥 ssh-keygen -q -t rsa -N ...

  4. SSH免手动输入密码和设置代理

    通过使用sshpass将密码写入命令里,直接执行,免去手动密码输入的步骤命令如下: sshpass -p password_abc ssh user_abc@ssh_host -p ssh_port ...

  5. github免输用户名/密码SSH登录的配置

    从github上获取的,自己整理了下,以备后用. Generating an SSH key mac windows SSH keys are a way to identify trusted co ...

  6. Linux 利用Google Authenticator实现ssh登录双因素认证

    1.介绍 双因素认证:双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统.双因素认证是一种采用时间同步技术的系统,采用了基于时间.事件和密钥三变量而产生的一 ...

  7. mac下生成ssh keys 并上传github仓储

    使用github仓储需要本机生成一个公钥key 添加到自己的git账户SSH keys中   mac 生成方法:   1. 打开终端 输入   ssh-keygen 然后系统提示输入文件保存位置等信息 ...

  8. Linux实战教学笔记05:远程SSH连接服务与基本排错(新手扫盲篇)

    第五节 远程SSH连接服务与基本排错 标签(空格分隔):Linux实战教学笔记-陈思齐 第1章 远程连接LInux系统管理 1.1 为什么要远程连接Linux系统 在实际的工作场景中,虚拟机界面或物理 ...

  9. 树莓派3B的食用方法-1(装系统 网线ssh连接)

    首先要有一个树莓派3B , 在某宝买就行, 这东西基本上找到假货都难,另外国产和英国也没什么差别,差不多哪个便宜买哪个就行. 不要买店家的套餐,一个是配的东西有些不需要,有的质量也不好. 提示:除了G ...

  10. linux启动SSH及开机自动启动

    本文地址 分享提纲: 1.查看是否启动 2. 设置自动启动 1.[查看是否启动] 启动SSH服务 “/etc/init.d/sshd start”.然后用netstat -antulp | grep ...

随机推荐

  1. 003eop常见问题设置

  2. 通用高效的数据修复方法:Row level repair

    导读:随着大数据的进一步发展,NoSQL 数据库系统迅速发展并得到了广泛的应用.其中,Apache Cassandra 是最广泛使用的数据库之一.对于 Cassandra 的优化是大家研究的热点,而 ...

  3. 菜鸟系列Fabric源码学习 — MVCC验证

    Fabric 1.4 源码分析 MVCC验证 读本节文档之前建议先查看[Fabric 1.4 源码分析 committer记账节点]章节. 1. MVCC简介 Multi-Version Concur ...

  4. vux中x-input在安卓手机输入框的删除按钮(@on-click-clear-icon)点击没反应

    首先看你自己的的版本好,如果在2.6.9以上,我是在git上找到的解决办法,记录一下,希望可以帮到有需要的小伙伴. 在项目中找 node_modules > vux > x-input & ...

  5. 「学习笔记」ST表

    问题引入 先让我们看一个简单的问题,有N个元素,Q次操作,每次操作需要求出一段区间内的最大/小值. 这就是著名的RMQ问题. RMQ问题的解法有很多,如线段树.单调队列(某些情况下).ST表等.这里主 ...

  6. ad域-iis

    环境准备: 1. win server 服务器安装完成 2.配置主机名 3.配置静态ip 安装ad域和iis 重启服务器 密码记住!!! 点击安装 把服务器的NDS设置成本机ip 重启完成 注意:ad ...

  7. docker概述和基本命令

    命名空间 Docker使用一种称为namespaces提供隔离工作空间的技术来称为容器.当您运行容器时,Docker会为该容器创建一组 名称空间. 这些命名空间提供了一层隔离.容器的每个方面都在一个单 ...

  8. bootstrap:图片轮播

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  9. GDAl C++ 创建Shp

    用于GDAL,C++开发环境测试. #include <iostream> #include "gdal_priv.h" #include "ogrsf_fr ...

  10. 原生javascript实现二级延时菜单

    一.实现原理: 使用定时器和排他思想完成 二.代码: <!DOCTYPE html> <html> <head> <title></title&g ...