|ω・)267 2024-05-25 19:43 采纳率: 88.9%
浏览 7
已结题

MyBaits-Plus

我使用mybaits-plus自动生成的list()方法进行查询数据库的测试时出现报错。
报错信息如下:
Error creating bean with name 'helloController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我的目录结构为:

img

其中controller层次的代码为
HelloController:

package com.controller;

import com.entity.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController注释表示一个用于处理HTTP请求的控制器
@RestController
public class HelloController {
    @GetMapping
    public String hello(){
        return "测试成功!!!!";
    }

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public List<User> list(){
        return userService.list();
    }
}

实体层
User:

package com.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

@Data
public class User {

    private Integer id;
    private String no;
    private String name;
    private String password;
    private Integer age;
    private Integer sex;
    private String phone;
    private Integer roleId;
    @TableField("isValid")
    private String isvalid;
}

UserService:

package com.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.entity.User;

public interface UserService extends IService<User> {

}

UserServiceImpl:

package com.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.User;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

}

UserMapper:

package com.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.entity.User;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface UserMapper extends BaseMapper<User> {

}

启动代码为:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ChangkuJavaApplication {

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

}


在百度上面搜说在启动类上添加@MapperScan注解于是我加了一个@MapperScan("com.mapper")还是报错。

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-05-25 19:43
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据你提供的错误信息和代码,问题出在Spring Boot无法自动装配UserMapper接口。错误信息提示No qualifying bean of type 'com.mapper.UserMapper' available,意味着Spring容器中没有找到匹配UserMapper类型的bean。

    解决这个问题,你可以尝试以下几个步骤:

    1. 确保UserMapper接口上有正确的注解

      • 你的UserMapper接口上已经使用了@Mapper注解,这是正确的。确保这个注解没有被遗漏。
    2. 检查application.yml配置

      • 确保你的MyBatis配置是正确的。通常,Spring Boot会自动配置MyBatis,但如果有特殊的配置需求,需要在application.yml中指定。
    3. 检查包扫描

      • 你已经尝试使用@MapperScan("com.mapper")注解来指定Spring进行包扫描。确保这个注解被添加到了正确的启动类上。
    4. 检查UserServiceImpl的实现

      • 你的UserServiceImpl实现了UserService接口,并继承了ServiceImpl。这是正确的,但需要确保UserServiceImpl类上有@Service注解,这样Spring Boot才能将其作为Bean管理。
    5. 检查HelloController的依赖注入

      • HelloController中通过@Autowired注入了UserService,这是正确的。确保没有其他的配置问题导致UserService无法被注入。
    6. 检查Spring Boot的自动配置

      • 确保没有其他的配置或代码覆盖了Spring Boot的自动配置。
    7. 清理和重建项目

      • 有时候,IDE或者构建工具可能会出现缓存问题。尝试清理和重建项目。
    8. 检查IDE的配置

      • 确保你的IDE(如IntelliJ IDEA或Eclipse)正确地识别了项目的配置和依赖。

    如果以上步骤都无法解决问题,你可以尝试以下额外的步骤:

    • 检查是否有多个Spring应用上下文:如果你的项目中有多个Spring应用上下文,确保UserMapper在正确的上下文中被扫描和注册。
    • 检查是否有其他配置类:如果你的项目中有其他配置类,确保它们没有覆盖掉UserMapper的自动装配。
    • 查看Spring Boot的日志:启动Spring Boot应用时,查看控制台输出的日志,可能会有关于Bean创建失败的更多信息。

    至于参考资料,你可以查看以下资源:

    希望这些信息能帮助你解决问题。如果问题仍然存在,请提供更多的信息,以便进一步分析。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月31日
  • 创建了问题 5月25日

悬赏问题

  • ¥15 Opencv(C++)异常
  • ¥15 VScode上配置C语言环境
  • ¥15 汇编语言没有主程序吗?
  • ¥15 这个函数为什么会爆内存
  • ¥15 无法装系统,grub成了顽固拦路虎
  • ¥15 springboot aop 应用启动异常
  • ¥15 matlab有关债券凸性久期的代码
  • ¥15 lvgl v8.2定时器提前到来
  • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
  • ¥15 cam_lidar_calibration报错