为不同的列名重用resultMap

时间:2021-07-11 21:40:42

Is there a way of reusing the same resultMap multiple times in a single query.

有没有办法在单个查询中多次重复使用相同的resultMap。

For example, suppose I have a "foo" resultMap:

例如,假设我有一个“foo”resultMap:

<resultMap id="foo" class="Foo">
  <result property="Bar" column="bar" />
</resultMap>

Is there a way to define another resultMap that reuses the above for different columns? Something like...

有没有办法定义另一个对不同列重用上述的resultMap?就像是...

<resultMap id="fizz"class="Fizz">
  <result property="Foo1" column="bar=bar1" resultMapping="foo" />
  <result property="Foo2" column="bar=bar2" resultMapping="foo" />
  <result property="Foo3" column="bar=bar3" resultMapping="foo" />
</resultMap>

2 个解决方案

#1


2  

Almost. If you select the ID of the Foo in your query, you can have the Fizz result map execute a SELECT for that ID, which will use the Foo result map.

几乎。如果在查询中选择Foo的ID,则可以让Fizz结果映射为该ID执行SELECT,这将使用Foo结果映射。

<result property="Foo1" column="bar1Id" select="selectFoo"/>

(Assuming you have a selectFoo query defined.) But that's extremely slow with large result sets, since it does an additional SELECT for every row.

(假设你定义了一个selectFoo查询。)但是对于大型结果集来说这是非常慢的,因为它为每一行做了额外的SELECT。

iBATIS has a solution to this problem for the typical case, where you have a composite object that contains various other objects. First, you define a query that joins your tables, then you can use fooMap to populate a Foo:

对于典型情况,iBATIS有一个解决此问题的方法,在这种情况下,您有一个包含各种其他对象的复合对象。首先,定义一个连接表的查询,然后可以使用fooMap填充Foo:

<result property="Foo1" resultMap="fooMap"/>

But you can't use that result map twice for two different Foos because the result map specifies certain column names. You can use another technique, though:

但是,您不能对两个不同的Foos使用该结果映射两次,因为结果映射指定了某些列名称。但是,您可以使用其他技术:

<result property="foo1.bar" column="foo1bar"/>
<result property="foo2.bar" column="foo2bar"/>

More detail in page 35 of the iBatis Datamapper manual.

有关iBatis Datamapper手册第35页的更多详细信息。

#2


1  

you could use resultmaps, which extend another resultmap e.g.

你可以使用结果图,它扩展了另一个结果图,例如

<resultMap id="document" class="Document"> 
  <result property="Id" column="Document_ID"/>
  <result property="Title" column="Document_Title"/>
  <discriminator column="Document_Type" type="string"/>
  <subMap value="Book" resultMapping="book"/>
  <subMap value="Newspaper" resultMapping="newspaper"/>
</resultMap>

<resultMap id="book" class="Book" extends="document"> 
  <property="PageNumber" column="Document_PageNumber"/>
</resultMap>

more info: http://ibatis.apache.org/docs/dotnet/datamapper/ch03s05.html

更多信息:http://ibatis.apache.org/docs/dotnet/datamapper/ch03s05.html

#1


2  

Almost. If you select the ID of the Foo in your query, you can have the Fizz result map execute a SELECT for that ID, which will use the Foo result map.

几乎。如果在查询中选择Foo的ID,则可以让Fizz结果映射为该ID执行SELECT,这将使用Foo结果映射。

<result property="Foo1" column="bar1Id" select="selectFoo"/>

(Assuming you have a selectFoo query defined.) But that's extremely slow with large result sets, since it does an additional SELECT for every row.

(假设你定义了一个selectFoo查询。)但是对于大型结果集来说这是非常慢的,因为它为每一行做了额外的SELECT。

iBATIS has a solution to this problem for the typical case, where you have a composite object that contains various other objects. First, you define a query that joins your tables, then you can use fooMap to populate a Foo:

对于典型情况,iBATIS有一个解决此问题的方法,在这种情况下,您有一个包含各种其他对象的复合对象。首先,定义一个连接表的查询,然后可以使用fooMap填充Foo:

<result property="Foo1" resultMap="fooMap"/>

But you can't use that result map twice for two different Foos because the result map specifies certain column names. You can use another technique, though:

但是,您不能对两个不同的Foos使用该结果映射两次,因为结果映射指定了某些列名称。但是,您可以使用其他技术:

<result property="foo1.bar" column="foo1bar"/>
<result property="foo2.bar" column="foo2bar"/>

More detail in page 35 of the iBatis Datamapper manual.

有关iBatis Datamapper手册第35页的更多详细信息。

#2


1  

you could use resultmaps, which extend another resultmap e.g.

你可以使用结果图,它扩展了另一个结果图,例如

<resultMap id="document" class="Document"> 
  <result property="Id" column="Document_ID"/>
  <result property="Title" column="Document_Title"/>
  <discriminator column="Document_Type" type="string"/>
  <subMap value="Book" resultMapping="book"/>
  <subMap value="Newspaper" resultMapping="newspaper"/>
</resultMap>

<resultMap id="book" class="Book" extends="document"> 
  <property="PageNumber" column="Document_PageNumber"/>
</resultMap>

more info: http://ibatis.apache.org/docs/dotnet/datamapper/ch03s05.html

更多信息:http://ibatis.apache.org/docs/dotnet/datamapper/ch03s05.html