欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Field testMapper in com.example.demo.service.TestService required a bean of type ‘com.example.demo.m

程序员文章站 2022-09-06 11:40:04
由于解决过程太漫长,直接给出解决方案: 原因mybatis的jar包导错了!Mapper接口类上使用了注解@Mapper,根据intellij idea提示引入了依赖: org.mybatis mybatis

由于解决过程太漫长,直接给出解决方案:

      原因mybatis的jar包导错了!Mapper接口类上使用了注解@Mapper,根据intellij idea提示引入了依赖:

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>

      然后@Mapper可以正常导包,但是会引发一系列的后续问题!

      正确的jar包依赖是:

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

在此记录一下我的解决过程,时间不充足的就没必要往下看了!

问题描述:springboot和mybatis建了个项目,写了个查询,启动项目报错:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-11 09:57:57.829 ERROR 16916 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field testMapper in com.example.demo.service.TestService required a bean of type 'com.example.demo.mapper.TestMapper' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.demo.mapper.TestMapper' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:64216', transport: 'socket'

Process finished with exit code 1

           我的Mapper接口上添加了@Mapper注解,注解的jar包地址:import org.apache.ibatis.annotations.Mapper;

            也没有问题,启动就是报错!

            然后我注意到Service中的自动注入dao对象名颜色变红了!我就在注解@Autowired中添加属性值required=false,取消校验,

@Service
public class TestService {
    // 添加require=false取消校验对象指针地址
    @Autowired(required=false)
    private TestMapper testMapper;

    public List<Map<String,Object>> getFirstSelect(){
        return testMapper.getFirstSelect();
    }

}

         项目可以正常启动了!心情老高兴了!结果一访问,后台报错:

java.lang.NullPointerException: null
	at com.example.demo.service.TestService.getFirstSelect(TestService.java:16) ~[classes/:na]
	at com.example.demo.controller.TestController.getFirstSelect(TestController.java:20) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_251]

           原因:@Mapper注解没起作用,Mapper类TestMapper没有被注册到springioc,导致注入失败,报错空指针!

          于是乎换了一种Mapper扫描方式:在启动类上面添加注解@MapperScan(mapper文件包地址),

        

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//臊面该包下面所有的mapper文件,和@Mapper注解两者可以二选一
@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

        @MapperScan的依赖:

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>

      启动后接着报错:

Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
	at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:122) ~[mybatis-spring-2.0.5.jar:2.0.5]

      百度原因:缺少依赖!导入下面依赖!

		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

      接下来就是个死循环了!哈哈!

        如果使用了此依赖,但是此依赖中的jdbc版本太旧了,导致连接不到数据库!
        如果使用exclude排除了这个jdbc版本,就重新报错:Property 'sqlSessionFa。。。。。

      问题又重新回到了原点!@Mapper!好像无解了!(能力有限)

     重新分析原因:结论是jar包有问题!但是正确的jar是什么呢?

      好在公司里有成功的spingboot+mybatis项目!pom文件拿来参考了一下!问题解决!

      

本文地址:https://blog.csdn.net/u011220266/article/details/107280714