MybatisPlus自帶方法報錯BindingException: Invalid bound statement (not found)

java从精通到入门發表於2024-08-05

問題描述:
MybatisPlus自帶方法如 xxx.list() xxx.save() 或則xxMapper.selectList()...
等方法無法使用報錯:Invalid bound statement (not found)
但是自己定義的sql方法可以使用
問題排查:
1、自定義方法可使用 排除xml位置不正確,包掃描沒掃到(target目錄下已生成對應的mapper和xml檔案)
2、檢查mybatis-plus.mapper-locations=classpath:mapper/*/*.xml 配置沒問題
3、檢查自定義配置程式碼:

@Bean(name = "mysqlSqlSessionFactory")
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*.xml"));
return bean.getObject();
}

找到問題:SqlSessionFactory是MyBatis的類,MybatisPlus應該使用MybatisSqlSessionFactoryBean

問題修復:

@Bean(name = "mysqlSqlSessionFactory")
public MybatisSqlSessionFactoryBean mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*.xml"));
return bean;
}

未經允許 禁止轉載

相關文章