今天本菜鸟搭建环境时,发现控制台有个警告,由于强迫症,决定干死它!警告信息如所示:
No MyBatis mapper was found in ‘[xx]’ package. Please check your configuration.
15:49:23.328 [main] DEBUG com.MainApplication - Running with Spring Boot v2.4.0, Spring v5.3.1
15:49:23.330 [main] INFO com.MainApplication - No active profile set, falling back to default profiles: default
15:49:25.811 [main] DEBUG tk.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [D:\workspace\springboot_test\target\classes\com\example\dao\PersonMapper.class]
15:49:25.812 [main] DEBUG tk.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'personMapper' and 'com.example.dao.PersonMapper' mapperInterface
15:49:25.813 [main] DEBUG tk.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'personMapper'.
15:49:26.133 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
15:49:27.261 [main] WARN org.mybatis.spring.mapper.ClassPathMapperScanner - No MyBatis mapper was found in '[com]' package. Please check your configuration.
15:49:28.056 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
15:49:28.120 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
15:49:28.121 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
15:49:28.122 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.39]
15:49:28.330 [main] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
15:49:28.331 [main] INFO org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 4906 ms
15:49:29.871 [main] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
15:49:30.311 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
15:49:30.362 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
15:49:30.381 [main] INFO com.MainApplication - Started MainApplication in 7.873 seconds (JVM running for 19.261)
结论
将tk.mybatis.spring.annotation.MapperScan 改为 org.mybatis.spring.annotation.MapperScan
package com;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import tk.mybatis.spring.annotation.MapperScan;
import javax.annotation.Resource;
/**
* 类描述
* 主启动类
*
* @author
* @version 1.0
* @date 2020/12/11 10:51
*/
@SpringBootApplication
@ComponentScan(value = {"com.example.controller"}, useDefaultFilters = false, includeFilters = {
@ComponentScan.Filter(value = {Autowired.class, Resource.class, Controller.class})
})
@ComponentScan(value = {"com.example.service"}, useDefaultFilters = false, includeFilters = {
@ComponentScan.Filter(value = {Autowired.class, Resource.class, Service.class})
})
//@MapperScan(basePackages = {"com.example.dao"}, properties = {
// "mappers=tk.mybatis.mapper.common.Mapper",
// "notEmpty=false",
// "identity=Mysql",
// "ORDER=BRFORE"
//})
@org.mybatis.spring.annotation.MapperScan(basePackages = {"com.example.dao"})
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
16:13:41.250 [main] DEBUG com.MainApplication - Running with Spring Boot v2.4.0, Spring v5.3.1
16:13:41.252 [main] INFO com.MainApplication - No active profile set, falling back to default profiles: default
16:13:44.225 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
16:13:44.978 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
16:13:45.000 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
16:13:45.001 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
16:13:45.002 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.39]
16:13:45.198 [main] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
16:13:45.199 [main] INFO org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 3819 ms
16:13:46.164 [main] INFO org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
16:13:47.148 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
16:13:47.199 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
16:13:47.222 [main] INFO com.MainApplication - Started MainApplication in 6.899 seconds (JVM running for 19.042)
原因
由于本人在项目中使用了tk.mybatis,
所以MapperScan使用了tk.mybatis.spring.annotation.MapperScan,
而警告是“org.mybatis.spring.mapper.ClassPathMapperScanner”,
将tk.mybatis.spring.annotation.MapperScan 改为 org.mybatis.spring.annotation.MapperScan后,
再次启动服务,警告没有了
总结
本人也查了不少帖子,注解方式和配置文件方式都试了,按照文档也仔细检查过自己的文件,都没有问题,发现这个问题其实有些惭愧,没注意细节。
当然,这个问题或许有其他原因,但是本菜鸟未遇到,不敢乱说,也不敢乱推荐文章,还请见谅。
疑问
将tk.mybatis.spring.annotation.MapperScan 改为 org.mybatis.spring.annotation.MapperScan,虽然警告没了,也能正常使用,但是是否存在隐藏的问题?请大佬赐教,不胜感激!