`
王者之剑
  • 浏览: 196459 次
  • 性别: Icon_minigender_1
  • 来自: 湖北
社区版块
存档分类
最新评论

Struts2+Spring2.5+Hibernate3.2实例教程(2-4)V0.1

    博客分类:
  • java
阅读更多

2)Spring2和Hibernate集成
   2.1)复制必要的jar
   将
D:\JavaTools\spring-framework-2.5.5\lib\j2ee\jta.jar
   D:\JavaTools\spring-framework-2.5.5\lib\c3p0\c3p0-0.9.1.2.jar
D:\JavaTools\hibernate-3.2\lib\antlr-2.7.6.jar
D:\JavaTools\hibernate-3.2\lib\asm.jar
D:\JavaTools\hibernate-3.2\lib\asm-attrs.jar
D:\JavaTools\hibernate-3.2\lib\cglib-2.1.3.jar
D:\JavaTools\hibernate-3.2\lib\commons-collections-2.1.1.jar
D:\JavaTools\hibernate-3.2\lib\dom4j-1.6.1.jar
D:\JavaTools\hibernate-3.2\lib\ehcache-1.2.3.jar
D:\JavaTools\hibernate-3.2\lib\jaxen-1.1-beta-7.jar
   复制到/WEB-INF/lib下
   2.2)修改配置文件
在/WEB-INF/applicationContext.xml中加入Hibernate相关配置。
  <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bitrac" />
        <property name="user" value="root" />
        <property name="password" value="" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>com/albertsong/bitrac/dao/Member.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="show_sql">true</prop>
            </props>
        </property>
    </bean>
加入数据访问类的配置
<bean id="memberDao" class="com.albertsong.bitrac.dao.MemberDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
给<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" />
添加属性<property name="memberDao" ref="memberDao" />
完成以后完整的applicationContext.xml如下:

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < beans   xmlns = " http://www.springframework.org/schema/beans "
  3.     xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "
  4.     xmlns:aop = " http://www.springframework.org/schema/aop "
  5.     xmlns:tx = " http://www.springframework.org/schema/tx "
  6.     xsi:schemaLocation = "
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  9.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " >
  10.  
  11.     < bean   id = " dataSource "  
  12.         class = " com.mchange.v2.c3p0.ComboPooledDataSource "   destroy-method = " close " >
  13.         < property   name = " driverClass " value = " com.mysql.jdbc.Driver " />  
  14.         < property   name = " jdbcUrl " value = " jdbc:mysql://localhost:3306/bitrac " />  
  15.         < property   name = " user " value = " root " />
  16.         < property   name = " password " value = "" />
  17.     </ bean >
  18.     < bean   id = " sessionFactory "
  19.         class = " org.springframework.orm.hibernate3.LocalSessionFactoryBean " >
  20.         < property   name = " dataSource " ref = " dataSource " />
  21.         < property   name = " mappingResources " >
  22.             < list >
  23.                 < value > com/albertsong/bitrac/dao/Member.hbm.xml </ value >
  24.             </ list >
  25.         </ property >
  26.         < property   name = " hibernateProperties " >
  27.             < props >
  28.                 < prop   key = " hibernate.dialect " >
  29.                     org.hibernate.dialect.MySQLDialect
  30.                 </ prop >
  31.                 < prop   key = " show_sql " > true </ prop >
  32.             </ props >
  33.         </ property >
  34.     </ bean >
  35.     < bean   id = " memberDao " class = " com.albertsong.bitrac.dao.MemberDaoImpl " >
  36.         < property   name = " sessionFactory " ref = " sessionFactory " />
  37.     </ bean >
  38.     < bean   name = " adminAction " class = " com.albertsong.bitrac.action.AdminAction " scope = " prototype " >
  39.         < property   name = " memberDao " ref = " memberDao " />
  40.     </ bean >
  41. </ beans >


(要注意的是如果没有scope="prototype",则登录出错以后,不能再正确登录,原因显然是AdminAction缓存了member的数据,具体原理暂不研究)
 新建Bitrac\src\com\albertsong\bitrac\dao\Member.hbm.xml,这是Hibernate的数据映射文件,内容如下

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < ! DOCTYPE   hibernate-mapping PUBLIC
  3.         " -//Hibernate/Hibernate Mapping DTD 3.0//EN "
  4.         " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
  5.  
  6. < hibernate-mapping   package = " com.albertsong.bitrac.dao " >
  7.  
  8.     < class   name = " Member " table = " BT_Member " >
  9.         < id   name = " id " column = " id " >
  10.             < generator   class = " native " />
  11.         </ id >
  12.         < property   name = " grade " />
  13.         < property   name = " username " />
  14.         < property   name = " password " />
  15.         < property   name = " validate " />
  16.         < property   name = " conserve " />
  17.         < property   name = " contact " />
  18.         < property   name = " picture " />
  19.         < property   name = " explain " />
  20.         < property   name = " numPost " />
  21.         < property   name = " numComm " />
  22.         < property   name = " numFile " />
  23.         < property   name = " publish " />
  24.         < property   name = " address " />
  25.  
  26.     </ class >
  27.  
  28. </ hibernate-mapping >


这 里使用了package,如果不使用package,则class的name要使用com.albertsong.bitrac.dao.Member这 样的。property的name和Member的属性对应,因为表BT_Member的列名和Member的属性一样,所以不需要使 用<column />标签。

 

分享到:
评论
2 楼 王者之剑 2009-03-19  
ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from input stream

说明hibernate配置文件有问题
这个教程带有完整的源代码,自己可以跟着教程一步步做,有问题请比较源代码。
1 楼 xingqiba 2009-03-18  
我学到了这儿有错了..是怎么回事啊....
2009-03-18 13:49:09,125 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1557c0: defining beans [loginAction,dataSource,sessionFactory,loginService,loginDao]; root of factory hierarchy
2009-03-18 13:49:09,125 ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:514)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:631)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 30 more
Caused by: org.dom4j.DocumentException: Error on line 1 of document  : The processing instruction must begin with the name of the target. Nested exception: The processing instruction must begin with the name of the target.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:505)
... 34 more
2009-3-18 13:49:09 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:514)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:631)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 30 more
Caused by: org.dom4j.DocumentException: Error on line 1 of document  : The processing instruction must begin with the name of the target. Nested exception: The processing instruction must begin with the name of the target.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:505)
... 34 more
2009-3-18 13:49:09 org.apache.catalina.core.ApplicationContext log
信息: Set web app root system property: 'project.root' = [D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\project\]
2009-3-18 13:49:09 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2009-3-18 13:49:09 org.apache.catalina.core.StandardContext start
严重: Context [/project] startup failed due to previous errors
2009-3-18 13:49:09 org.apache.catalina.core.ApplicationContext log
信息: Shutting down log4j
2009-3-18 13:49:09 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
2009-3-18 13:49:09 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2009-3-18 13:49:09 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2009-3-18 13:49:09 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/78  config=null
2009-3-18 13:49:09 org.apache.catalina.startup.Catalina start
信息: Server startup in 2875 ms

相关推荐

    struts1.2 + spring2.5 + hibernate3.2框架demo

    Struts1.2、Spring2.5和Hibernate3.2是经典的Java企业级开发框架组合,它们各自在应用程序的不同层次上发挥着重要作用。Struts1.2是一个MVC(Model-View-Controller)框架,主要负责处理用户界面与业务逻辑之间的...

    struts2+spring2.5+hibernate3.2整合完整项目,带数据库脚本

    总的来说,这个"struts2+spring2.5+hibernate3.2整合完整项目"提供了一个完整的开发实例,涵盖了前端到后端,以及数据库的各个环节。对于学习者而言,可以深入理解这三大框架的协同工作,提升Java Web开发技能。而...

    struts2+spring2.5+Hibernate3.2整合示例

    Struts2、Spring和Hibernate是Java Web开发中的三...以上就是关于“Struts2+Spring2.5+Hibernate3.2整合示例”的主要知识点,这个整合实例涵盖了Java Web开发中的重要技术,对于开发者来说,深入学习和实践将非常有益。

    struts2.1 + spring 2.5 + hibernate 3.2 增删改查

    总结来说,"struts2.1 + spring 2.5 + hibernate 3.2 增删改查"的例子涵盖了Web应用开发中的核心部分:前端请求处理(Struts2)、业务逻辑管理(Spring)和数据库操作(Hibernate)。这个例子为初学者提供了一个很好...

    cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip

    cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip cdst ssh代码...

    Struts2+Spring2.5+Hibernate3.2集成实例开发2

    Struts2+Spring2.5+Hibernate3.2集成实例开发2 Struts2+Spring2.5+Hibernate3.2集成实例开发2

    struts2+spring2.5+hibernate3.2

    Struts2、Spring2.5和Hibernate3.2是经典的Java Web开发框架组合,被称为SSH(Struts2、Spring、Hibernate)集成。这个整合包可能是为了帮助开发者快速搭建基于这些技术的项目环境。 Struts2是MVC(Model-View-...

    struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版

    struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版 struts2 的方式自己去看简单。 spring2.5 是用注释来注入 hibernate3.2 是用ejb3注解映射关系 hibernate3 +个属性可以自动...

    struts2+spring2.5+hibernate3.2大型项目

    非常好的学习资料 struts2+spring2.5+hibernate3.2大型项目spring mvc

    Struts2+Spring2.5+Hibernate3.2集成实例开发

    Struts2+Spring2.5+Hibernate3.2集成实例开发 附带我本人写的一个实例,如果默认数据库Oracle10g ,可以根据实际情况调整;文件较大,分为两个部分下载,同时下在完成后,执行Struts2+Spring+Hibernate.part1.rar...

    struts2.1.6+spring2.5=hibernate3.2整合实例

    struts2.1.6+spring2.5+hibernate3.2整合实例,绝对原创,绝对运行正常,jar文件的截图和sql都有 由于自己学习时耗了很多时间所以要分,请大家理解,有什么问题可以和我联系http://blog.163.com/lzy_eagle

    用户登录(Struts1.2+Spring2.5+Hibernate3.2)

    1、可以运行(发布后,删除\Tomcat 6.0\webapps\ssh\WebRoot\WEB-INF\lib下的asm-2.2.3.jar) 2、采用Struts1.2 + Spring2.5 + Hibernate3.2 整合开发 3、SSH入门级实例

    ecside+struts2+spring2.5+hibernate3.2

    标题 "ecside+struts2+spring2.5+hibernate3.2" 提及的是一个基于Java的Web开发框架组合,这个组合是企业级应用的常见选择,特别是对于那些需要强大持久层、业务层和表现层管理的项目。让我们逐一深入探讨这四个关键...

    struts2+spring2.5+hibernate3.2整合的简单例子

    这两天做的一个适合初学者的SSH框架整合的例子,相信对初学者会有一定帮助。

    struts1.2+spring2.5+hibernate3.2学习实例(Eclipse)

    由于上传限制,lib包不能上传,大家可以下载spring2.5版本的支持库(下最大的那个zip包),全部加进去就行了,还有mysql的连接包,项目很简单,直接导入eclipse,就可以使用,包内有mysql的sql文件,框架已经搭好,...

    struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 02部分

    struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 01部分 struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 02部分 大家只要将这两个部分的包全部下载下来,就可以搭建struts2 + spring + ...

    struts2+spring2.5+hibernate3.2 例子

    Struts2、Spring2.5和Hibernate3.2是经典的Java Web开发框架组合,它们各自在应用程序的不同层面提供了强大的功能。下面将详细解释这三个框架以及它们如何协同工作。 **Struts2** Struts2是一个基于MVC(Model-View...

    Struts1.2+Spring2.5+Hibernate3.2框架搭建(一)

    在IT行业中,构建高效、可维护的企业级应用是至关重要的,..."Struts+hibernate+Spring的整合方法.doc"则可能深入探讨整合过程中的一些技巧和注意事项。这些文档对于学习和理解这个经典框架组合的集成过程非常有价值。

    ecside+struts2+spring2.5+hibernate3.2部分源代码

    标题 "ecside+struts2+spring2.5+hibernate3.2部分源代码" 描述了一个基于四个核心技术的项目,它们分别是ECSide、Struts2、Spring 2.5 和 Hibernate 3.2。这些技术是Java开发中的重要组件,尤其在构建企业级应用时...

    struts2.1.6 +spring2.5 +hibernate3.2整合开发的 jar包 _ 01部分

    struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 01部分 struts2.1.6 +spring2.5 +hibernate3.2 整合开发的 jar包 _ 02部分 大家只要将这两个部分的包全部下载下来,就可以搭建struts2 + spring + ...

Global site tag (gtag.js) - Google Analytics