Skip to content

Commit

Permalink
Minor improvements on multiple result set documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Apr 25, 2013
1 parent 10877db commit 5f0f7e5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 68 deletions.
50 changes: 28 additions & 22 deletions src/site/es/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,13 @@ public class User {
para acceder una sola vez a la base de datos y obtener datos relacionados sin usar una join.</p>

<p>En el ejemplo, el procedimiento almacenado deolverá dos result sets. El primero contendrá
Blogs y el segundo Authors. Se debe proporcionar un nombre a cada resultset informando el atributo
Blogs y el segundo Authors.</p>

<source><![CDATA[SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM AUTHOR WHERE ID = #{id}]]></source>

<p>Se debe proporcionar un nombre a cada resultset informando el atributo
<code>resultSets</code> del mapped statement con una lista de nombres separados por comas.</p>

<source><![CDATA[<select id="selectBlog" resultSets="blogs,authors" resultMap="blogResult">
Expand All @@ -1226,14 +1232,14 @@ public class User {
</p>

<source><![CDATA[<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" javaType="Author" resultSet="authors">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<id property="id" column="id" />
<result property="title" column="title"/>
<association property="author" javaType="Author" resultSet="authors" column="author_id" foreignColumn="id">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="bio" column="bio"/>
</association>
</resultMap>]]></source>

Expand Down Expand Up @@ -1347,29 +1353,29 @@ public class User {
dos resultsets, uno con Blogs y otro con Posts:
</p>

<source><![CDATA[SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM POST WHERE BLOG_ID = #{id}]]></source>

<p>Se debe proporcionar un nombre a cada resultset informando el atributo
<code>resultSets</code> del mapped statement con una lista de nombres separados por comas.</p>

<source><![CDATA[<select id="selectBlog" resultSets="blogs,posts" parameterType="int" resultMap="blogResult">
{call getBlogsAndPosts(#{id,jdbcType=INTEGER,mode=IN})}
</select>]]></source>

<p>Especificamos que la collection "posts" se rellenará con datos contenidos en el resultset llamado "posts":</p>

<source><![CDATA[<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<collection property="posts" ofType="Post" resultSet="posts">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
<id property="id" column="id" />
<result property="title" column="title"/>
<collection property="posts" ofType="Post" resultSet="posts" column="id" foreignColumn="blog_id">
<id property="id" column="id"/>
<result property="subject" column="subject"/>
<result property="body" column="body"/>
</collection>
</resultMap>]]></source>

<p>En este ejempo solo hay un Blog en el primer result set y todos sus Post relacionados en el segundo,
asi que no hay duda de qué Post debe enlazarse con qué Blog. Pero en caso de que la sentencia
devuelva más de un Blog, entonces necesitaremos proporcionar información de cómo emparejar
los padres con los hijos. Para ello se usan los atributos <code>column</code> y <code>foreignColumn</code>:</p>

<source><![CDATA[<collection property="posts" column="id" foreignColum="blog_id" resultSet="posts"/>]]></source>

<p>
<span class="label important">NOTA</span> No hay límite en profundidad, amplitud o combinaciones de las asociaciones y colecciones que mapees. Debes tener en cuenta el rendimiento cuando crees los mapeos. Las pruebas unitarias y de rendimiento de tu aplicación son de gran utilidad para conocer cuales el mejor enfoque para tu aplicación. La parte positiva es que MyBatis te permite cambiar de opinión más tarde, con muy poco (o ningún) cambio en tu código.
</p>
Expand Down
47 changes: 27 additions & 20 deletions src/site/ja/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,13 @@ public class User {

<p>データベースによってはストアドプロシージャから複数の ResultSet を返すことができます。この機能を利用すると、テーブルの結合(Join)を使わず一度の問い合わせで互いに関連する複数のデータを取得することができます。</p>

<p>以下の例では、ストアドプロシージャが2つの ResultSet を返します。1つめの ResultSet には複数の Blog のデータが含まれており、2つめの ResultSet には複数の Author のデータが含まれています。ストアドプロシージャを実行する select 要素の <code>resultSets</code> 属性で、それぞれの ResultSet に名前を付けておく必要があります(カンマ区切り)。ここでは、それぞれ blogs, authors としています。</p>
<p>以下の例では、ストアドプロシージャが2つの ResultSet を返します。1つめの ResultSet には複数の Blog のデータが含まれており、2つめの ResultSet には複数の Author のデータが含まれています。</p>

<source><![CDATA[SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM AUTHOR WHERE ID = #{id}]]></source>

<p>ストアドプロシージャを実行する select 要素の <code>resultSets</code> 属性で、それぞれの ResultSet に名前を付けておく必要があります(カンマ区切り)。ここでは、それぞれ blogs, authors としています。</p>

<source><![CDATA[<select id="selectBlog" resultSets="blogs,authors" resultMap="blogResult">
{call getBlogsAndAuthors(#{id,jdbcType=INTEGER,mode=IN})}
Expand All @@ -1413,14 +1419,14 @@ public class User {
<p>下記の resultMap では、authors 内の各 Author が、それぞれ対応する Blog にマッピングされます。</p>

<source><![CDATA[<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" javaType="Author" resultSet="authors">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<id property="id" column="id" />
<result property="title" column="title"/>
<association property="author" javaType="Author" resultSet="authors" column="author_id" foreignColumn="id">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="bio" column="bio"/>
</association>
</resultMap>]]></source>

Expand Down Expand Up @@ -1551,27 +1557,28 @@ public class User {
association で説明したのと同様に、2つの ResultSet を返すストアドプロシージャを呼び出すことができます。1つ目の ResultSet には Blog のリスト、2つ目の ResultSet には Post のリストが含まれているものとします。
</p>

<source><![CDATA[SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM POST WHERE BLOG_ID = #{id}]]></source>

<p>ストアドプロシージャを実行する select 要素の <code>resultSets</code> 属性で、それぞれの ResultSet に名前を付けておく必要があります(カンマ区切り)。ここでは、それぞれ blogs, posts としています。</p>

<source><![CDATA[<select id="selectBlog" resultSets="blogs,posts" parameterType="int" resultMap="blogResult">
{call getBlogsAndPosts(#{id,jdbcType=INTEGER,mode=IN})}
</select>]]></source>

<p>以下の resultMap では、posts に含まれる Post が、対応する Blog にマッピングされます。</p>

<source><![CDATA[<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<collection property="posts" ofType="Post" resultSet="posts">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
<id property="id" column="id" />
<result property="title" column="title"/>
<collection property="posts" ofType="Post" resultSet="posts" column="id" foreignColumn="blog_id">
<id property="id" column="id"/>
<result property="subject" column="subject"/>
<result property="body" column="body"/>
</collection>
</resultMap>]]></source>

<p>上記の例では最初の ResultSet に含まれている Blog が一件のみで、二番目の ResultSet に含まれている Post は全てその Blog に関連していることを前提としています。複数の Blog が返される可能性がある場合は、<code>column</code>
および <code>foreignColumn</code> 属性を使って、Blog と Post を関連付けるキーを指定する必要があります。</p>

<source><![CDATA[<collection property="posts" column="id" foreignColum="blog_id" resultSet="posts"/>]]></source>

<p>
<span class="important">Note</span> collection と association のマッピングに関して、階層の深さやオブジェクトの大きさに制限はありませんし、両者を組み合わせることも可能です。
ただ、パフォーマンスのことは忘れないようにしてください。
Expand Down
57 changes: 31 additions & 26 deletions src/site/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ public class User {
<source><![CDATA[<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" resultMap="authorResult"/>
<association property="author" resultMap="authorResult" />
</resultMap>
<resultMap id="authorResult" type="Author">
Expand Down Expand Up @@ -1530,8 +1530,14 @@ public class User {
at once and return a resultset per each one. This can be used to hit the database just once
and return related data without using a join.</p>

<p>In the example, the stored procedure will return two result sets. The first will
contain Blogs and the second Authors. A name must be given to each result set by adding a
<p>In the example, the stored procedure executes the following queries and returns two result sets.
The first will contain Blogs and the second Authors.</p>

<source><![CDATA[SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM AUTHOR WHERE ID = #{id}]]></source>

<p>A name must be given to each result set by adding a
<code>resultSets</code> attribute to the mapped statement with a list of names separated by commas.</p>

<source><![CDATA[<select id="selectBlog" resultSets="blogs,authors" resultMap="blogResult">
Expand All @@ -1544,14 +1550,14 @@ public class User {
</p>

<source><![CDATA[<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" javaType="Author" resultSet="authors">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
<id property="id" column="id" />
<result property="title" column="title"/>
<association property="author" javaType="Author" resultSet="authors" column="author_id" foreignColumn="id">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="bio" column="bio"/>
</association>
</resultMap>]]></source>

Expand Down Expand Up @@ -1685,34 +1691,33 @@ public class User {
<h4>Multiple ResultSets for Collection</h4>

<p>
As we did for the association, we can call an stored procedure that returns two result sets, one with Blogs
As we did for the association, we can call an stored procedure that executes two queries and returns two result sets, one with Blogs
and another with Posts:
</p>

<source><![CDATA[SELECT * FROM BLOG WHERE ID = #{id}
SELECT * FROM POST WHERE BLOG_ID = #{id}]]></source>

<p>A name must be given to each result set by adding a
<code>resultSets</code> attribute to the mapped statement with a list of names separated by commas.</p>

<source><![CDATA[<select id="selectBlog" resultSets="blogs,posts" parameterType="int" resultMap="blogResult">
{call getBlogsAndPosts(#{id,jdbcType=INTEGER,mode=IN})}
</select>]]></source>

<p>We specify that the "posts" collection will be filled out of data contained in the result set named "posts":</p>

<source><![CDATA[<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<collection property="posts" ofType="Post" resultSet="posts">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
<id property="id" column="id" />
<result property="title" column="title"/>
<collection property="posts" ofType="Post" resultSet="posts" column="id" foreignColumn="blog_id">
<id property="id" column="id"/>
<result property="subject" column="subject"/>
<result property="body" column="body"/>
</collection>
</resultMap>]]></source>

<p>In this example there will be just one Blog in the first result set and all its related
Posts in the second one, so there is no doubt which Post should be liked with
each Blog. But in case the statement can return more than one Blog, then we need to
provide information about how to match parents with childs. For that purpose the <code>column</code>
and <code>foreignColumn</code> attributes are used:</p>

<source><![CDATA[<collection property="posts" column="id" foreignColum="blog_id" resultSet="posts"/>]]></source>

<p>
<span class="label important">NOTE</span> There's no limit to the depth, breadth or
combinations of the associations and collections that you map.
Expand Down

0 comments on commit 5f0f7e5

Please sign in to comment.