Mybatis结果声明重用
问题描述:
以下代码正在工作映射程序类,但它具有结果集声明的副本(这对我来说是巨大的)。
我如何重用@Results声明?Mybatis结果声明重用
@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);
}
答
这是常见的问题:达到注释限制。使用注释的人经常试图“禁止”XML(或其他文件类型)配置。
我担心你不能重复使用注释。然后,您的选择仅限于代码复制或部分使用XML,至少适用于声明,通过@ResultMap(“resultMapId”)引用它们。
(Mybatis)将XML元素加载到注册表中,而注释(通常)可以看作是方法声明的一部分。 注解被设计为绑定到方法:与XML不同,没有id引用。
答
您可以使用@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();