Dao层的bean注入失败,无法解析映射文件的棘手问题

nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'xxxConfigErrOfflineDao' defined in file
[F:\work\xxx\dao\xxxConfigErrOfflineDao.class]: Cannot resolve reference to bean 'sqlSessionTemplate' while setting bean property 'sqlSessionTemplate';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionTemplate'
defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0;
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

问题描述

从错误日志来看,问题仍然集中在 sqlSessionFactory 创建失败,原因是 MyBatis 无法解析映射文件 xxxConfigOfflineReasonDao.xml。
经过各种排查并未发现问题源,最后反应过来可能是实体的问题,因为使用MyBatis-Plus。

问题原因:

实体中:

@TableId(value = "UUID" )
private String uuid;

@TableId(value = "RULE_ID" )
private String ruleId; 

TableId注解粘了两次,这会导致 MyBatis-Plus 无法正确识别主键字段,从而引发一系列异常。

原因分析

@TableId 的作用:

@TableId 是 MyBatis-Plus 提供的注解,用于标识实体类中的主键字段。

默认情况下,一个实体类只能有一个主键字段。如果存在多个 @TableId,MyBatis-Plus 无法确定哪个字段是真正的主键。

MyBatis-Plus 的主键策略:

MyBatis-Plus 依赖于主键字段来生成 SQL 语句(如 INSERT、UPDATE、SELECT 等)。

如果存在多个 @TableId,MyBatis-Plus 会抛出异常,因为它无法确定哪个字段是主键。

具体错误场景:

在你的代码中,uuid 和 ruleId 都被标记为 @TableId,这会导致 MyBatis-Plus 在解析实体类时抛出异常。

异常可能表现为:

UnsatisfiedDependencyException(依赖注入失败)。

BeanCreationException(Bean 创建失败)。

SqlSessionFactory 初始化失败。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容