本文介绍了Mybatis Mapper扫描仪为什么选错了班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring与Mybatis一起使用.我将其配置为扫描整个项目中的映射器,并假定它确定了映射器,因为它找到了引用Java接口的XML文件.

I use Spring with Mybatis. I have it configured to scan for mappers in my whole project and I assumed it determined a mapper because it found an XML file which has reference to a java interface.

但这在今天被证明是不正确的,因为我不得不添加一个不是映射器类的新接口,而Mybatis认为是这样,因此由于以下错误,这在我的应用程序中引起了问题:

But this is proven incorrect today because I had to add a new interface which is not a mapper class and Mybatis thinks it is, so it is causing problems in my app due to this error:

映射的语句"集合不包含com.blah.MyInterface.someMethod的值

Mapped Statements collection does not contain value for com.blah.MyInterface.someMethod

com.blah.MyInterface只是一个简单的接口,我需要将其包含在Spring上下文中,因此我给了它@Component标记.那是使用错误的标签吗?那是混乱的源头吗?

com.blah.MyInterface is just a simple interface which I needed to be included in Spring context so I gave it the @Component tag. Is that the wrong tag to use? Is that where the confusion comes from?

我只需要创建此接口,就可以让代理将我的数据库调用包装在一个可以放置@Transactional标记的位置,因为Spring在Controller方法中会忽略它.

I just needed to create this interface so that I can have a proxy wrap my database calls in one place where I can put a @Transactional tag, since Spring ignores it when it is in my Controller method.

示例代码

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映射器中包含它!

I dont want this included in the MyBatis mappers!

根据要求添加mybatis配置xml:

added the mybatis config xml as requested:

<?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 xml配置的一部分,称为mybatis映射器扫描程序:

This is the part of my spring xml config which calls the mybatis mapper scanner:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.blah" />
</bean>

因此,我将其设置为扫描整个项目,该项目包括上面的界面,但我无法想象它只是抓住了每个界面并考虑了所有映射器!

So I set it to scan the whole project which includes my interface above but I can't imagine it just grabs every single interface and considers them all mappers!

在调试日志中,我看到mybatis正在接我的界面:

In my debug log I see mybatis picking up my interface:

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,没有XML命名空间,它只是一个普通的常规接口,MyBatis不应该认为它是Mapper服务

There is no XML for this interface, there is no mapper namespace for it, it's just a plain old regular interface and MyBatis should not be thinking it is a mapper service

推荐答案

好吧,看起来MyBAtis扫描仪确实确实占用了每个接口,它没有任何智能"来标识映射器接口,就像我认为的那样-查找匹配的XML或名称空间.我必须在映射器配置中添加一个过滤器,然后引入一个新的注释来注释我的映射器接口.

Ok it looks like MyBAtis scanner does indeed take every interface, it does not have any "smarts" in it to identify mapper interfaces as I thought it would - based on finding matching XML or namespaces. I had to add a filter to the mapper configuration and then introduce a new annotation to annotate my mapper interfaces.

这篇关于Mybatis Mapper扫描仪为什么选错了班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:29