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

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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b