在 Spring Boot @Configuration 注解类中使用 Mybatis 查询数据库启动报错
2017-11-01 15:17
706 查看
场景
想在 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;
相关文章推荐
- SpringCloud SpringBoot mybatis 分布式微服务(十六)Spring Boot中使用Flyway来管理数据库版本
- Spring mvc + mybatis web项目当系统启动时运行某方法查询数据库内容
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十一)使用Redis数据库(1)
- 使用Spring Boot +JPA+mybatis快速启动一个web项目
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十二)使用Redis数据库(2)
- SpringBoot中使用MyBatis,使用PageHelper进行分页查询以及碰到的错误
- SpringCloud SpringBoot mybatis 分布式微服务(八)Spring Boot中使用JdbcTemplate访问数据库
- Spring Boot干货系列:(九)数据存储篇-SQL关系型数据库之MyBatis的使用
- 使用Spring Boot+MyBatis框架做查询操作
- Spring Boot通过Mybatis,使用mapper接口和xml配置sql,连接数据库
- springboot+springmvc+mybatis 使用注解对数据库的增、改和查操作
- Spring-Boot整合mybatis(一),使用默认的数据库连接池
- Spring Boot干货系列:(九)数据存储篇-SQL关系型数据库之MyBatis的使用
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
- SpringCloud SpringBoot mybatis 分布式微服务(十二)Spring Boot中使用Redis数据库
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十八)使用Flyway来管理数据库版本
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十六)使用JdbcTemplate操作数据库
- SpringBoot使用Mybatis注解进行一对多和多对多查询
- 启动springBoot,使用h2内嵌数据库,修改hibernate方言
- Spring Boot快速入门(五):使用MyBatis(注解形式)进行数据库操作