SpringBoot3.2.0整合MyBatis-plus的相关问题
文章目录
Springboot3.2.0 GA版发布于 2023-11-24
环境:
SpringBoot3.2.0+Gradle8.x+Java17
1. build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
}
group = 'com.yuan'
version = '1.0.0'
java {
sourceCompatibility = '17'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
//....
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.4.1'
}
tasks.named('test') {
outputs.dir snippetsDir
useJUnitPlatform()
}
tasks.named('asciidoctor') {
inputs.dir snippetsDir
dependsOn test
}
hibernate {
enhancement {
enableAssociationManagement = true
}
}
2. mybatis-plus整合问题
1. 错误描述
升级后出现
java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
问题,如下
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.0)
2023-11-24 16:34:46.822 [background-preinit] INFO org.hibernate.validator.internal.util.Version:21 - HV000001: Hibernate Validator 8.0.1.Final
2023-11-24 16:34:46.862 [restartedMain] INFO com.yuan.YuanBoot3AdminApplication:50 - Starting YuanBoot3AdminApplication using Java 17.0.7 with PID 33080 (E:\myGradleProjects\springboot3\yuan-boot3-vue3\yuan-boot3-admin\build\classes\java\main started by jinshengyuan in E:\myGradleProjects\springboot3)
2023-11-24 16:34:46.863 [restartedMain] INFO com.yuan.YuanBoot3AdminApplication:659 - The following 3 profiles are active: "myhome", "api_docs", "flyway"
2023-11-24 16:34:46.899 [restartedMain] INFO o.s.b.d.env.DevToolsPropertyDefaultsPostProcessor:252 - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-11-24 16:34:46.900 [restartedMain] INFO o.s.b.d.env.DevToolsPropertyDefaultsPostProcessor:252 - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-11-24 16:34:47.938 [restartedMain] INFO o.s.d.r.config.RepositoryConfigurationDelegate:292 - Multiple Spring Data modules found, entering strict repository configuration mode
2023-11-24 16:34:47.940 [restartedMain] INFO o.s.d.r.config.RepositoryConfigurationDelegate:139 - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2023-11-24 16:34:47.974 [restartedMain] INFO o.s.d.r.config.RepositoryConfigurationDelegate:208 - Finished Spring Data repository scanning in 24 ms. Found 0 Redis repository interfaces.
2023-11-24 16:34:48.067 [restartedMain] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext:624 - Exception encountered during context initialization - cancelling refresh attempt: java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
2023-11-24 16:34:48.078 [restartedMain] INFO o.s.b.a.logging.ConditionEvaluationReportLogger:82 -
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-11-24 16:34:48.091 [restartedMain] ERROR org.springframework.boot.SpringApplication:839 - Application run failed
java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getTypeForFactoryBeanFromAttributes(FactoryBeanRegistrySupport.java:86)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:838)
at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:620)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:573)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:532)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:138)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:775)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:597)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:753)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:455)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:323)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1331)
at com.yuan.YuanBoot3AdminApplication.main(YuanBoot3AdminApplication.java:24)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50)
Process finished with exit code 0
2. 问题分析及解决
1. 原因分析
经排查发现引入的mybaits-plus版本为
3.5.4.1
,依赖spring与mybatis整合的版本为org.mybatis:mybatis-spring:2.1.1
,所以会导致
Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
的问题
2. 解决方式
有两种解决方式
- 升级Mybatisplus的版本到最高版本,如
implementation 'com.baomidou:mybatis-plus-generator:3.5.5'
- 还是用
3.5.4.1
版本,需要在build.gradle
中引入mybatis-spring
的高版本,如下mybatis-spring:3.0.3
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
}
group = 'com.yuan'
version = '1.0.0'
java {
sourceCompatibility = '17'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
//....
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.4.1'
// https://mvnrepository.com/artifact/org.mybatis/mybatis-spring
//解决MyBaits-Plus依赖中spring-mybatis:2.1.1版本报`Invalid value type for attribute 'factoryBeanObjectType': java.lang.String`的问题
implementation 'org.mybatis:mybatis-spring:3.0.3'
}
tasks.named('test') {
outputs.dir snippetsDir
useJUnitPlatform()
}
tasks.named('asciidoctor') {
inputs.dir snippetsDir
dependsOn test
}
hibernate {
enhancement {
enableAssociationManagement = true
}
}