我把Spring和Mybatis连用。我已经将它配置为扫描整个项目中的映射器,我认为它确定了一个映射器,因为它找到了一个XML文件,该文件引用了一个java接口。
但这在今天被证明是不正确的,因为我不得不添加一个新的界面,它不是映射类,而且Mybatis认为是这样的,因此由于这个错误,它在我的应用程序中造成了问题:
映射语句集合不包含com.blah.MyInterface.someMethod的值。
com.blah.MyInterface只是一个简单的接口,需要包含在Spring中,所以我给了它@Component标记。用错标签了吗?这就是混乱的根源吗?
我只需要创建这个接口,这样我就可以让一个代理将我的数据库调用包装在一个地方,在这里我可以放置一个@Transactional,因为Spring在我的Controller方法中会忽略它。
样本代码
package com.blah.something;
@Component public interface MyInterface {
public void someMethod( SomeObject obj) throws Exception;
}
package com.blah.something;
public class MyImplementation implements MyInterface {
@Transactional
public void someMethod( SomeObject obj) throws Exception {
... do a whole bunch of stuff
}
}我不希望这包括在MyBatis地图!
编辑:根据请求添加了mybatis:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="false" />
<setting name="defaultStatementTimeout" value="60"/>
</settings>
<typeAliases>
<typeAlias alias="StripTrailingZerosBigDecimalTypeHandler" type="com.blah.typehandlers.StripTrailingZerosBigDecimalTypeHandler"/>
</typeAliases>
</configuration>这是我的spring配置的一部分,它调用了mybatis扫描仪:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.blah" />
</bean>因此,我设置它扫描整个项目,其中包括我的界面以上,但我无法想象,它只是抓住每一个接口,并考虑他们所有的映射者!
在我的调试日志中,我看到mybatis正在拾取我的接口:
12/9/13 11:18:44 904 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.findCandidateComponents:4125] - Scanning file [D:\Weblogic\wls11\domains\ldapdomain\autodeploy\default\WEB-INF\classes\com\blah\MyInterface.class]
12/9/13 11:18:44 904 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.findCandidateComponents:4125] - Identified candidate component class: file [D:\Weblogic\wls11\domains\ldapdomain\autodeploy\default\WEB-INF\classes\com\blah\MyInterface.class]
12/9/13 11:18:44 904 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.findCandidateComponents:4125] - Scanning file [D:\Weblogic\wls11\domains\ldapdomain\autodeploy\default\WEB-INF\classes\com\blah\MyImplementation .class]
12/9/13 11:18:44 904 [org.mybatis.spring.mapper.MapperScannerConfigurer$Scanner.findCandidateComponents:4125] - Ignored because not a concrete top-level class: file [D:\Weblogic\wls11\domains\ldapdomain\autodeploy\default\WEB-INF\classes\com\blah\MyImplementation .class]这个接口没有XML,也没有映射名称空间,它只是一个普通的常规接口,MyBatis不应该认为它是映射器服务
发布于 2013-12-10 15:42:08
好的,看起来MyBAtis扫描器确实没有任何接口,它没有任何“智能”来识别映射器接口,我认为它是基于查找匹配的XML或名称空间的。我必须向mapper配置添加一个过滤器,然后引入一个新的注释来注释我的mapper接口。
https://stackoverflow.com/questions/20406045
复制相似问题