resultMap的用法,并且可以处理一对多关系

它是映射集的引用,将执行强大的映射功能,resultMap 可以给予我们自定义映射规则的机会
它是 MyBatis 最复杂的元素,可以配置映射规则,级联,typeHandler等

1、Java实体类

public class Group implements Serializable {

    
    private String id;//主键id
    private String gname;//小组名称
    private Integer gnum;//成员数量
    private List<Student> studentList;//小组成员

}

public class Student implements Serializable {

    
    private String id;//主键id
    private String name;//学生姓名
    private String age;//学生年龄
    private String gid;//组外键

}

2、mysql语句如下:

<resultMap id="astChecksPlanMap" type="org.modules.ast.entity.Group">
		<result column="id" property="id" jdbcType="VARCHAR"/>
		<result column="name" property="gname" jdbcType="VARCHAR"/>
		<result column="num" property="gnum" jdbcType="Integer"/>
		<collection property="studentList" column="id" select="findStudentList"></collection>
</resultMap>


<select id="findStudentList" resultType="org.modules.ast.entity.Student">
		SELECT
			id,
			name,
			age,
            gid
		FROM
			student
		where 
			gid = #{id} 
</select>


<select id="selectMyPagg" resultMap="astChecksPlanMap">
        SELECT
			id,
			name,
			num
		FROM
			group
 </select>

3、返回的结果如下

{
id:"121"
name:"飞虎队"
num:"3"
studentList:{
    [id:"001,name:"张三",age:"12",gid:"121"]
    [id:"002,name:"李四",age:"11",gid:"121"]
    [id:"003,name:"王五",age:"10",gid:"121"]
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值