Largemage 2020-08-06 17:08 采纳率: 50%
浏览 1245
已结题

SpringBoot项目使用mybatis逆向工程生成的Mapper无法自动注入,报required a bean of 'xxx' that could not be found错误,求解决方案!

报错内容为

Field vulLibMapper in com.shtec.dataonweb.service.TestService required a bean of type 'com.shtec.dataonweb.dao.VulLibMapper' that could not be found.
The injection point has the following annotations:
@org.springframework.beans.factory.annotation.Autowired(required=true)



Service内容为

package com.shtec.dataonweb.service;
import com.shtec.dataonweb.bean.VulLibExample;
import com.shtec.dataonweb.dao.VulLibMapper;
import com.shtec.dataonweb.bean.VulLib;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class TestService {

    @Autowired
    VulLibMapper vulLibMapper;

    public VulLib testUpdated() {
        VulLibExample example = new VulLibExample();
        List<VulLib> list = vulLibMapper.selectByExample(example);
        return list.get(0);
    }
}



Mapper内容就不列出来了,都是根据generatorConfig.xml自动生成的,mybatis-generator-core的版本为1.3.4,mybatis-spring-boot-starter的版本为2.1.3,mybatis-generator-maven-plugin的版本为1.3.2。


很多方法都试过,给Mapper加@Repository无效。如果用@MapperScan则会报如下错误


nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required


我自己搞定了,原因是没配置application.yml内的mybatis配置,把xml文件和接口文件都放在一个包下,然后配置:

mybatis:
  mapper-locations: com.shtec.dataonweb.mapper.*.xml
  type-aliases-package: com.shtec.dataonweb.bean

再入口类加上@MapperScan("com.shtec.dataonweb.mapper")自动扫描。


最后在pom.xml的bulid里加上:

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

大功告成~

  • 写回答

7条回答 默认 最新

  • 牧子与羊 2020-08-06 17:54
    关注

    看service没用,看这句话:required a bean of type 'com.shtec.dataonweb.dao.VulLibMapper' that could not be found.意思是说你的service依赖的dao找不到,说明你的dao层类没有被代理到,这里要看看是否配置了这些参数:
    1. 指定dao层的interface路径
    2. 指定mapper.xml的路径
    3. 是否配置了数据源,这里sessionFactory自动注入需要数据源的

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?