在前一段时间遇到了一个异常: BindingException ,无效的绑定语句。
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.practicecode.wxy.mybatis.service.TbCustomerService.getBaseMapper
我已经在启动类中添加了 @MapperScan 注解,在找了很长时间的原因与测试下发现,mapper层调用API都是好使的,只有service接口调用的方法都报异常,后来才发现该注解下的包路径没有写对:
// 错误示范
@MapperScan("com.example.practicecode.wxy.mybatis")
// 正确的写法
@MapperScan("com.example.practicecode.wxy.mybatis.mapper")
笔者当时误认为mapper层包括在mybatis包下直接写就可以,没想到犯了这么低级的错误。
除却上面的错误方式还有一种也会造成上面的绑定异常,当 src/main/java 此路径下的mapper包与 src/main/resources 此路径下的mapper.xml路径或名称不一致时也会返回异常信息。
小提示: 顺便记录一下近日遇到的一个异常:
代码如下:
@SpringBootTest
@RequiredArgsConstructor
class PracticeCodeApplicationTests {
private final StudentMapper studentMapper;
@Test
public void myTest(){
Student student = studentMapper.selectStudentById(1);
System.out.println(student);
}
}
异常如下:
org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [final com.example.practicecode.wxy.mybatis.mapper.StudentMapper studentMapper] in constructor [public com.example.practicecode.PracticeCodeApplicationTests(com.example.practicecode.wxy.mybatis.mapper.StudentMapper)].
此异常是因为我在测试类中使用了@RequiredArgsConstructor这个注解,去掉使用 @Autowired 即可。