问题一 :调用MybatisPuls本身方法提示,绑定无效或不存在
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pttp.dao.SysUserMapper.selectById
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:229) ~[mybatis-3.5.13.jar:3.5.13]
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.13.jar:3.5.13]
at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:96) ~[mybatis-3.5.13.jar:3.5.13]
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
at org.apache.ibatis.util.MapUtil.computeIfAbsent(MapUtil.java:36) ~[mybatis-3.5.13.jar:3.5.13]
原因:重复依赖导致
场景:创建项目的时候,选择着了Mybatis-puls的依赖,又再次引入了官方的依赖
问题二:sqlSessionFactory出问题了
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
at org.springframework.util.Assert.notNull(Assert.java:204) ~[spring-core-6.0.9.jar:6.0.9]
at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:122) ~[mybatis-spring-2.0.7.jar:2.0.7]
at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:73) ~[mybatis-spring-2.0.7.jar:2.0.7]
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) ~[spring-tx-6.0.9.jar:6.0.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1816) ~[spring-beans-6.0.9.jar:6.0.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766) ~[spring-beans-6.0.9.jar:6.0.9]
... 44 common frames omitted
解决:使用Mybatis就不要用SqlSessionTemplate,改为Mybatis的 MybatisSqlSessionFactory
配置类如下
@Configuration
public class MybatisPlusConfig {
private String mapperLocations = "classpath*:/mapper/*.xml";
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource)
throws Exception {
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
return sessionFactory.getObject();
}
}
问题三:Spring-core 找不到 NestedIOException
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/NestedIOException
at com.pttp.config.MybatisPlusConfig.sqlSessionFactory(MybatisPlusConfig.java:29) ~[classes/:na]
at com.pttp.config.MybatisPlusConfig$$SpringCGLIB$$0.CGLIB$sqlSessionFactory$0(<generated>) ~[classes/:na]
at com.pttp.config.MybatisPlusConfig$$SpringCGLIB$$2.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258) ~[spring-core-6.0.9.jar:6.0.9]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-6.0.9.jar:6.0.9]
at com.pttp.config.MybatisPlusConfig$$SpringCGLIB$$0.sqlSessionFactory(<generated>) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ~[spring-beans-6.0.9.jar:6.0.9]
... 59 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.core.NestedIOException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
... 70 common frames omitted
原因: SpringBoot 3 已经把这个类删掉了,所以MyBatis 找不到这个类
解决:升级Myabtis 版本
错误的版本 (SpringBoot 3 不要使用这个版本)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
改为
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>