问题
今天在项目启动类SuperJsonManagerApplication
中,加上这行代码@MapperScan("com.**.mapper")
后,如下所示:
package com.superjson.superjsonmanager;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author zs
* @datetime 2022/8/5 15:16
* @desc superjson框架的启动类
*/
@SpringBootApplication
@MapperScan("com.**.mapper")
public class SuperJsonManagerApplication {
public static void main(String[] args) {
SpringApplication.run(SuperJsonManagerApplication.class, args);
}
}
启动项目时,报出如下错误:
com.superjson.superjsonmanager.SuperJsonManagerApplication
Connected to the target VM, address: '127.0.0.1:58651', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.1)
2022-08-05 16:06:56.891 INFO 138836 --- [ main] c.s.s.SuperJsonManagerApplication : Starting SuperJsonManagerApplication using Java 1.8.0_102 on DESKTOP-UVTEHFR with PID 138836 (D:\project\1myProject\superjson\superjsonmanager\target\classes started by zby in D:\project\1myProject\superjson\superjsonmanager)
2022-08-05 16:06:56.893 INFO 138836 --- [ main] c.s.s.SuperJsonManagerApplication : No active profile set, falling back to 1 default profile: "default"
2022-08-05 16:06:57.322 WARN 138836 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.superjson.superjsonmanager]' package. Please check your configuration.
2022-08-05 16:06:57.561 INFO 138836 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8088 (http)
2022-08-05 16:06:57.566 INFO 138836 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-08-05 16:06:57.567 INFO 138836 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.64]
2022-08-05 16:06:57.651 INFO 138836 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-08-05 16:06:57.651 INFO 138836 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 725 ms
2022-08-05 16:06:57.885 WARN 138836 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'mybatis-org.mybatis.spring.boot.autoconfigure.MybatisProperties': Could not bind properties to 'MybatisProperties' : prefix=mybatis, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'mybatis.configuration' to org.apache.ibatis.session.Configuration
2022-08-05 16:06:57.887 INFO 138836 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2022-08-05 16:06:57.894 INFO 138836 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-08-05 16:06:57.906 ERROR 138836 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'mybatis.configuration' to org.apache.ibatis.session.Configuration:
Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.boot.context.properties.NestedConfigurationProperty org.apache.ibatis.session.Configuration]
Action:
Update your application's configuration
Disconnected from the target VM, address: '127.0.0.1:58651', transport: 'socket'
Process finished with exit code 1
分析问题
定位到上诉问题中的这行语句:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.boot.context.properties.NestedConfigurationProperty org.apache.ibatis.session.Configuration]
正如我们所知道的那样,spring项目在启动时,会加载及初始化配置文件的数据。
spring拿到这些数据后,然后,实例化对应的实体类。如果数据中没有待实例化的实体类所需要的属性信息,那就回出现转换失败的问题。
基于此,可能是配置文件出现了问题,导致spring无法获取对应属性的值。
我的配置文件如下所示:
server:
port: 8088
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
password: root
username: root
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
mybatis:
check-config-location: true
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-aliases-package: com.superjson.superjsonmanager.mapper
mapper-locations: classpath:mapper/*.xml
发现mybatis
下的configuration
字段没有初始化值,因而,便出现了上诉错误。
解决问题
删除configuration
字段即可,如下所示:
mybatis:
check-config-location: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-aliases-package: com.superjson.superjsonmanager.mapper
mapper-locations: classpath:mapper/*.xml
重新启动spring ,发现没有报出错误: