SpringBoot Integration MyBatis, shows that Mapper cannot be found.

SpringBoot integrates MyBatis, startup project, error shows that Mapper cannot be found

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

Description:

Field userListMapper in com.example.demo.service.UserService required a bean of type "com.example.demo.mybatis.UserListMapper" that could not be found.


Action:

Consider defining a bean of type "com.example.demo.mybatis.UserListMapper" in your configuration.

I"ve been looking for it all day, but I still can"t find the problem. Here"s a simplified demo, I wrote. Can you help me see what"s wrong? The purpose of
is to query data from the database.

DemoApplication

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(value = "com.example.demo")
public class DemoApplication {

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

User

package com.example.demo.model;

public class User {

    private String id;

    private String name;

    public String getId() {

        return id;
    }

    public void setId(String id) {

        this.id = id;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }
}

UserListMapper

package com.example.demo.mybatis;

import com.example.demo.model.User;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface UserListMapper {

    List<User> findAllUsers();
}

UserListMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.mybatis">

    <select id="findAllUsers" resultType="com.example.demo.model.User">
        select id, name from User
    </select>

</mapper>

UserService

package com.example.demo.service;

import com.example.demo.mybatis.UserListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserListMapper userListMapper;
}

application.yml

spring:
  datasource:
    url:
    username:
    password:
    driver-class-name:
    hikari:
      minimum-idle: 10
      maximum-pool-size: 100

Thank you very much!

Mar.12,2021

you have added @ Component, to the Mapper class. This annotation declares the component, and it is often not clear which layer of the mvc this component is used to broadly use @ Component to be managed by spring.
but the bean (UserMapper), in mybatis belongs to the mapper layer and should be processed by mybatis first. So to load the mapper file into mybatis first, the correct way to convert or load mybatis into bean
that can be used by spring should be to add @ MapperScan ("com.example.demo.mybatis")
to the DemoApplication class. Please point out if there is any error.


answer the way you are currently in use.

UserListMapper class changed to

package com.example.demo.mybatis;

import com.example.demo.model.User;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
public interface UserListMapper {

    List<User> findAllUsers();
}

changed the comments for the class.

Menu