学无先后,达者为师

网站首页 编程语言 正文

mybatis(mybatis-plus)报invalid bound statement (not found)或者找不到xml文件(各种情况)

作者:小徐敲java 更新时间: 2024-04-05 编程语言

情况1:xml文件不在resource目录下的必须使用绝对路径

mybatis-plus:
  # Mapper.xml 文件位置 Maven 多模块项目的扫描路径需以 classpath*: 开头
  # 实现接口绑定
  mapper-locations: classpath*:com/example/clickhouse/mapper/**/xml/*Mapper.xml

情况2:查看yml文件是否添加mybatis配置(xml文件在resource目录下)

mybatis-plus:
  # Mapper.xml 文件位置 Maven 多模块项目的扫描路径需以 classpath*: 开头
  # 实现接口绑定
  mapper-locations: classpath*:mybatis/xml/*Mapper.xml

情况3:区分使用的的版本是mybatis还是mybstis-plus,yml配置也不同(xml文件在resource目录下)

#mybatis:
mybatis-plus:
  # Mapper.xml 文件位置 Maven 多模块项目的扫描路径需以 classpath*: 开头
  # 实现接口绑定
  mapper-locations: classpath*:mybatis/xml/*Mapper.xml

情况4:调用mybatis(mybatis-plus)的IService的查询方法看是否也报这个错误,如果也报这个错误就是SqlSessionFactory手动注入了,如下

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(DynamicDataSource dynamicDataSource) throws Exception {

        //MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dynamicDataSource);
        //sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/example/poi/mapper/**/xml/*Mapper.xml"));
        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);
        /// 创建Configuration对象
        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();
    }

情况5:使用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>

情况6:打开项目的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>

原文链接:https://blog.csdn.net/qq_19891197/article/details/134550106

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新