在 Spring Boot @Configuration 注解类中使用 Mybatis 查询数据库启动报错

场景

想在 Spring Boot 启动时使用 guava 缓存,将数据库的部分内容放入缓存中,所以写了一个 CacheConfig 类,但是在启动过程中报错,错误为:

@Configuration
public class CacheConfig {

}
Mapped Statements collection does not contain value for com.xxx.xxx.dao.ScoreDao.selectById

类似情况为:一个 person 关联了一个 score,拥有字段 scoreId(外键),并且在Person 类中有一个成员 Score score,使用 Mybatis 在查询 person 时一并将 score 查询出来。这段程序在之前已经运行测试成功。
Dao 中方法为:
PersonDao

@Select("select " + FIELDS + " from person")
@ResultMap("PersonMapper.PersonResult")
public List<Person> selectAll();

ScoreDao

@Select("select " + FIELDS + " from score where sid = #{sid}")
Score selectById(long sid);

Person ResultMap 在 xml 文件中配置为

<mapper namespace="SellerRelationMapper">  
    <resultMap type="SellerRelation" id="SellerRelationResult">  
        <id property="sid" column="sid"/>  
        <result property="scoreId" column="score_id"/>  
        <association property="score" column="score_id" select="com.xxx.xxx.dao.ScoreDao.selectById"/>  
    </resultMap>  
</mapper>

原因

在执行 CacheConfig 类中方法时,ScoreDao 还没有被注入,所以无法被找到。

解决方法

原本想是否能控制依赖注入的顺序,让 CacheConfig 在 ScoreDao 被注入后再执行,但是没有查询到方案,于是只能在 CacheConfig 类或被 CacheConfig 类依赖的 Service 添加如下代码

@Autowired
private ScoreDao ScoreDao;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值