zoukankan      html  css  js  c++  java
  • SSH三大框架手动整合

    1、 三大框架整合jar包筛选 
    struts2 2.3.7 	
    	* jar包 从apps/struts2_blank.war 找出最基本的开发jar包 
    	* struts2-convention-plugin-2.3.7.jar 进行struts2注解开发 (如果不用注解开发,不要导入)
    	* struts2-json-plugin-2.3.7.jar  用于Ajax开发 ,自带json插件 
    	* struts2-spring-plugin-2.3.7.jar 用来进行 struts2和Spring 框架整合 (内置整合策略)
    	
    在web.xml 配置核心Filter (StrutsPrepareAndExecuteFilter)
    	<!-- 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>
    在src目录创建struts.xml 核心配置文件 
    		
    Spring 3.2 
    	* 最基本jar包 spring-core spring-beans spring-context spring-expression  
    	* 依赖日志 commons-logging  log4j   在src目录 创建 log4j.properties 
    	
    	AOP开发
    		spring-aop-3.2.0.RELEASE.jar
    		spring-aspects-3.2.0.RELEASE.jar
    		com.springsource.org.aopalliance-1.0.0.jar
    		com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    	整合Hibernate 
    		spring-jdbc-3.2.0.RELEASE.jar
    		spring-tx-3.2.0.RELEASE.jar
    		spring-orm-3.2.0.RELEASE.jar
    	web工程中配置Spring监听器 
    		spring-web-3.2.0.RELEASE.jar
    	测试集合junit 
    		spring-test-3.2.0.RELEASE.jar
    	
    在src目录或者WEB-INf 创建 applicationContext.xml 	
    	引入所有schema  : beans 、context、aop、 tx 
    	<beans xmlns="http://www.springframework.org/schema/beans"
    		   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		   xmlns:context="http://www.springframework.org/schema/context"
    		   xmlns:aop="http://www.springframework.org/schema/aop"
    		   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/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>
    
    * 在web.xml 配置Spring Listener , 加载Spring 框架 
      <!-- Spring 监听器 -->
      <listener>
      	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <context-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>classpath:applicationContext.xml</param-value>
      </context-param>
    
    在Servlet/Filter中获得Spring上下文对象
    	WebApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(servletContext);
     
    Hibernate 3.6 
    	* 最基本jar hibernate3.jar 、 lib
    equired*.jar 、 libjpahibernate-jpa-2.0-api-1.0.1.Final.jar 
    	* 整合log4j日志 slf4j-log4j12-1.7.2.jar  log4j.jar  
    	* 如果使用c3p0  liboptionalc3p0c3p0-0.9.1.jar
    	* 导入数据库jdbc驱动 mysql-connector-java-5.0.8-bin.jar
    	* 二级缓存 ehcache-1.5.0.jar commons-logging.jar backport-util-concurrent.jar
    在src创建hibernate.cfg.xml 
    	<!-- JDBC基本连接参数 -->
    	<session-factory> <!-- 理解为连接池 -->
    		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    		<property name="hibernate.connection.url">jdbc:mysql:///spring3day3</property>
    		<property name="hibernate.connection.username">root</property>
    		<property name="hibernate.connection.password">abc</property>
    		<!-- 配置方言 -->
    		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    		
    		<!-- 常见其它配置 -->
    		<property name="hibernate.show_sql">true</property> <!-- 控制台上打印SQL -->
    		<property name="hibernate.format_sql">true</property> <!-- 控制台输出时,对SQL语句格式化 -->
    		<!-- 测试环境 create/ create-drop 正式环境 update validate -->
    		<property name="hibernate.hbm2ddl.auto">update</property> <!-- 自动建表 -->
    		<!-- 事务自动提交 -->
    		<property name="hibernate.connection.autocommit">true</property>
    		<!-- 使用c3p0 -->
    		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    		<!-- 配置Session与线程绑定 -->
    		<property name="hibernate.current_session_context_class">thread</property>
    		
    		<!-- 在核心配置文件中 引用 mapping 映射文件 -->
    	</session-factory>
    编程使用 HibernateUtils 工具类 	
    	Configuration configuration = new Configuration().configure();
    	SessionFactory sessionFactory = configuration.buildSessionFactory();
    	Session session = sessionFactory.openSession() / getCurrentSession();
    	Transaction transaction = session.beginTransaction();
    	数据库操作
    	transaction.commit();
    ----------------------------------------------------------------------------------------------------------------------------------
    
    手工整合案例 ssh1 2 、 struts2 spring 内部都提供整合其他框架的插件, 通过插件 完成三大框架整合 struts2 内部提供 struts2-spring-plugin-2.3.7.jar 用来整合Spring * 导入整合jar包,struts 对象管理, 结合Spring spring 内部提供 spring-orm-3.2.0.RELEASE.jar 用来整合其他持久层框架 Hibernate 导入 struts2-spring-plugin-2.3.7.jar 在struts.xml 配置 <constant name="struts.objectFactory " value="spring"></constant> 方式一: 将struts.xml中 <action class=""> class属性值,定义伪类 ,指向applicationContext.xml <bean> 的 id <action name="addbook" class="addbook"> <result>/index.jsp</result> </action> <bean id="addbook" class="cn.itcast.action.AddBookAction" scope="prototype"> <property name="bookService" ref="bookService"></property> </bean> 常见错误:主要发生在有表单验证时 ,效果是第一次输出错误数据 无法通过,第二次输入正确数据 无法通过 原因分析 : struts2 管理Action ,默认多个实例 , 将Action配置到Spring的Bean ,默认scope是singleton , 在<bean>配置 scope="prototype " 方式二: 不需要在spring中配置 Action 实例管理 <action name="addbook" class="cn.itcast.action.AddBookAction"> <result>/index.jsp</result> </action> struts.objectFactory.spring.autoWire = name 即时你不将Action 配置到Spring ,Service也可以按照name 自动装配 Action代码 public void setBookService(BookService bookService) { this.bookService = bookService; } applicationContext.xml <bean id="bookService" class="cn.itcast.service.BookService"></bean> 常见错误 : 如果set方法名 和 Spring bean id 不一样,无法注入 3、 spring 整合 hibernate spring 提供 spring-orm-3.2.0.RELEASE.jar 用来整合 hibernate 方式一: 零障碍整合 目的: Hibernate 的SessionFactory 注入 ------ HibernateTemplate 模板工具类 通过HibernateTemplate完成常见操作 BookDAO extends HibernateDaoSupport ,完成注入 sessionFactory , 使用sessionFactory 创建 HibernateTemplate public void save(Book book){ this.getHibernateTemplate().save(book); } <!-- 配置DAO --> <bean id="bookDAO" class="cn.itcast.dao.BookDAO"> <!-- 注入 sessionFactory --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置sessionFactory 整合 Hibernate --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 加载hibernate.cfg.xml --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> 对Service 进行事务管理 <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 针对sessionFactory进行事务管理 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> @Transactional public class BookService { } 方式二 : 将Hibernate的配置信息 完全写入applicationContext.xml Spring的配置文件 中 * 不会使用 hiberate.cfg.xml hibernate 配置信息 分为三部分 : JDBC连接参数、 hibernate常用属性 、hbm文件映射 JDBC连接参数 ---- 通过配置DataSource 连接池 <!-- 引用properties 属性文件 内容 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> 在Spring配置文件中 配置Hibernate属性,必须完整属性名 映射hbm <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 加载连接池 --> <property name="dataSource" ref="dataSource"></property> <!-- hibernate常用属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> </props> </property> <!-- 映射hbm文件 --> <!-- <property name="mappingLocations"> --> <!-- <list> --> <!-- <value>classpath:cn/itcast/domain/Book.hbm.xml</value> --> <!-- </list> --> <!-- </property> --> <property name="mappingDirectoryLocations"> <list> <value>classpath:cn/itcast/domain</value> </list> </property> </bean>

    1、 jar包筛选
    2、 理解手动整合SSH框架 原理 (重要 ) ---- ssh1 (不需要动手实践)

    3、 struts2 插件 整合Spring 两种 (重点 ---- 动手多实践) ssh2
      spring 整合 hibernate 两种

  • 相关阅读:
    Thinkphp下实现D函数用于实例化Model格式
    Thinkphp3.2下导入所需的类库 同java的Import 本函数有缓存功能
    Thinkphp下记录和统计时间(微秒)和内存使用情况
    python打造seo必备工具-自动查询排名
    Python爬虫爬企查查数据
    解决Android8.0系统应用打开webView报错
    团队冲刺第七天个人博客
    团队冲刺第六天个人博客
    团队冲刺第五天个人博客
    团队冲刺第四天个人博客
  • 原文地址:https://www.cnblogs.com/vaer/p/4012827.html
Copyright © 2011-2022 走看看