@Mapper和@MapperScan注解,xxx required a bean of type xxx that could not be found,

搭建springboot+mybatis+mysql+maven项目,从controller-service-dao,最后写mapper.xml,启动之后报错如下:

Field helloWorldDAO in com.example.demo.service.ServiceImpl.HelloWorldServiceImpl required a bean of type 'com.example.demo.DAO.HelloWorldDAO' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.demo.DAO.HelloWorldDAO' in your configuration.

 报错基本的意思就是无法找到注入HelloWorldDAO这个bean,经过查找定位,可以用以下两种方法解决:

1:在HelloWorldDAO这个类上添加@Mapper注解,代码如下:

package com.example.demo.DAO;

import com.example.demo.entity.student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
@Mapper
public interface HelloWorldDAO {
     List<student> query(String studentId) throws Exception;
}

2:在启动类上添加包@MapperScan注解,代码如下:

@MapperScan("com.example.demo.DAO")
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

总结:在mapper接口上使用@Mapper注解,可以生成相应的接口实现类。如果接口类比较少的话,当然用@Mapper也是可以的。但是,我们平常在工作中,可能对应的接口比较多,这个时候用@Mapper注解就显得比较麻烦了。这个时候我们可以在启动类上加@MapperScan注解,@MapperScan("com.example.demo.DAO"),里面写对应接口的路径,这样我们只需要配置一次,在当前路径下的接口类中都可以使用了,这样是不是很方便呢。

知识就是要不断的学习,不断的复习,才会记忆的更加的深刻。加油,美好的风景一直在路上。

引用提供了两种解决方法来解决Mapper层报错的问题。方法1是在IDEA的设置中去除映射方法检查,方法2是在Mapper对应方法上添加注解@MapKey("key")来映射返回的Map中的key值。如果是已有项目并且有很多Mapper的情况下,推荐使用方法1。如果是团队项目并且已经有很多Mapper被开发,那就不建议使用@MapKey注解,而是使用方法1来避免修改其他人的代码。 另外,引用提供了一种关闭报错的方法,即在报错的方法上添加@SuppressWarnings("MybatisXMapperMethodInspection")注解来关闭报错。 对于@Autowired出现的报错"Could not autowire. No beans of 'XXX' type found.",这是因为没有找到对应类型的bean。可能是因为没有正确配置或者没有添加对应的依赖。需要检查配置文件或者添加对应的依赖来解决这个问题。 引用中提到,使用字段注入(Field injection)时,不推荐添加@Autowired(required = false)注解。这是因为字段注入不是一个推荐的注入方式。 综上所述,对于@MapKey is required的报错问题,可以通过上述方法解决。对于@Autowired出现的报错问题,需要检查配置文件或添加对应的依赖。同时需要避免使用@Autowired(required = false)进行字段注入。123 #### 引用[.reference_title] - *1* [解决:IDEA安装MyBatisPlus插件后mapper报@MapKey is required](https://blog.csdn.net/lyj15012351173/article/details/130735128)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *2* *3* [【javaCould not autowire. No beans ofXXXtype found.](https://blog.csdn.net/u010638673/article/details/127283893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值