Spring+Hibernate+Struts2整合文档

来源:互联网 发布:苹果cms精仿模板 编辑:程序博客网 时间:2024/04/28 13:30
 

培训终于到了最后一个阶段了!

 

使用工具MyEclipse8.6

 

今天初探Hibernate老师提前说了SSH的整合,鸡动人心!

过程:

view plaincopy to clipboardprint?
  1. 版本:struts2.2.1 + spring3.0 + hibernate3.3  
  2. MyEclipse8.6引入JAR包  
  3.   
  4. 1.引入JAR包:  
  5. a)可以使用myeclipse自带的功能引入所需要的包:  
  6.   右键工程-->MyEclipse--> add Hibernate capabilities,add spring capabilities  
  7.   
  8. b)struts2的包可以从 下载的目录下复制有 七 个包  
  9.   例如:E:/CL/API/struts-2.2.1.1-all/struts-2.2.1.1/apps/struts2-blank/WEB-INF/lib  
  10.   
  11. c)还需要插件包 struts2-spring-plugin-2.2.1.1.jar  
  12.   例如:E:/CL/API/struts-2.2.1.1-all/struts-2.2.1.1/lib  
  13.   
  14.   
  15.   
  16. 2.配置文件:(主要是web.xml和applicationContext.xml的配置)  
  17.   
  18. web.xml:  
  19.   
  20.     <filter>  
  21.         <filter-name>struts2</filter-name>  
  22.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  23.     </filter>  
  24.   
  25.     <filter-mapping>  
  26.         <filter-name>struts2</filter-name>  
  27.         <url-pattern>/*</url-pattern>  
  28.     </filter-mapping>  
  29.   
  30.     <context-param>  
  31.         <param-name>contextConfigLocation</param-name>  
  32.         <param-value>classpath:applicationContext.xml</param-value>  
  33.     </context-param>  
  34.   
  35.     <listener>  
  36.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  37.     </listener>  
  38.   
  39.   
  40. applicationContext.xml:  
  41.   
  42. <beans xmlns="http://www.springframework.org/schema/beans"  
  43.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  44.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
  45.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  46.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  47.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  48.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  49.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  50.   
  51.     <!-- 支持元注释 -->  
  52.     <context:annotation-config />  
  53.   
  54.     <!-- 扫描包目录 -->  
  55.     <context:component-scan base-package="com"></context:component-scan>  
  56.   
  57.     <bean id="sessionFactory"  
  58.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  59.         <property name="configLocation" value="classpath:hibernate.cfg.xml">  
  60.         </property>  
  61.     </bean>  
  62.   
  63.     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  64.         <property name="sessionFactory">  
  65.             <ref bean="sessionFactory" />  
  66.         </property>  
  67.     </bean>  
  68. </beans>  
  69.   
  70.   
  71. hibernate.cfg.xml:  
  72.   
  73.     <!DOCTYPE hibernate-configuration PUBLIC  
  74.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  75.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  76.   
  77.     <hibernate-configuration>  
  78.   
  79.         <session-factory>  
  80.             <property name="dialect">  
  81.                 org.hibernate.dialect.Oracle9Dialect  
  82.             </property>  
  83.             <property name="connection.url">  
  84.                 jdbc:oracle:thin:@localhost:1521:oracle  
  85.             </property>  
  86.             <property name="connection.username">chenl</property>  
  87.             <property name="connection.password">chenl</property>  
  88.             <property name="connection.driver_class">  
  89.                 oracle.jdbc.driver.OracleDriver  
  90.             </property>  
  91.   
  92.             <!--   
  93.                 <mapping resource="com/po/TUser.hbm.xml" />  
  94.                 <mapping resource="com/po/TDetail.hbm.xml" />  
  95.             -->  
  96.         </session-factory>  
  97.   
  98.     </hibernate-configuration>  
  99.   
  100.   
  101. struts.xml:  
  102.     <!DOCTYPE struts PUBLIC  
  103.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  104.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  105.   
  106.     <struts>  
  107.         <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  108.         <constant name="struts.devMode" value="true" />  
  109.         <package name="user" extends="struts-default" namespace="/user">  
  110.             <action name="user" class="com.jungle.action.UserAction">  
  111.             </action>  
  112.         </package>  
  113.     </struts>  

 


图片步骤:

引入spring的包

 

引入hibernate的包:

一直下一步。。。

 

view plaincopy to clipboardprint?
  1. 结束.  
  2.   
  3. PS:  
  4.   
  5. 使用hibernateTemplate:  
  6.   
  7. Action中代码:  
  8.   
  9. @Component("userAction")  
  10. @Scope("prototype")  
  11. public class UserAction {  
  12.   
  13.  @Resource  
  14.  private HibernateTemplate hibernateTemplate;  
  15.   
  16.  public void execute() {  
  17.   TUser user = new TUser();  
  18.   user.setUsername("aaa");  
  19.   
  20.   TDetail tdetail = new TDetail();  
  21.   tdetail.setIdcard("11111111111");  
  22.   tdetail.setEmail("aa@qq.com");  
  23.   
  24.   tdetail.setTuser(user);  
  25.   user.setTdetail(tdetail);  
  26.   
  27.   hibernateTemplate.save(user);  
  28.   hibernateTemplate.flush();  
  29.  }  
  30. }  

 

 

注意: 由于是比较新的版本 所以 整合后需要用到asm3.3版本的jar包而不是asm.jar

下载地址 自己百度(asm3.3.jar)

http://apps.hi.baidu.com/share/detail/16816998

原创粉丝点击