【问题标题】:Is using external DAO jar/library of MyBatis possible?是否可以使用 MyBatis 的外部 DAO jar/库?
【发布时间】:2014-05-13 01:55:52
【问题描述】:

我想制作一个由 dao、domain(java class)、mapper(xml) 和 mybatis-config.xml 组成的 jar。这样,多个项目可以包含 jar,避免项目之间的重复类。

我做了罐子,但出现以下错误。

当类、xml 文件在 spring 项目中而不是外部 jar 中时,它可以正常工作。

ERROR [main] (org.springframework.test.context.TestContextManager:314) - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@472b82e5] to prepare test instance [com.ktnet.ca.web.controller.CmpControllerTest@799eace3]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:154)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:100)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [test-database-context.xml]: Invocation of init method failed; nested exception is java.lang.Error: Unresolved compilation problems: 
    The import com.test.ca.param cannot be resolved
    SearchParam cannot be resolved to a type
    SearchParam cannot be resolved to a type
    SearchParam cannot be resolved to a type

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:610)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:128)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)

spring 配置文件如下。

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="ca-ds" p:mapperLocations="classpath:/com/test/ca/mapper/*mapper.xml"
        p:configLocation="classpath:/com/test/ca/mybatis-config.xml" />

有人知道如何解决这个问题吗?

【问题讨论】:

  • The import com.test.ca.param cannot be resolved -- SearchParam cannot be resolved to a type
  • 但是jar文件中存在包和类。当类、xml 文件在 spring 项目中而不是外部 jar 中时,它可以正常工作。
  • 有任何证据证明com.test.ca.param.SearchParam 实际上在类路径中?

标签: spring dao mybatis mapper


【解决方案1】:

我不确定我是否正确阅读了您的问题:

您在一个 JAR 文件中有映射器 XML(在包 /com/test/ca/mapper/*mapper.xml 下),并且似乎查找失败?

我认为是资源表达式"classpath:/com/test/ca/mapper/*mapper.xml" 导致了问题。 Spring实际上在这里做了一个技巧来实现通配符。它将首先根据通配符之前的路径(即/com/test/ca/mapper)在类路径中找到资源。对于包含此类资源的第一个类路径元素(JAR 等),它将使用该特定 JAR 来搜索 *mapper.xml

因此,您很可能有另一个 JAR 具有目录 /com/test/ca/mapper,而 Spring“错误地”使用它来查找您的 *mapper.xml

解决的方法很简单,改用classpath*:代替classpath:。这将告诉 Spring 从所有类路径元素中查找,而不是从第一个遇到的元素中查找。

【讨论】:

  • 我尝试使用 classpath*,但没有成功。无论如何感谢您的描述,因为我不知道通配符的工作方式。
【解决方案2】:

这个由 spring 引起的问题不能扫描你的映射器来做他们的 DI。让我们将组件扫描配置点添加到您的 jar 文件中的包中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2021-03-20
    相关资源
    最近更新 更多