Mybatis:IllegalArgumentException:映射的语句集合不包含xxx的值

时间:2019-04-27 05:18:35

标签: spring-boot mybatis

我有两个具有Vendor关系的实体Goodsone-to-many,该关系看起来像:

enter image description here

我正在将mybatis与注释器,映射器一起使用:

GoodsMapper

public interface GoodsMapper {
    @Select("select * from goods where id=#{goodsId}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "vendor_id", property = "vendor", 
                 one = @One(select = "com.xxx.server.mapper.VendorMapper.getVendor"))
    })
    Goods getGoods(@Param("goodsId") String goodsId);
}

VendorMapper

public interface VendorMapper {

    @Select("select * from vendor where id=#{vendorId}")
    Vendor getVendor(@Param("vendorId") String vendorId);

}

忽略实体代码及其他...

当我调用goodsMapper.getGoods(goodsId)时,发现了以下异常:

Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.xxx.server.mapper.VendorMapper.getVendor
### The error may exist in com/xxx/server/mapper/GoodsMapper.java (best guess)
### The error may involve com.xxx.server.mapper.GoodsMapper.getGoods
### The error occurred while handling results
### SQL: select * from goods where id=?
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.xxx.server.mapper.VendorMapper.getVendor
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
    ... 117 more
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.xxx.server.mapper.VendorMapper.getVendor
    at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:933)
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:726)
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:719)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getNestedQueryMappingValue(DefaultResultSetHandler.java:740)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getPropertyMappingValue(DefaultResultSetHandler.java:465)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyPropertyMappings(DefaultResultSetHandler.java:441)

我已经检查了com.xxx.server.mapper.VendorMapper.getVendor的{​​{1}}的类路径select,这是正确的。

感谢任何帮助〜

1 个答案:

答案 0 :(得分:1)

在我的情况下,这是由于Spring还没有初始化引用的集合引起的。

解决方案是在@parent映射器中添加@DependsOn注释。

@DependsOn("VendorMapper")
public interface GoodsMapper{
...
}

@Repository("VendorMapper")
public interface VendorMapper {
...
}