×îÐÂÏûÏ¢£º¹Ø×¢¡¾Ì«Æ½ÑóѧϰÍø¡¿Î¢ÐŹ«Öںţ¬¿ÉÒÔ»ñÈ¡È«Ì××ÊÁÏ,¡¾È«Ì×Java»ù´¡27Ìì¡¿¡¾JavaEE¾ÍÒµÊÓƵ4¸öÔ¡¿¡¾Android¾ÍÒµÊÓƵ4¸öÔ¡¿

mybatis associationÒ»¶ÔÒ»¹ØÁª²éѯÓëcollectionÒ»¶Ô¶à¹ØÁª²éѯѧϰ

Javaweb ̫ƽÑóѧϰÍø ä¯ÀÀ ÆÀÂÛ

mybatis associationÊÇÒ»¶ÔÒ»µÄ¹ØÁª²éѯ£¬ËùνµÄÒ»¶ÔÒ»¹ØÁª²éѯ£¬Àí½âºÜ¼òµ¥¡£±ÈÈçÎÒÃǹ´Ñ¡¡°Ð¡Ã÷¡±Í¬Ñ§Ö®ºó´ø³ö¡°Ð¡Ã÷°Ö°Ö¡±µÄÄêÁ䣬ְҵµÈµÈ¸öÈËÐÅÏ¢£¬ÒòΪСÃ÷¾ÍÒ»¸ö°Ö°Ö£¬ËùÒÔÊÇÒ»¶ÔÒ»¹Øϵ¡£

Èç¹ûÄÜ´ø³ö¡°Ò»ÈºÐ¡Ã÷°Ö°Ö¡±£¬ÄǾͲ»ÊÇmybatis associationÒ»¶ÔÒ»¹ØÁª²éѯ£¬¶øÊÇÊôÓÚmybatis collectionÒ»¶Ô¶à¹ØÁª²éѯµÄ·¶³ëÁË£¬Èç¹ûСÃ÷Óкܶà°Ö°Ö¾Í¡£¡£¡£¡£ÁË£¡

Ò»£ºÏÂÃæÎÒÃÇÏÈÀ´Ñ§Ï°mybatis associationÒ»¶ÔÒ»¹ØÁª²éѯ°É£¡

associationÒ»¶ÔÒ»¹ØÁª²éѯÐèÒªÁ½¸öʵÌåÀ࣬һ¸öÊÇСÃ÷£¨XiaoMing£©µÄʵÌåÀ࣬һ¸öÊÇСÃ÷°Ö°Ö£¨XiaoMingFather£©µÄʵÌåÀ࣬·Ö±ðÈçÏ£º

СÃ÷µÄʵÌåÀࣨXiaoMing£©ÈçÏ£º

public class XiaoMing {
        //СÃ÷Ö÷¼üid
	private int id;
	//Ãû×Ö
	private String name;
	
	//°Ö°Ö£¬Ò»¶ÔÒ»²éѯ֮ºó»á½«°Ö°ÖµÄ¶ÔÏó¸³Öµ¸øÕâ¸öÊôÐÔ
	private XiaoMingFather father;
	
	//getter setter...
}

СÃ÷°Ö°ÖµÄʵÌåÀࣨXiaoMingFather£©ÈçÏ£º

public class XiaoMingFather {
        //СÃ÷°Ö°ÖÖ÷¼üid
	private int id;
	//СÃ÷µÄid£¬ºóÃæ»áÓõ½Ð¡Ã÷id²é³öСÃ÷°Ö°Ö¶ÔÏó
	private int xiaomingid;
	//СÃ÷°Ö°ÖµÄÃû×Ö
	private String name;
	//СÃ÷°Ö°ÖµÄÄêÁä
	private int age;
	//СÃ÷°Ö°ÖµÄ¹¤×÷Ãû³Æ
	private String jobName;
	
	//getter setter...
}

²½ÖèÒ»£ºÔÚСÃ÷°Ö°ÖµÄmapper javaÀàÖмÓÈëÒ»¸ö½Ó¿Ú£¬¸ù¾ÝСÃ÷id²éѯСÃ÷°Ö°ÖµÄ¶ÔÏ󣬴úÂëÈçÏ£º

package com.tpyyes.dao; //mapperËùÔÚ°ü·¾¶

public interface XiaoMingFatherMapper{

	//·µ»Ø¶ÔÏóΪСÃ÷°Ö°Ö£¬´«ÈëµÄΪСÃ÷id
	XiaoMingFather selectbyXiaomingid(Integer xiaomingid);
	
}

²½Öè¶þ£ºÔÚСÃ÷°Ö°ÖµÄmapper xmlÎļþÖÐʵÏÖselectbyXiaomingid()Õâ¸ö²éѯÓï¾ä£¬Ð¡±à½«Õâ¸öÎļþÈ¡ÃûΪXiaoMingFatherMapper.xml£¬¼ÓÈ롰СÃ÷°Ö°Ö¡±µÄÊý¾Ý¿â±íÃûΪ¡°xiao_ming_father¡±£¬Ôò´úÂëÈçÏ£º

<select id="selectbyXiaomingid" parameterType="java.lang.Integer" resultMap="BaseResultMap">
	select * from xiao_ming_father where id = #{xiaomingid,jdbcType=INTEGER}
</select>

²½ÖèÈý£ºÔÚСÃ÷mapper xmlÅäÖÃÎļþÖÐÌí¼ÓÈçϵÄresultMapÓ³É䣬ÔÚĩβ¼ÓÈë¡°association¡±±êÇ©£¬´«ÈëµÄÖµÊÇСÃ÷µÄid£¬Ó³ÉäÎļþÈçÏ£º

<resultMap id="BaseResultMap" type="com.voavoice.english.model.Menu">
	<id column="id" jdbcType="INTEGER" property="id" />
	<result column="name" jdbcType="VARCHAR" property="name" />
	
	// fatherÊôÐÔ¾ÍÊÇСÃ÷ʵÌåÀàÖеİְÖÊôÐÔ£¬idÊÇСÃ÷µÄidÖµ£¬¸ù¾ÝСÃ÷µÄid²éѯ³öСÃ÷°Ö°Ö£¡
	<association property="father" column="id" select="com.tpyyes.dao.XiaoMingFatherMapper.selectbyXiaomingid"/>
</resultMap>

ͨ¹ýÉÏÃæÈý¸ö²½ÖèÖ®ºó£¬µ±ÎÒÃÇÔÚ³ÌÐòÖиù¾Ýid»òÆäËüÌõ¼þ²éѯСÃ÷¶ÔÏóµÄʱºò£¬³ÌÐò¾Í»á×Ô¶¯È¥¡°xiao_ming_father¡±ÕâÕűíÖаÑСÃ÷°Ö°ÖµÄÊý¾Ý²éѯ³öÀ´£¬²¢¸³Öµµ½¡°father¡±Õâ¸öÊôÐÔÖÐÁË£¬ºÜ¼òµ¥°É£¡

¶þ£ºmybatis collectionÒ»¶Ô¶à¹ØÁª²éѯ

ËùνµÄÒ»¶Ô¶à¹ØÁª²éѯ£¬¾ÍÊǸù¾ÝÒ»¸öСÃ÷µÄidÄܲéѯ³ö¡°¶à¸öСÃ÷°Ö°Ö¡±£¬µ±ÎÒÃÇÕâÑùÀí½âµÄʱºò£¬¾Í²»ÄÑѧϰmybatis collectionÒ»¶Ô¶à¹ØÁª²éѯÁË£¬ÆäʵֻÐèÒª¸Ä±äÈý¸ö²¿·Ö£¬¾Í¿ÉÒÔ²éѯ³öСÃ÷¶à¸ö°Ö°ÖµÄÐÅÏ¢ÁË£¬ÈçÏ£º

Ð޸IJ¿·ÖÒ»£º½«Ð¡Ã÷µÄʵÌåÀࣨXiaoMing£©ÖеÄfather¸Ä³ÉList¼¯ºÏ£¬ÊµÌåÀàÈçÏ£º

public class XiaoMing {
        //СÃ÷Ö÷¼üid
	private int id;
	//Ãû×Ö
	private String name;
	
	//°Ö°Ö£¬Ò»¶ÔÒ»²éѯ֮ºó»á½«°Ö°ÖµÄ¶ÔÏó¸³Öµ¸øÕâ¸öÊôÐÔ
	private List<XiaoMingFather> fatherList;
	
	//getter setter...
}

Ð޸IJ¿·Ö¶þ£º½«Ð¡Ã÷°Ö°ÖµÄmapper javaÀàÖеĽӿڴӷµ»ØµÄʵÌåÀà¶ÔÏó±ä³É·µ»ØList¼¯ºÏ£¬´úÂëÈçÏ£º

package com.tpyyes.dao; //mapperËùÔÚ°ü·¾¶

public interface XiaoMingFatherMapper{

	//·µ»Ø¶ÔÏóΪСÃ÷°Ö°ÖµÄ¼¯ºÏ£¬´«ÈëµÄΪСÃ÷id
	List<XiaoMingFather> selectbyXiaomingid(Integer xiaomingid);
	
}

Ð޸IJ¿·ÖÈý£º½«Ð¡Ã÷mapper xmlÅäÖÃÖеÄassociation±êÇ©Õⲿ·Ö£¬¸Ä³Écollection±êÇ©£¬Íâ¼üÊôÐԸijɡ°fatherList¡±£¬´«Èëµ½selectbyXiaomingid()·½·¨ÖеÄÖµÈÔÈ»ÊÇСÃ÷µÄid£¬´úÂëÈçÏ£º

<collection property="fatherList" column="id" select="com.tpyyes.dao.XiaoMingFatherMapper.selectbyXiaomingid"/>

ÕâÑù£¬²éѯСÃ÷µÄ¸öÈËÐÅÏ¢£¬¾Í»á×Ô¶¯´ø³öСÃ÷µÄ¶à¸ö°Ö°ÖÁË£¡Å£êþ°É£¡

À´Ô´ÍøÕ¾£ºÌ«Æ½ÑóѧϰÍø£¬×ªÔØÇë×¢Ã÷³ö´¦£ºhttp://www.tpyyes.com/a/javaweb/782.html
"ÎÄÕºÜÖµ£¬´òÉÍêûÀÍ×÷ÕßÒ»ÏÂ"
΢Ðźţº Javaweb_engineer

´òÉÍ

È¡Ïû

¸ÐлÄúµÄÖ§³Ö£¬ÎÒ»á¼ÌÐøŬÁ¦µÄ£¡

ɨÂëÖ§³Ö
ɨÂë´òÉÍ£¬Äã˵¶àÉپͶàÉÙ

´ò¿ªÖ§¸¶±¦É¨Ò»É¨£¬¼´¿É½øÐÐɨÂë´òÉÍŶ

Óë±¾ÎÄÏà¹ØµÄÎÄÕÂ

·¢±íÎÒµÄÆÀÂÛ
È¡ÏûÆÀÂÛ

񡀂

ÄúµÄ»Ø¸´ÊÇÎÒÃǵĶ¯Á¦£¡

  • êÇ³Æ (±ØÌî)

ÍøÓÑ×îÐÂÆÀÂÛ