[Mybatis] 注解操作

  • 查询数据库返回时,将数据库字段与实体类属性进行对应(主要用于多表关联)

@Select("select * from order")
    @Results({
            @Result(id=true,property = "id",column = "id"),   // 设置主键, property = "实体类属性名", column = "数据字段名"
            @Result(property = "orderNum", column = "orderNum"),
         // 一对一关系, 获取productId后, 进行IProductDao.findById查询(该查询的参数就是productId)
            @Result(property = "product", column = "productId", javaType = Product.class, one = @One(select = "dao包名.IProductDao.findById")) ,
           // 一对多关系, 通过中间表进行查询, 此时查询方法的参数是orderId(即"id"), 返回javaType是List类型
            @Result(property = "travellers", column = "id", javaType = java.util.List.class, many = @Many(select = "dao包名.ITravellerDao.findByOrdersId"))
    })
    public List<Orders> findAll() throws Exception;

 

posted @ 2020-04-24 22:01  鱼也  阅读(164)  评论(0)    收藏  举报