mybatis - 在@One注释上传递多个参数

时间:2017-12-21 07:50:52

标签: mybatis

我正在尝试访问我的辅助数据库中的表,我从主数据库中获取了该表。我的难点是将“DB-Name”作为参数传递给我的辅助查询,(顺便说一句,我正在使用基于MyBatis注释的Mappers)。

这是我的Mapper

@SelectProvider(type = DealerQueryBuilder.class, method = "retrieveDealerListQuery")
@Results({
    @Result(property="dealerID",                column="frm_dealer_master_id"),
    @Result(property="dealerTypeID",            column="frm_dealer_type_id",        one=@One(select="retrieveDealerTypeDAO")),
    @Result(property="dealerName",              column="frm_dealer_name")
})
public List<Dealer> retrieveDealerListDAO(@Param("firmDBName") String firmDBName);

@Select("SELECT * from ${firmDBName}.frm_dealer_type where frm_dealer_type_id=#{frm_dealer_type_id}")
@Results({
    @Result(property="dealerTypeID",            column="frm_dealer_type_id"),
    @Result(property="dealerType",              column="frm_dealer_type")
})
public DealerType retrieveDealerTypeDAO(@Param("firmDBName") String firmDBName, @Param("frm_dealer_type_id") int frm_dealer_type_id);

我拥有的firmDBName是从我的“Primary DB”获得的。

  • 如果我在第二个查询中省略$ {firmDBName},则查询将尝试访问我的主数据库并抛出未找到的表“PrimaryDB.frm_dealer_type”。所以它基本上是在我的主数据库中搜索名为“frm_dealer_type”的表。
  • 如果我尝试重写@Result,就像

    一样

    @Result(property =“dealerTypeID”,column =“firmDBName = firmDBName,frm_dealer_type_id = frm_dealer_type_id”,one = @ One(select =“retrieveDealerTypeDAO”)),

它会抛出一个错误,即“firmDBName”列不存在。

  • 将$ {firmDBName}更改为#{firmDBName}也没有帮助。

我确实参考过这个博客 - here

我想要一个解决方案,将我的参数firmDBName从我的主查询传递到辅助查询。

1 个答案:

答案 0 :(得分:1)

此处的限制是您的列必须由第一个@SELECT返回。 如果您查看测试用例here,您将看到第一个Select返回的parent_xxx值。 您的DealerQueryBuilder必须选择firmDBName作为返回值,并且您的列必须将返回列的名称映射到该值。

您的列定义总是错误的,它应该是: {frm_dealer_type_id=frm_dealer_type_id,firmDBName=firmDBName}或从您的第一个选择中返回的任何内容。

您可以再次参考上面的测试用例以及此处的文档http://www.mybatis.org/mybatis-3/sqlmap-xml.html#Nested_Select_for_Association