简体   繁体   English

将 MyBatis ResultMap 中的非列参数传递给嵌套选择

[英]Passing a non-column parameter in a MyBatis ResultMap to a nested select

I've a One-to-Many relationship in my domain model where I basically want to read Foos and a filtered set of Bars with one MyBatis select statement using a nested select for the Bars .我在我的域模型中有一个一对多的关系,我基本上想读取Foos和一组过滤的Bars和一个 MyBatis select 语句,使用Bars的嵌套选择。

To explain: My domain model classes look more or less like this (the real domain model is more complex of course, but my problem boils down to this):解释一下:我的领域模型类看起来或多或少是这样的(真正的领域模型当然更复杂,但我的问题归结为这个):

public class Foo {
   private String name;
   private Set<Bar> bars;
   // getters and setters omitted
}

public class Bar {
   private String color;
   // getters and setters omitted
}

Now I want to read Foos with a certain name with Bars of a certain color:现在我想用特定颜色的Bars读取具有特定名称的Foos

public interface FooRepository {
  public List<Foo> selectFoosWithBars(String name, String color);
}

The relevant parts of my MyBatis XML Mapper files look like:我的 MyBatis XML Mapper 文件的相关部分如下所示:

<select id="selectFoosWithBars" resultMap="fooResult">
   SELECT f.id f_id, f.name f_name FROM foos f WHERE f.name = #{name}
</select>

<select id="selectBars" resultMap="barResult">
   SELECT b.color b_color FROM bars b
   JOIN foos f ON (b.f_id = #{id})
   WHERE b.color = #{color}
</select>

<resultMap id="fooResult" type="Foo">
   <result property="name" column="f_name">
   <collection property="bars" select="selectBars" column="f_id" />
</resultMap>

<resultMap id="barResult" type="Bar">
   <result property="color" column="b_color" />
</resultMap>

All fine, except the #{color} parameter within the selectBars SELECT.一切正常,除了selectBars SELECT 中的#{color}参数。 I can use the color parameter within the first selectFoosWithBars without any problem, but how can I pass the parameter to the nested selectBars ?我可以在第一个selectFoosWithBars使用 color 参数而没有任何问题,但是如何将参数传递给嵌套的selectBars

Note, that I'm currently trying to performance tune the SQL and simply joining the bars and foos tables in the first SELECT is unfortunately not an option.请注意,我目前正在尝试对 SQL 进行性能调整,但不幸的是,在第一个 SELECT 中简单地加入barsfoos表并不是一种选择。

This may be achieved by using a trick with an artificial column in the main query and configure column parameter appropriately.这可以通过在主查询中使用带有人工列的技巧并适当配置column参数来实现。

Here is relevant part of the column attribute documentation :这是列属性文档的相关部分:

The column name from the database, or the aliased column label that holds the value that will be passed to the nested statement as an input parameter.数据库中的列名,或保存将作为输入参数传递给嵌套语句的值的别名列标签。

Add artificial column with the color value to the main query:将具有颜色值的人工列添加到主查询:

<select id="selectFoosWithBars" resultMap="fooResult">
   SELECT f.id f_id, f.name f_name, #{color} f_color
   FROM foos f WHERE f.name = #{name}
</select>

And then use f_color column to pass parameter to selectBars :然后使用f_color列将参数传递给selectBars

<select id="selectBars" resultMap="barResult">
   SELECT b.color b_color FROM bars b
   JOIN foos f ON (b.f_id = #{id})
   WHERE b.color = #{color}
</select>

<resultMap id="fooResult" type="Foo">
   <result property="name" column="f_name">
   <collection property="bars" select="selectBars" column="{id=f_id,color=f_color}" />
</resultMap>

If I'm reading this correctly then the same solution that I discuss here:如果我正确阅读了这篇文章,那么我在这里讨论的解决方案相同:

how to pass a constant value to nested column with mybaits association 如何将常量值传递给具有 mybaits 关联的嵌套列

Should work.应该管用。 You pass it in as a column attribute by selecting it from the selectFoosWithBars select.您可以通过从selectFoosWithBars选择selectFoosWithBars其作为列属性selectFoosWithBars

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM