mybatis报错Invalid bound statement (not found)

报错:

1.Invalid bound statement (not found) 错误

比较全面的解答:
参考
https://www.cnblogs.com/mmzs/p/11759456.html
其中有一点可能不太注意,xml标签的resultMap和resultType属性,注意区分!

我额外遇到的问题
没有绑定xml文件路径。
虽然在application.yml文件中配置了

# MyBatis 配置
mybatis:
mapper-locations: classpath:mapper/*.xml

但是后来为了完成批处理功能,学习了几篇文章,总结后决定以java配置类方式全局配置Mybatis实现SqlSessionFactory的依赖注入。
(另一种方式是全局mybatis-config.xml配置文件,然后在java中读取此xml配置文件进行配置–实际上这种方式需要将之前在application.yml中指定的数据源、xml路径重新以xml方式再配置一遍。)

于是写了MybatisConfig.java配置类,然后其他地方需要时直接如下使用,很方便。

@Autowired
private SqlSessionFactory sqlSessionFactory;

然而,在配置类中,生成SqlSessionFactory的Bean时,需要单独对其配置数据源、xml文件路径等,自己总结时只set了数据源相关属性。

@Configuration
@MapperScan("com.example.rbac.mapper")
public class MybatisConfig {

    @Autowired
    private Environment env;

    @Bean
    SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

		// 设置数据源
        PooledDataSource dataSource = new PooledDataSource();
        dataSource.setDriver(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        factoryBean.setDataSource(dataSource);

		// !!!!!!!!!!!!!!!!!!!!不要忘了这里!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // 设置mapper 对应的XML 文件的路径
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		factoryBean.setMapperLocations(resolver.getResources(env.getProperty("mybatis.mapper-locations")));

        // 设置mapper 接口所在的包
        factoryBean.setTypeAliasesPackage("com.example.rbac.mapper");

        return factoryBean.getObject();
    }

    @Bean
    SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

总之,为了自己实现SqlSessionFactory的依赖注入,写Mybatis配置类的时候,无论哪种实现方式,都需要完整的再重新定义一遍。原来在application.yml文件里的配置不会生效了。
如果没有s.setMapperLocations(...),那么还是会报Invalid bound statement(not found)即XxMapper.xml没有与java接口映射起来。

2.其他报错

resultMap和resultType属性别写错了。注意区分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值