MyBatis找不到Mapper(Spring Boot项目) - 已解决

No MyBatis mapper was found in '[com.example]' package. Please check your configuration.

Description:

Field loader in com.ex.data.MainController required a bean of type 'com.ex.data.load.ExampleMapper' 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.ex.data.load.ExampleMapper' in your configuration.

问题有二,先从最简单的入手:

1.检查是否指定了类扫描路径,MyBatis项目两个都得指定:

@MapperScan("com.example.data.load")
@SpringBootApplication(scanBasePackages = "com.example.data")

2.是否正确定义了mapper:

/src/main/java/com/ex/data/load/LoaderMapper.java

package com.ex.data.load;

public interface LoaderMapper {
	
	public Long count();

}

/src/main/resources/com/ex/data/load/LoaderMapper.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.ex.data.load.LoaderMapper">
 <select id="count" resultType="java.lang.Long">
  select count(*) from example
 </select>
</mapper>

说明:目录结构需一致,namespace也可保持一致。