下面的代码是正常工作的mapper类,但是它具有结果集声明的复制内容(在我的情况下这非常大)。
如何重用@Results声明?

@Mapper
public interface DailyMasterCurrentTradeDao {
    @Select("select * from dly_mstr_curr_trd")
    @Results({
        @Result(property = "accAdsStC", column = "ACC_ADS_ST_C"),
        ...
    })
    List<DailyMasterCurrentTrade> selectDailyMasterCurrentTrades();

    @Select("select * from dly_mstr_curr_trd where rownum < #{rownumThreshold}")
    @Results({
        @Result(property = "accAdsStC", column = "ACC_ADS_ST_C"),
        ...
    })
    List<DailyMasterCurrentTrade> selectFewDailyMasterCurrentTrades(long rownumThreshold);
}

最佳答案

您可以使用@ResultMap引用/重用另一个@Results定义。

@Select("SELECT * FROM user where id = ${value}")
@ResultMap("userResult")
User findOne(Long id);

@Select("SELECT * FROM user")
@Results(id = "userResult", value = {
    @Result(property = "id", column = "id", id = true),
    @Result(property = "name", column = "name"),
    @Result(property = "phone", column = "phone")
})
List<User> findAll();

详细信息请参见:https://github.com/mybatis/mybatis-3/issues/155

09-17 18:48