SpringBoot整合Mybatis扫描不到Mapper的问题

闲来无事,想学学springboot,开始搭建一个项目,但是一直显示mapper扫描不到的错误:

这里写图片描述
“Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘userMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.minibike.bike.user.dao.UserMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}”

“Field userMapper in com.minibike.bike.user.controller.UserController required a bean of type ‘com.minibike.bike.user.dao.UserMapper’ that could not be found.”
这里写图片描述

于是在网上找解决方案,都是治标不治本,不得不说现在的博客质量下降的厉害,太误导人了,打开好多个都是一样的内容,难道没人遇到的错误一样,连包名和类名也一样吗?有的人抄别人博客,类名和包名都懒得换一下?

最后解决方案是在Application.java主类上加注解@MapperScan(“com.minibike.bike.user.dao”)。括号中引号里边是mapper所在包路径。

加上注解后不再报错,也能正常输出oooo
这里写图片描述

第一次接触springboot,昨天下午到今早一直没找到问题所在,后来查文档、注解,然后不断尝试,最后终于解决。

当然这个只是我个人项目开发中遇到的问题解决办法,不一定通用,但是如过遇到这样的问题,也可以一试。

  • 42
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 70
    评论
Spring Boot 整合 MyBatis-Plus 的步骤如下: 1.添加 MyBatis-Plus 和 MyBatis-Spring-Boot-Starter 依赖。在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> ``` 2.添加数据库配置。在 application.yml 文件中添加以下配置: ``` spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false username: root password: root ``` 3.创建 Mapper 接口。在 mapper 接口上添加 @Mapper 注解,并且继承 BaseMapper 接口,例如: ``` @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 4.创建 Mapper XML 文件。在 resources 目录下创建 mapper 目录,并在该目录下创建与 Mapper 接口同名的 XML 文件,例如:UserMapper.xml。 ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> <result column="email" property="email" /> </resultMap> <select id="selectById" resultMap="BaseResultMap"> select * from user where id=#{id} </select> </mapper> ``` 5.在 Service 中调用 Mapper 接口。例如: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } } ``` 这样,Spring Boot 就成功整合MyBatis-Plus 和 Mapper XML 文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 70
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值