Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 510|回复: 0

Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-7-18 17:24:49 | 显示全部楼层 |阅读模式

    果然不看教程直接使用在遇到问题会懵逼,连解决问题都得搜半天还不一定能帮你解决了。。。

    ***************************
    APPLICATION FAILED TO START
    ***************************
    Description:
    Field mapper in com.demo.service.impl.UserServiceImpl required a bean of type 'com.demo.mapper.UserMapper' that could not be found.
    Action:
    Consider defining a bean of type 'com.demo.mapper.UserMapper' in your configuration.

    SpringBoot启动失败,告诉我Bean配置失败,楼主看了看  该用的注解都用上了  这是咋的回事嘞?

    mapper(Dao层)

     1 package com.demo.mapper;
     2 
     3 import org.springframework.context.annotation.ComponentScan;
     4 import org.springframework.stereotype.Repository;
     5 
     6 import com.demo.domain.User;
     7 
     8 //@Component
     9 @Repository
    10 public interface UserMapper {
    11 
    12     public User gYeMian(User u);
    13 
    14     public int sYeMian(User u);
    15 
    16 }

    service

    package com.demo.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.demo.domain.User;
    import com.demo.mapper.UserMapper;
    import com.demo.service.UserService;
    
    @Service(value = "userService")
    public class UserServiceImpl implements UserService{
        
        @Autowired
        private UserMapper mapper;
    
        @Override
        public User gYeMian(User u) {
            User user = mapper.gYeMian(u);
            return user;
        }
    
        @Override
        public int sYeMian(User u) {
            int i = mapper.sYeMian(u);
            return i;
        }
    
    }

    controller

     1 package com.demo.controller;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.ResponseBody;
     9 
    10 import com.demo.domain.User;
    11 import com.demo.service.UserService;
    12 
    13 @Controller
    14 @RequestMapping(value = "/uc")
    15 public class UserController {
    16     
    17     @Autowired
    18     private UserService userService;
    19     
    20     @ResponseBody
    21     @RequestMapping("/stemp.htm")
    22     private String sYeMian(String muBan, HttpServletRequest request){
    23 
    24         User u = new User();
    25         u.setMuBan(muBan);
    26         System.out.println("muBan=" + muBan);
    27         int i = userService.sYeMian(u);
    28         
    29         if (i>0){
    30             return "存储成功";
    31         }
    32             return "存储失败";
    33     }
    34 
    35 }

    后来在网上看到网友说要用@Mapper注解,这才把问题解决了   至于具体原因,楼主还需要好好看看文档再来解释。

    解决方案一:

    mapper(Dao层)

     1 package com.demo.mapper;
     2 
     3 import org.apache.ibatis.annotations.Mapper;
     4 
     5 import com.demo.domain.User;
     6 
     7 @Mapper
     8 public interface UserMapper {
     9 
    10     public User gYeMian(User u);
    11 
    12     public int sYeMian(User u);
    13 
    14 }

     解决方案二:

    Application(启动类)

     1 package com.demo;
     2 
     3 import org.mybatis.spring.annotation.MapperScan;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 
     7 @SpringBootApplication
     8 @MapperScan(value = "com.demo.mapper")
     9 public class App 
    10 {
    11     public static void main(String[] args) throws Exception {
    12         SpringApplication.run(App.class, args);
    13     }
    14 }

    原因:在mybatis-spring-boot-autoconfigure的jar包中有一个类 MybatisAutoConfiguration,在这个类中的registerBeanDefinitions方法告诉了我们

     1 @Override
     2     public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
     3 
     4       logger.debug("Searching for mappers annotated with @Mapper");
     5 
     6       ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
     7 
     8       try {
     9         if (this.resourceLoader != null) {
    10           scanner.setResourceLoader(this.resourceLoader);
    11         }
    12 
    13         List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
    14         if (logger.isDebugEnabled()) {
    15           for (String pkg : packages) {
    16             logger.debug("Using auto-configuration base package '{}'", pkg);
    17           }
    18         }
    19 
    20         scanner.setAnnotationClass(Mapper.class);
    21         scanner.registerFilters();
    22         scanner.doScan(StringUtils.toStringArray(packages));
    23       } catch (IllegalStateException ex) {
    24         logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
    25       }
    26     }

    转:https://www.cnblogs.com/JealousGirl/p/bean.html
    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-1-31 21:40 , Processed in 0.061507 second(s), 30 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表