0

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.

8
  • 1
    I'm actually surprised it worked with 2.7. afaik support for Ibatis has been dropped a long while ago, instead you should be using MyBatis (the successor of Ibatis). Commented Jan 22, 2024 at 10:25
  • @M.Deinum I meant mybatis, sorry wrong tag, now its fixed. Sorry this is my 3rd time using stack overflow, a bit clumsy Commented Jan 22, 2024 at 10:29
  • If you are using the starter you will also need to upgrade that to a Spring Boot 3 compatible version. Commented Jan 22, 2024 at 10:31
  • @M.Deinum you mean spring-boot-starter-web? Commented Jan 22, 2024 at 10:35
  • @lilpiggie0522 You didn't mention the version of mybatis-spring-boot-starter, but it has to be 3.0.3. Commented Jan 22, 2024 at 11:04

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

The mybatis-spring-boot-starter pulls in the MyBatis dependencies and the MyBatis auto-configuration for Spring Boot. The configuration in Spring Boot got quite an overhaul in Spring Boot 3.x (actually already in 2.7.x but that only issued a warning 3.0 removed the old ways). Hence the old version of the auto configuration for MyBatis won't work with newer Spring Boot versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.