javaspring-bootinversion-of-controlmybatisspring-boot-3

Springboot 3.2.2 Failed to inject bean using @Autowire but works when changing springboot version back to 2.7.5


Hi there,

I get this error When I tried to start my main springboot class.

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

Description:

Field deptMapper in com.itheima.service.impl.DeptServiceImpl required a bean of type 'com.itheima.mapper.DeptMapper' 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.itheima.mapper.DeptMapper' in your configuration.

And the version I am using for springboot is 3.2.2

However, when I change it back to 2.7.5 in pom.xml, it starts the application fine. My codes remained the same when using both versions of springboot.

I just can't figure out why. My file structures are fine, I followed springboots' instructions on path defining. Here is a picture of my path if this helps: project structure

Anything related would be much appreciated, thank you!!! Below are my code:

controller java file:

package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController //
public class DeptController {
    @Autowired(required = true)
    private DeptService deptService;
//    @RequestMapping(value = "/depts", method = RequestMethod.GET)
    @GetMapping("/depts")
    public Result list() {
        log.info("search all departments");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }
}


service layer(DAO) interface file:

package com.itheima.service;

import com.itheima.pojo.Dept;

import java.util.List;

public interface DeptService {
    /*search all departments*/
    List<Dept> list();
}

Implementation class in service layer(DAO)

package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service //当前实现类交给IOC容器管理,成为容器的bean对象
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }
}

Mapper interfaces that fetches data from mysql using mybatis:

package com.itheima.mapper;

import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DeptMapper {
    //    查询全部的部门数据
    @Select("select * from dept")
    List<Dept> list();
}

Pojo class that IOC container injects data to:

package com.itheima.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dept {
    private Integer id;
    private String name;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

When I change springboot version back to 2.7.5 in pom.xml, it starts the application fine. My codes remained the same when using both versions of springboot. I just can't figure out why.


Solution

  • OK as @ave has mentioned, my problem was that the version of mybatis-spring-boot-starter I was using is 2.0.2 which was auto-generated by Idea IDE. I had not realised this was the problem since I reckon mybatis-spring-boot-starter is only in charge of encapsulating the columns from mysql into a pojo I had defined. Turns out that is some how important for springboot3's IOC injection as well. Which is something that I don't understand at the moment.

    Again thank @ave for helping me out.

    Solution: change version of following dependency in pom.xml of maven project to 3.0.3

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