{"id":19939,"date":"2024-03-15T19:39:44","date_gmt":"2024-03-15T19:39:44","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/"},"modified":"2024-03-21T16:53:55","modified_gmt":"2024-03-21T16:53:55","slug":"how-to-configure-one-to-many-query-in-mybatis","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/","title":{"rendered":"How to configure One-to-Many query in MyBatis?"},"content":{"rendered":"<p>In MyBatis, one-to-many queries can be achieved by configuring relationship. Below are the steps to configure one-to-many queries.<\/p>\n<ol>\n<li>In the MyBatis mapping file, first define the query statement for the main table and assign an alias to each field. For example, if the main table is user, the query statement is defined as follows:<\/li>\n<li>Use the following query to get user information, including their ID, username, age, and address, for a specific user ID.<\/li>\n<li>Here, a left join is used to connect the main table with the sub-table, linking them through the id of the main table and the user_id of the sub-table.<\/li>\n<li>In the mapping file, specify the query for the child table and assign an alias to each field. For example, if the child table is &#8220;address,&#8221; the query would be defined as follows:<\/li>\n<li>Retrieve addresses for a specific user by their user ID.<\/li>\n<li>Here, the user_id is used as the query condition to link the primary table&#8217;s id with the secondary table&#8217;s user_id.<\/li>\n<li>In the entity class corresponding to the main table, add a collection property of the entity class corresponding to the subtable to store the results of a one-to-many query. For example, add a List<Address> property to the User class.<\/li>\n<li>Omitting the getter and setter methods, the class User includes fields for id (integer), username (String), age (integer), and a list of addresses.<\/li>\n<li>In the mapping file, establish the association between the main table and the subordinate table using the <collection> tag. Use the <collection> tag in the query statement of the main table to reference the query statement of the subordinate table, and specify the corresponding entity class collection property of the subordinate table using the property attribute. For example:<\/li>\n<li>Retrieve the user&#8217;s id, username, age, and address from the user table, and left join it with the address table using the user id as the key. The query should only return results based on the specified user id. Additionally, map the addresses to the user based on the addressResultMap.<\/li>\n<li>The resultMap property here points to the result mapping of the subordinate table, which can be defined using another <resultMap> tag.<\/li>\n<li>Define the result mapping of the subordinate table in the query statement using the <resultMap> tag. For example:<\/li>\n<li>The &#8220;addressResultMap&#8221; is mapping properties from the Address class to the columns in the database table.<\/li>\n<li>Here, the <result> tag is used to configure each field in the subordinate table to its corresponding entity class property.<\/li>\n<\/ol>\n<p>By configuring the steps above, it is possible to achieve a one-to-many query. When querying the main table, MyBatis will automatically query the data from the secondary table based on the configured relationship, and map the query results to the collection property of the corresponding entity class of the main table.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MyBatis, one-to-many queries can be achieved by configuring relationship. Below are the steps to configure one-to-many queries. In the MyBatis mapping file, first define the query statement for the main table and assign an alias to each field. For example, if the main table is user, the query statement is defined as follows: Use [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-19939","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to configure One-to-Many query in MyBatis? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to configure One-to-Many query in MyBatis?\" \/>\n<meta property=\"og:description\" content=\"In MyBatis, one-to-many queries can be achieved by configuring relationship. Below are the steps to configure one-to-many queries. In the MyBatis mapping file, first define the query statement for the main table and assign an alias to each field. For example, if the main table is user, the query statement is defined as follows: Use [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T19:39:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-21T16:53:55+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin Taylor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Taylor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/\"},\"author\":{\"name\":\"Benjamin Taylor\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\"},\"headline\":\"How to configure One-to-Many query in MyBatis?\",\"datePublished\":\"2024-03-15T19:39:44+00:00\",\"dateModified\":\"2024-03-21T16:53:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/\"},\"wordCount\":470,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/\",\"name\":\"How to configure One-to-Many query in MyBatis? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T19:39:44+00:00\",\"dateModified\":\"2024-03-21T16:53:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to configure One-to-Many query in MyBatis?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\",\"name\":\"Benjamin Taylor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"caption\":\"Benjamin Taylor\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to configure One-to-Many query in MyBatis? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/","og_locale":"en_US","og_type":"article","og_title":"How to configure One-to-Many query in MyBatis?","og_description":"In MyBatis, one-to-many queries can be achieved by configuring relationship. Below are the steps to configure one-to-many queries. In the MyBatis mapping file, first define the query statement for the main table and assign an alias to each field. For example, if the main table is user, the query statement is defined as follows: Use [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T19:39:44+00:00","article_modified_time":"2024-03-21T16:53:55+00:00","author":"Benjamin Taylor","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Benjamin Taylor","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/"},"author":{"name":"Benjamin Taylor","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9"},"headline":"How to configure One-to-Many query in MyBatis?","datePublished":"2024-03-15T19:39:44+00:00","dateModified":"2024-03-21T16:53:55+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/"},"wordCount":470,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/","name":"How to configure One-to-Many query in MyBatis? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T19:39:44+00:00","dateModified":"2024-03-21T16:53:55+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-configure-one-to-many-query-in-mybatis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to configure One-to-Many query in MyBatis?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9","name":"Benjamin Taylor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","caption":"Benjamin Taylor"},"url":"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/19939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=19939"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/19939\/revisions"}],"predecessor-version":[{"id":53708,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/19939\/revisions\/53708"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=19939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=19939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=19939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}