【问题标题】:MyBatis mapping @One annotation issueMyBatis 映射@One 注解问题
【发布时间】:2017-10-03 12:39:58
【问题描述】:

我使用了myBatis,在应用启动过程中出现错误:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for pl.net.manager.dao.ProductDAO.getService()
### The error may exist in pl/net/manager/dao/ProductDAO.java (best guess)
### The error may involve pl.net.manager.dao.ProductDAO.getProduct
### The error occurred while handling results
### SQL: select * from product where id=?
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for pl.net.manager.dao.ProductDAO.getService()

我有如下表格:产品、服务、费率计划

我有三个 DAO:

ProductDAO:

@Mapper
public interface ProductDAO extends ServiceDAO, RateplanDAO {

    @Select("select * from product")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "productStatus", column = "product_status"),
            @Result(property = "createdBy", column = "created_by"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "modifiedBy", column = "modified_by"),
            @Result(property = "modifiedDate", column = "modified_date"),
            @Result(property = "service", column = "service", one = @One(select = "getService()")),
            @Result(property = "rateplan", column = "rateplan", one = @One(select = "getRateplan()"))
    })
    public List<Product> getProductList();

    @Select("select * from product where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "productStatus", column = "product_status"),
            @Result(property = "createdBy", column = "created_by"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "modifiedBy", column = "modified_by"),
            @Result(property = "modifiedDate", column = "modified_date"),
            @Result(property = "service", column = "service", one = @One(select = "getService()")),
            @Result(property = "rateplan", column = "rateplan", one = @One(select = "getRateplan()"))
    })
    public Product getProduct(Integer id);

ServiceDAO:

@Mapper
public interface ServiceDAO {

    @Select("select * from service where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "serviceStatus", column = "service_status"),
            @Result(property = "name", column = "name"),
            @Result(property = "createdBy", column = "created_by"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "modifiedBy", column = "modified_by"),
            @Result(property = "modifiedDate", column = "modified_date")
    })
    public Service getService(Integer id);

RateplanDAO:

@Mapper
public interface RateplanDAO {

    @Select("select * from rateplan where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "rateplanStatus", column = "rateplan_status"),
            @Result(property = "createdBy", column = "created_by"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "modifiedBy", column = "modified_by"),
            @Result(property = "modifiedDate", column = "modified_date")
    })
    public Rateplan getRateplan(Integer id);

我已经为产品编写了简单的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MainApplication.class)
@SpringBootTest
public class ProductDaoTest {

    @Autowired
    ProductDAO productDAO;

    @Test
    public void getAllProductsTest(){
        List<Product> list = productDAO.getProductList();
        assertNotNull(list);
        assertEquals(2, list.size());
    }

    @Test
    public void getSpecifiedProductTest(){
        Integer id =1;
        Product product = productDAO.getProduct(id);

        assertEquals(id, product.getId());

    }
}

如果我理解正确,问题出在映射上,但根据文档,它应该可以正常工作。您有什么想法,如何解决?

【问题讨论】:

    标签: java junit mybatis dao


    【解决方案1】:

    你需要删除@Result中方法名后面的括号:

    @Result(property = "service", column = "service", one = @One(select = "getService()"))
    

    =>

    @Result(property = "service", column = "service", one = @One(select = "getService"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-25
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 2015-06-19
      相关资源
      最近更新 更多