问题分析
https://blog.csdn.net/msu9527/article/details/119059103
源码分析
2021-07-21 16:03:35.539 169254213631626854594179000022164 http-nio-8005-exec-4 ERROR c.a.f.c.a.ExceptionAdvice:27 - /user/list
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.*.Mapper.get
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
at org.apache.ibatis.binding.MapperProxy.lambda$cachedMapperMethod$0(MapperProxy.java:61)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:61)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:56)
at com.sun.proxy.$Proxy206.get(Unknown Source)
查看报错代码位置(正常下图 ms 应该是会有值的):
正常mybatis 启动
正常mybatis 启动执行 org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration#sqlSessionFactory
自动加载mapper/*.xml 中的文件
启动时在类SqlSessionFactoryBean.class 的mapperLocations 加断点, 查看加载的xml文件:
注:这里是2.0.2.jar 不同版本位置可能不同,找到org.mybatis.spring.SqlSessionFactoryBean#buildSqlSessionFactory 方法中mapperLocations 即可
执行接口时:正常下图中ms 是不会为空的,启动时所有的xml 像上图被加载到,所以报错有可能是启动时加载不到
org.apache.ibatis.binding.MapperMethod.SqlCommand#SqlCommand
mybatis-plus 启动
执行方法:com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusAutoConfiguration#sqlSessionFactory
由于值this.properties.resolveMapperLocations() 为空,不会进来下面的方法
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
factory.setMapperLocations(this.properties.resolveMapperLocations());
}
然后在下面文件就看不到 mapperLocations 的值
com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean#buildSqlSessionFactory
这里就是我的报错原因,引入了新的依赖导致应用启动时走了mybatis-plus 的类,而并没有配置mapper xml 的地址,所以执行mapper 接口时 报ms找不到,直接把依赖exclusion 掉或者添加mapper配置
mybatis-plus 指定mapper 方法,官方示例
或者yml 配置
mybatis-plus:
mapper-locations: classpath:mapper/**/**Mapper.xml