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 初始化失败。