About the use of collection in mapper file Resultmap in MyBatis

Source: Internet
Author: User

There are two types of Resultmap and select for collection, and you must manually specify a

1. Entity classes:

1  PackageCom.mrlu.mybatis.domain;2 3 Importjava.util.List;4 5 /**6 * Created by Stefan on 15-12-31.7  */8  Public classUser {9     PrivateInteger ID;Ten     PrivateString name; One     PrivateList<account>accounts; A  -      PublicInteger getId () { -         returnID; the     } -  -      Public voidsetId (Integer id) { -          This. ID =ID; +     } -  +      PublicString GetName () { A         returnname; at     } -  -      Public voidsetName (String name) { -          This. Name =name; -     } -  in @Override -      PublicString toString () { to         return"User{" + +"id=" + ID + -", Name= ' + name + ' + ' + the", accounts=" + accounts + *‘}‘; $     }Panax Notoginseng  -      PublicList<account>getaccounts () { the         returnaccounts; +     } A  the      Public voidSetaccounts (list<account>accounts) { +          This. accounts =accounts; -     } $  $}
 PackageCom.mrlu.mybatis.domain;/*** Created by Stefan on 15-12-31.*/ Public classAccount {PrivateInteger ID; PrivateInteger userId; PrivateString num;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicInteger getUserId () {returnuserId; }     Public voidSetuserid (Integer userId) { This. UserId =userId; }     PublicString Getnum () {returnnum; }     Public voidsetnum (String num) { This. num =num; } @Override PublicString toString () {return"account{" + "id=" + ID + ", userid=" + UserId + ", num= '" + num + "+ ‘}‘; }}

2. DAO:

 package   Com.mrlu.mybatis.dao;  import   Com.mrlu.mybatis.domain.User;  import   java.util.List;  /**   * Created by Stefan on 15-12-31.  */ public  interface   Userdao { void   insert (user user);  public  list<user> SelectAll ();}  
 Package Com.mrlu.mybatis.dao; Import Com.mrlu.mybatis.domain.Account; /**  */Publicinterface  Accountdao    {publicvoid  Insert (account account);}

3. Mapper.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >< Mapper namespace= "Com.mrlu.mybatis.dao.AccountDao" >       <insert id= "Insert" usegeneratedkeys= "true" keyproperty= "id" > INSERT into account              (user_id, num) VALUES (                #{userid}, #{num}              )       </ Insert></mapper>

Resultmap:

1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >3<mapper namespace= "Com.mrlu.mybatis.dao.UserDao" >4<resultmap id= "Baseresultmap" type= "Com.mrlu.mybatis.domain.User" >5<result column= "id" property= "id"/>6<result column= "name" property= "name"/>7<collection property= "Accounts" oftype= "Com.mrlu.mybatis.domain.Account" javatype= "Java.util.ArrayList" resultmap= "Accountmap" >8<result column= "id" property= "id"/>9<result column= "user_id" property= "UserId"/>Ten<result column= "num" property= "num"/> One</collection> A</resultMap> -<resultmap id= "Accountmap" type= "Com.mrlu.mybatis.domain.Account" > -<result column= "id" property= "id"/> the<result column= "user_id" property= "UserId"/> -<result column= "num" property= "num"/> -</resultMap> -<insert id= "Insert" keyproperty= "id" usegeneratedkeys= "true" > + INSERT into User (name) VALUES ( - #{name} +               ) A</insert> at  -<select id= "SelectAll" resultmap= "Baseresultmap" > - SELECT a.id,a.user_id,a.num from user T -Right JOIN account A on t.id=a.user_id -</select> -</mapper>

Select

1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >3<mapper namespace= "Com.mrlu.mybatis.dao.UserDao" >4<resultmap id= "Baseresultmap" type= "Com.mrlu.mybatis.domain.User" >5<result column= "id" property= "id"/>6<result column= "name" property= "name"/>7<collection property= "Accounts" column= "id" select= "selectaccount" >8<result column= "id" property= "id"/>9<result column= "user_id" property= "UserId"/>Ten<result column= "num" property= "num"/> One</collection> A</resultMap> -<insert id= "Insert" keyproperty= "id" usegeneratedkeys= "true" > - INSERT into User (name) VALUES ( the #{name} -               ) -</insert> -  +<select id= "SelectAll" resultmap= "Baseresultmap" > -SELECT *From user T +</select> A  at<select id= "Selectaccount" resulttype= "Com.mrlu.mybatis.domain.Account" > -SELECT * from Account -WHERE user_id =#{id} -</select> -</mapper>

The fields marked are required and are not marked as optional.

About the use of collection in mapper file Resultmap in MyBatis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.