情况1:查看yml文件是否添加mybatis配置
mybatis-plus : mapperLocations : classpath*:mybatis/xml/*Mapper.xml
情况2:区分使用的的版本是mybatis还是mybstis-plus,yml配置也不同
mybatis-plus : mapperLocations : classpath*:mybatis/xml/*Mapper.xml
情况3:调用mybatis(mybatis-plus)的IService的查询方法看是否也报这个错误,如果也报这个错误就是SqlSessionFactory手动注入了,如下
@Bean ( "sqlSessionFactory" ) public SqlSessionFactory sqlSessionFactoryBean ( DynamicDataSource dynamicDataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean ( ) ; sessionFactory. setDataSource ( dynamicDataSource) ; return sessionFactory. getObject ( ) ; }
如上的方法永远不会调用到mybatis的bean,需要修改成如下才行,同时在yml配置的mybatis都失效了,需要在SqlSessionFactory 设置,如设置读取*Mapper.xml的路径
@Bean ( "sqlSessionFactory" ) public SqlSessionFactory sqlSessionFactoryBean ( DynamicDataSource dynamicDataSource) throws Exception { MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean ( ) ; sessionFactory. setDataSource ( dynamicDataSource) ; MybatisConfiguration mybatisConfiguration = new MybatisConfiguration ( ) ; mybatisConfiguration. setLogImpl ( org. apache. ibatis. logging. stdout. StdOutImpl. class ) ; sessionFactory. setConfiguration ( mybatisConfiguration) ; sessionFactory. setMapperLocations ( new PathMatchingResourcePatternResolver ( ) . getResources ( "classpath*:com/example/poi/mapper/**/xml/*Mapper.xml" ) ) ; return sessionFactory. getObject ( ) ; }
情况4:使用mybatis-plus必须导入以下依赖(不要使用spring的mybatis,不然和spring本身数据库管理冲突,还有就是注意使用springboot版本和mybatis版本问题,建议都是使用mybatis-plus,不然使用mybatis可能出现sqlFactory找不到等问题)
< dependency> < groupId> com.baomidou</ groupId> < artifactId> mybatis-plus-boot-starter</ artifactId> < version> 3.5.1</ version> </ dependency>
情况5:打开项目的target目录,观察里面是否有对应的××Mapper.xml文件,若没有,则在pom.xml文件中加入如下配置
< build> < resources> < resource> < directory> src/main/resources</ directory> < includes> < include> **/*.properties</ include> < include> **/*.yml</ include> < include> **/*.xml</ include> </ includes> < filtering> true</ filtering> </ resource> < resource> < directory> src/main/java</ directory> < includes> < include> **/*.properties</ include> < include> **/*.xml</ include> </ includes> < filtering> true</ filtering> </ resource> </ resources>