报错信息如下:
Description:
Field userDao in com.test.springmvc.service.impl.UserServiceImpl required a bean of type 'com.test.springmvc.dao.UserDao' 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.test.springmvc.dao.UserDao' in your configuration.
截图:
项目目录结构:
SpringBootApplication启动类和controller、service、dao包同一级才能扫描到
在保证目录结构没问题的情况下:
1.检查自己的依赖MyBatis相关依赖是否齐全
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>springmvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc</name>
<description>SpringMVCPractice</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
</project>
2.配置文件是否配置了jdbc信息和MyBatis的别名和包扫描
#端口
server.port=8080
#jdbc配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb09?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#MyBatis
mybatis.mapper-locations=classpath:com/test/dao/*.xml
mybatis.type-aliases-package=com.test.domain
并检查对应的Mapper文件
<?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.test.springmvc.dao.UserDao">
<select id="findALL" resultType="com.test.springmvc.domain.User">
select *
from user
</select>
<select id="findById" resultType="com.test.springmvc.domain.User">
select *
from user
where id=#{id}
</select>
<insert id="addUser">
insert into user (name,age)
values (#{name},#{age})
</insert>
<delete id="delUserById">
delete
from user
where id=#{id}
</delete>
<update id="updateUser">
update user
set name=#{name},age=#{age}
where id=#{id}
</update>
</mapper>
3.最后查看controller、service和dao是否添加了对用的注解。尤其是dao接口上的注解,要加上@Mapper这个ibatis的注解
package com.test.springmvc.dao;
import com.test.springmvc.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface UserDao {
/**
* 查询全部User
*
* @return
*/
List<User> findALL();
/**
* 根据id查询User
*
* @param id
* @return
*/
User findById(int id);
/**
* 新增User
*
* @param user
* @return
*/
Integer addUser(User user);
/**
* 根据id删除
*
* @param id
* @return
*/
Integer delUserById(int id);
/**
* 修改User
*
* @param user
* @return
*/
Integer updateUser(User user);
}