使用MyBatis(十)一对多集合的嵌套结果

1.实体类

package com.yw.test09;

public class Student
{
	private int s_id;
	private String s_name;
	private int age;
	private int c_id;
	
	
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}
	public int getC_id()
	{
		return c_id;
	}
	public void setC_id(int c_id)
	{
		this.c_id = c_id;
	}
	public int getS_id()
	{
		return s_id;
	}
	public void setS_id(int s_id)
	{
		this.s_id = s_id;
	}
	public String getS_name()
	{
		return s_name;
	}
	public void setS_name(String s_name)
	{
		this.s_name = s_name;
	}
	@Override
	public String toString()
	{
		return "Student [s_id=" + s_id + ", s_name=" + s_name + ", age=" + age + ", c_id=" + c_id + "]";
	}


	
	
}

Class班级的实体类不变


2.建立映射文件

<?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.yw.test09.ClassMapper">
	
	<resultMap id="classResult" type="com.yw.test09.Class">
	
		<id property="id" column="id" />
		<result property="name" column="name" />
		
		<collection property="students" ofType="com.yw.test09.Student" javaType="List" >
			<id property="s_id" column="s_id" />
			<result property="s_name" column="s_name" />
			<result property="age" column="age" />
			<result property="c_id" column="c_id" />
		</collection>
	</resultMap>
	
	<select id="selectClass" resultMap="classResult">
		select c.id,c.name,s.id as s_id,s.name as s_name,s.age,s.c_id  from class c join student s  on c.id=s.c_id where c.id=#{id}
	</select>
	
</mapper>

3.测试类一样

4.效果如下



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值