在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  HTML/ SpringBoot集成MyBatis,顯示找不到Mapper。

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

SpringBoot集成MyBatis,啟動(dòng)項(xiàng)目,報(bào)錯(cuò)顯示找不到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.

找了一天了,還是沒找到問題所在,下面有我寫的一個(gè)簡化后的demo,能幫我看看錯(cuò)在哪里嗎?
目的是從數(shù)據(jù)庫查詢數(shù)據(jù)。

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

萬分感謝??!

回答
編輯回答
誮惜顏

你是在Mapper類上加了一個(gè)@Component,這個(gè)注解是聲明組件,往往是不明確這個(gè)組件在mvc中哪一層才寬泛的使用@Component來交給spring進(jìn)行管理。
但mybatis中的bean(UserMapper),屬于mapper層,應(yīng)該首先需要經(jīng)過mybatis作處理才可以。所以要首先把mapper文件加載到mybatis中,由mybatis轉(zhuǎn)換或加載成spring能使用的bean
正確的做法應(yīng)該是在DemoApplication類上加上@MapperScan("com.example.demo.mybatis")
如有錯(cuò)誤請(qǐng)指出。

2017年9月23日 04:33