SpringBoot集成MyBatis,显示找不到Mapper。

SpringBoot集成MyBatis,启动项目,报错显示找不到Mapper

***************************
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.

找了一天了,还是没找到问题所在,下面有我写的一个简化后的demo,能帮我看看错在哪里吗?
目的是从数据库查询数据。

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

万分感谢!!

阅读 20.2k
2 个回答

你是在Mapper类上加了一个@Component,这个注解是声明组件,往往是不明确这个组件在mvc中哪一层才宽泛的使用@Component来交给spring进行管理。
但mybatis中的bean(UserMapper),属于mapper层,应该首先需要经过mybatis作处理才可以。所以要首先把mapper文件加载到mybatis中,由mybatis转换或加载成spring能使用的bean
正确的做法应该是在DemoApplication类上加上@MapperScan("com.example.demo.mybatis")
如有错误请指出。

回答一下目前在用的方式。

UserListMapper类改为

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();
}

更改了类的注解。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题