Mybatis 结果声明重用
Mybatis results declaration reuse
以下代码正在工作映射器 class 但它具有结果集声明的复制(在我的情况下这是巨大的)。
我如何重用@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);
}
这是一个常见问题:达到注释限制。使用注释的人经常寻求 "ban" XML(或其他文件类型)配置。
恐怕您不能重复使用注释。然后,您的选择仅限于代码重复或部分使用 XML,至少对于 resultMap 声明,通过 @ResultMap("resultMapId") 引用它们。
(Mybatis) XML 元素被加载到注册表中,而注释(通常)可以看作是方法声明的一部分。
注释旨在绑定到方法:没有要引用的 id,不像 XML.
您可以使用@ResultMap 来reference/reuse 另一个@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();
以下代码正在工作映射器 class 但它具有结果集声明的复制(在我的情况下这是巨大的)。
我如何重用@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);
}
这是一个常见问题:达到注释限制。使用注释的人经常寻求 "ban" XML(或其他文件类型)配置。
恐怕您不能重复使用注释。然后,您的选择仅限于代码重复或部分使用 XML,至少对于 resultMap 声明,通过 @ResultMap("resultMapId") 引用它们。
(Mybatis) XML 元素被加载到注册表中,而注释(通常)可以看作是方法声明的一部分。 注释旨在绑定到方法:没有要引用的 id,不像 XML.
您可以使用@ResultMap 来reference/reuse 另一个@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();