SpringBoot整合Mybatis遇到的问题

本文介绍了初学者在SpringBoot项目中集成Mybatis时遇到的UserMapper注入失败问题及解决方案。通过添加@Mapper注解成功解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初学SpringBoot,在集成Mybatis的过程中遇到了点问题:
出现问题之前的操作:使用Mybatis的generator自动生成dao层和entity层中的类和接口
在UserController中注入userMapper:
代码如下:
@Autowired
private UserMapper userMapper;
运行SpringBoot应用,报错!!!
问题如下:
Field userMapper in com.coder520.mamabike.user.controller.UserController required a
 bean of type 'com.coder520.mamabike.user.dao.UserMapper' that could not be found.
问题说注入失败,找不到UserMapper。
解决问题步骤:
步骤一:.查看pom.xml文件中是否加了如下代码:

<resource>
       <directory>src/main/java</directory>
           <includes>
              <include>**/*.xml</include>
           </includes>
    </resource>
无效;
步骤二:很多人说在SpringBoot主程序中添加@MapperScan("路径"),或许这样可以解决一些人的问题,但我的还是没有解决。
步骤三:在UserMapper.java 中添加注解:@Mapper;
问题解决;
import org.apache.ibatis.annotations.Mapper;

      @Mapper
public interface UserMapper {
    int deleteByPrimaryKey(Long id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}
有人说是SpringBoot版本过高的原因,需要添加@Mapper,不知道所以然,但问题解决了。
运行成功图:






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值