通用Mapper不能注入

lengmianshi 发布于 2016/10/03 10:36
阅读 2K+
收藏 0

@Liuzh_533 你好,大神,想跟你请教个问题:

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

这里在配置basePackage的时候,将通用Mapper<T>所在的包com.isea533.mybatis.mapperhelper也配置上了。这样就能在Spring4中直接注入Mapper<T>。

请问basePackage配置的两个包是我自己定义的吗?这样配了后在baseSerivce里就无法注入通用Mapper了。报这个错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemCatController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemCatService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [tk.mybatis.mapper.common.Mapper] is defined: expected single matching bean but found 3: itemCatMapper,itemDescMapper,itemMapper

部分代码如下:

public abstract class BaseService<T extends BasePojo> {

    @Resource
    Mapper<T> mapper;
    
    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public T queryById(Long id){
        return mapper.selectByPrimaryKey(id);
    }

贴出一个Service代码,其他类似:

@Service
public class ItemCatService extends BaseService<ItemCat>{
/*

贴出一个Mapper的代码,其他两个类似,只是泛型实体类不同:


public interface ItemDescMapper extends Mapper<ItemDesc>{
}

spring xml配置:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tt.mapper" />
</bean>
说明:我把所有的mapper接口都放在com.tt.mapper包下

望大神能指点一二,不甚感激!

加载中
0
Liuzh_533
Liuzh_533
不要扫描通用接口!
0
l
lengmianshi

解决方案:

1.@Resources改为@Autowired

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

改为:

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

0
无解无界
无解无界

解决了吗?我也遇到这个问题

Consider defining a bean of type 'tk.mybatis.mapper.common.Mapper' in your configuration

l
lengmianshi
看我上面的回复
返回顶部
顶部