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

Posted 爱吃醋的兔子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot:Action:Consider defining a bean of type '*.*.*' in your configuration解决方案相关的知识,希望对你有一定的参考价值。

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

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

 







以上是关于Spring Boot:Action:Consider defining a bean of type '*.*.*' in your configuration解决方案的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Spring Boot 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web?

《02.Spring Boot连载:Spring Boot实战.Spring Boot核心原理剖析》

spring-boot-quartz, 依赖spring-boot-parent

spring-boot系列:初试spring-boot

Spring Boot:Spring Boot启动原理分析

Spring Boot:Spring Boot启动原理分析