wy19280469 2009-06-30 12:39
浏览 321
已采纳

关于struts2 spring ibatis 结合问题

这2天 在学习IBATIS
准备写一个 struts hibernate ibatis 的例子出来

但是一直就没成功 我说一下我的配置步骤,请大家帮我指点一下我哪里错了

1.首先我在WEB.XML文件里 加入 STRUTS2的主控制器

<!-- struts2 主控制器 -->


struts2
org.apache.struts2.dispatcher.FilterDispatcher


struts2
/*

2.然后添加SPRING支持

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext*.xml
    </param-value>
</context-param>
<!-- 整合Spring框架 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.在classpath 下创建 applicationContext.xml 文件

文件内容如下

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://localhost:3306/test</value>
    </property>
    <property name="username">
        <value>root</value>
    </property>
    <property name="password">
        <value>root</value>
    </property>
</bean>


<bean id="sqlMapClient"
    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation">
        <value>classpath:SqlMapConfig.xml</value>
    </property>
    <property name="dataSource">
        <ref local="dataSource" />
    </property>
</bean>




<!-- 配置事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="find*" propagation="REQUIRED"
            read-only="true" />
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>
<!-- 配置那些类的方法进行事务管理 -->
<aop:config>
    <aop:pointcut id="allManagerMethod"
        expression="execution(* org.fire.inoutstock.service.*.*(..))" />
    <aop:advisor advice-ref="txAdvice"
        pointcut-ref="allManagerMethod" />
</aop:config>

然后在classpath下创建 sqlmapclient.xml文件

文件内容

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">



我就这样搭建的, 然后跑的时候出错 错误信息如下

2009-6-30 12:36:33 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 'sqlMapClient' defined in file [D:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\test2\WEB-INF\classes\applicationContext-common.xml]: Invocation of init method failed; nested exception is com.ibatis.common.exception.NestedRuntimeException: Error occurred. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)
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:220)
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:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
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:3843)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:924)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:887)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
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:578)
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: com.ibatis.common.exception.NestedRuntimeException: Error occurred. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:81)
at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:62)
at org.springframework.orm.ibatis.SqlMapClientFactoryBean.buildSqlMapClient(SqlMapClientFactoryBean.java:329)
at org.springframework.orm.ibatis.SqlMapClientFactoryBean.afterPropertiesSet(SqlMapClientFactoryBean.java:280)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1333)
... 39 more
Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:53)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:78)
... 44 more
Caused by: com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:114)
at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:75)
at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:93)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:63)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:51)
... 45 more
Caused by: java.io.IOException: Could not find resource sqlMap.T_User.xml
at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:101)
at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:86)
at com.ibatis.common.resources.Resources.getResourceAsReader(Resources.java:150)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser$11.process(SqlMapConfigParser.java:336)
at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:112)
... 49 more
2009-6-30 12:36:33 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2009-6-30 12:36:33 org.apache.catalina.core.StandardContext start
严重: Context [/test2] startup failed due to previous errors
2009-6-30 12:36:33 org.apache.catalina.core.ApplicationContext log

就这样我没成功

我导入的包有 ibatis-common-2.jar ibatis-sqlmap-2.jar ibatis-dao-2.jar 其他的包都和是STRUTS 和SPRING 还有其他的包了

请教大家帮我看看那里有问题 最好能总结一下 struts2 spring ibatis 搭建的步骤和相关的 JAR包

[b]问题补充:[/b]
还有一个问题。。。

就是ibatis 不能用spring 的aop来管理事物吗? 如果能的话要怎样来写??


tx:attributes
read-only="true" />

/tx:attributes
/tx:advice

提示创建txAdvice错误

错误信息
Error creating bean with name 'txAdvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined

谢谢PURAS 刚才的回答。。

  • 写回答

2条回答 默认 最新

  • Puras 2009-06-30 13:09
    关注

    com.ibatis.common.exception.NestedRuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource sqlMap.T_User.xml

    这句已经写的很明显了,没有找到sqlMap.T_User.xml啦
    如果你的T_User.xml是在sqlMap目录下,那将

    改成

    好好检查一下自己的各个配置文件

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题