2

在我的 Spring 项目中,Ibatis 有一个愚蠢的配置问题。请不要对我说这一切是如何设置的,我只是遵循“内部项目结构政策”。

所以这里是结构,我们有“ src/ main /resources/META-INF/ ”文件夹,其中包含应用程序使用的所有配置文件,然后有一个“ src/ test /resources/META-INF/ ” " 仅包含具有不同设置以运行单元测试的配置文件。

在我们的例子中,这只是一个文件,src/ main /resources/META-INF/spring/application- config.xml变成了src/ test /resources/META-INF/spring/test-application- config.xml。我不打算概述两者之间的细微差别,因为那部分工作正常。

test-application-config.xml导入src/ main /resources/META-INF/ spring /data-access-config.xml文件就好了,它又使用了src / main /resources/META-INF/ ibatis / sqlmap-config.xml成功...之后就是下地狱的时候了。

到目前为止,我们使用 Spring 在类路径中查找下一个配置文件,但是当我们点击sqlmap-config.xml时,我们将 Spring 框架留给了我相信的 ibatis 框架,它加载了其中定义的资源文件,相对于类路径(取自文档,不管是什么意思)。

sqlmap-config.xml中定义了一些我们正在使用的资源文件,它们位于src/ main /resources/META-INF/ ibatis /mapping文件夹中。它们是这样引用的:

<sqlMapConfig><sqlMap resource="/META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>

当我正常运行应用程序时效果很好,但是当我运行我的 JUnit 测试用例时,我得到一个 IO 异常,指出它找不到文件/META-INF/ibatis/mapping/MyObject.xml

我试图将sqlmap-config.xml中的路径更改为“ mapping/MyObject.xml ”,但这没有帮助。我也尝试使用 Spring 类路径前缀“ classpath:META-INF/ibatis/mapping/MyObject.xml ”,也没有工作。

任何人都会对如何正确设置 Ibatis 以使其适用于应用程序和 junit 有任何想法?

谢谢。

4

3 回答 3

1

为了解决这个问题,我从src/test/resources/META-INF文件夹中删除了所有的 Ibatis 文件和文件夹。

src/ main /resources/META-INF/ibatis/mapping文件中的sqlmap-config.xml现在映射如下:

<sqlMapConfig><sqlMap resource="META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>

请注意,与我最初的帖子相比,前导“/”消失了……我认为这就是这里的不同之处。

希望这可以帮助遇到类似问题的任何人。

于 2009-12-28T19:01:49.287 回答
0

Just to see whether what you are saying is actually the problem.. you might want to place your mappings (MyObject.xml) in the same folder as sqlmap-config.xml. I say this because I've had my fair share of spring + ibatis + unit testing problems. (see resolved question asked by me)

Also, you might be getting IO exception because the mappings file does not exist outside the container (when you run tests).

You should also post definition for bean created from SqlMapClientFactoryBean. This should have configLocation property that contains path to sqlMapConfig xml

于 2009-11-13T02:59:50.980 回答
0

我遇到了同样的问题,找不到(快速)解决方案来解释究竟可能出了什么问题。因此我的回答。

正如Ibatis 的 Spring 文档所说:

请记住,iBATIS 从类路径加载资源,因此请务必将“Account.xml”文件添加到类路径中。

在您的情况下,通过添加META-INF到您的 webproject 构建路径,即如果您使用 Eclipse,<classpathentry kind="src" path="META-INF"/>请在您的项目中设置.classpath(这将在 Eclipse 的导航器视图下可见)

于 2014-03-05T19:00:51.570 回答