0x02一次Mybatis启动失败

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘annualSpendPlanningBaseAgent’ defined in file [/Users/liaozk/project_lightning/aspcenter/aspcenter.service/target/classes/com/bp/agent/AnnualSpendPlanningBaseAgent.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘annualSpendPlanningBaseService’ defined in file [/Users/liaozk/project_lightning/aspcenter/aspcenter.service/target/classes/com/bp/impl/AnnualSpendPlanningBaseServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘annualSpendPlanningBaseMapper’ defined in file [/Users/liaozk/project_lightning/aspcenter/aspcenter.service/target/classes/com/bp/dao/mapper/rds/AnnualSpendPlanningBaseMapper.class]: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory’ threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file [/Users/liaozk/project_lightning/aspcenter/aspcenter.service/target/classes/mapping/rds/AspActionHistoryMapper.xml]’; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is ‘file [/Users/liaozk/project_lightning/aspcenter/aspcenter.service/target/classes/mapping/rds/AspActionHistoryMapper.xml]’. Cause: java.lang.ClassCastException: com.baomidou.mybatisplus.core.MybatisConfiguration$StrictMap$Ambiguity cannot be cast to org.apache.ibatis.mapping.ResultMap at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)


ASP服务,有时候可以正常启动,有时候不能正常启动,即使代码没有任何修改。why?

原因

过去了好几周了,已经忘记当时debug的细节了最后的原因总结如下:

asp服务中有一个mapper中的resultmap的定义使用了嵌套

image-20250817113153288

发布时,这个mapper先被解析,会触发严格模式,严格模式要求整个mybatis注册的resultMap的id必须全局唯一。而在另外两个mapper中刚好定义了id一样的resultmapper,导致校验失败,最终只需要修改resultmap的id即可。

image-20250817113642539

严格模式下,该断点取得对象不一定是ResultMap类型,导致类型转换失败。

image-20250817113820646

附加Q:
1.为什么严格模式下,resultmap有嵌套就需要检查id是否唯一?

A:因为嵌套通过id引用

2.为什么这个检查是针对所有的resultmap 而不是当前命名空间中的?

A:ResultMap 可以被跨命名空间引用

3.可以在当前mapper中引用其他mapper的resultmap

A:可以,association/collection 需要全限定名称

4.mybatisplus的Table相关注解,会自动生成resultmap?resultmap的id如何命名

5.为什么xml解析的顺序不一致?为什么本地固定按照名称排序显示,解析也是相同的排序?

A:ClassLoader.getResources() 不保证顺序,jar 包中文件顺序取决于打包时的文件读取顺序