qq_39015948
qq_39015948
采纳率40%
2018-03-28 07:51 阅读 2.1k

mybatis 注入mapper的问题

5

mybatis动态代理mapper(interface),然后用@Res..注入一直报错,然后无奈用了配置文件,
在service层用get/set方法来做,发现这个MAPPER还是空的!!

@Service("testService")
public class TestServiceImpl implements TestService{

private TestMapper testMapper;


public TestMapper getTestMapper() {
    return testMapper;
}


public void setTestMapper(TestMapper testMapper) {
    this.testMapper = testMapper;
}


@Transactional
@Override
public Object tset1(){  
    TestMapper mapper = ApplicationContextFactory.getBean("testMapper", TestMapper.class);
    mapper.insertOne();
    throw new RuntimeException("test");
}

}

如上,但是ApplicationContextFactory.getBean又可以取到并且操作,扫描包应是没问题的,不然应该会报错吧

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

12条回答 默认 最新

  • gold159 gold159 2018-03-28 08:09

    @Autowired
    private TestMapper testMapper;

    点赞 2 评论 复制链接分享
  • aquriushu aquriushu 2018-03-28 08:13

    你的类变量testMapper只是声明了bean,但是并没有注入到这个类里面来。
    这段代码看起来并没有错误。不过有可能是因为没有截图完。
    @Autowired/@resource两个注解都是可以的。
    private TestMapper testMapper;

    点赞 评论 复制链接分享
  • gold159 gold159 2018-03-28 08:15

    你mapper加上
    @Component("testmapper")
    @Mapper
    public interface TestMapper{

    用@Resource注入试一试
    @Resource(name="testmapper")
    private TestMapper testMapper;

    点赞 评论 复制链接分享
  • qq_39015948 qq_39015948 2018-03-28 08:26

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.zcloudMVC.mapper.TestMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}
    这是用注解报的错

    点赞 评论 复制链接分享
  • wushucheng18 诚哥丿 2018-03-28 08:27

    mapper 相当于hibernate的dao 组件了,需要被spring扫描到,检查一下spring的配置文件:

    ....



    点赞 评论 复制链接分享
  • qq_39015948 qq_39015948 2018-03-28 08:28
    <bean name="testService" class="com.zcloudMVC.service.impl.TestServiceImpl">
        <property name="testMapper">
            <ref bean="testMapper" />
        </property>
    </bean>
    
    这是配置文件mapper到 service的
    
    点赞 评论 复制链接分享
  • wushucheng18 诚哥丿 2018-03-28 08:30
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xxx.xxx.*.dao" />
    </bean>
    
    点赞 评论 复制链接分享
  • qq_39015948 qq_39015948 2018-03-28 08:30





    这是配置文件,mapper注入到service的
    
    
    @Service("testService")
    

    public class TestServiceImpl implements TestService{

    private TestMapper testMapper;
    
    
    public TestMapper getTestMapper() {
        return testMapper;
    }
    
    
    public void setTestMapper(TestMapper testMapper) {
        this.testMapper = testMapper;
    }
    
    
    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
    @Override
    public Object tset1(){  
        TestMapper mapper = ApplicationContextFactory.getBean("testMapper", TestMapper.class);
        mapper.insertOne();
        throw new RuntimeException("test");
    }
    

    }

    用配置文件方式的service

    点赞 评论 复制链接分享
  • wushucheng18 诚哥丿 2018-03-28 08:32

    哎呦, 手敲的代码竟然都不见了


    点赞 评论 复制链接分享
  • qq_39015948 qq_39015948 2018-03-28 08:46
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zcloudMVC.mapper;com.emacle.docm.mapper" />
    </bean>
    
    点赞 评论 复制链接分享
  • fanwt7236 fanwt7236 2018-03-28 10:15

    我怎么感觉是配置文件上的问题呀?会不会是注入错了容器?

    点赞 评论 复制链接分享
  • qq_38353586 qq_38353586 2018-03-28 13:37

    private TestMapper testMapper;
    注入

    点赞 评论 复制链接分享

相关推荐