mybatis系列八:一对多关联查询

这里仍然以系表(department)和学生表(student)为例来讲解。系和学生是典型的一对多的关系。

一对多关联我们使用的是collection 标签。collection 标签如何使用,下面我们来看具体的案例。

========================StudentMapper.xml====================

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "mybatis-3-mapper.dtd">
<mapper namespace="stu">
    <!-- 单张表的映射 -->
    <!-- autoMapping="true":对于列名和实体类的属性名一致,那么我们可以省略相应的映射关系 -->
    <resultMap id="stuMap" type="StudentEntity" autoMapping="true">
       <!--id不要省-->
       <id property="stuId" column="stuId"/>
    </resultMap>
</mapper>

==========================DeptEntity.java=======================

package com.obtk.entitys;

import java.util.*;

public class DeptEntity implements java.io.Serializable {

	private static final long serialVersionUID = -2856143193567492289L;
	private int deptId;
	private String deptName;  //注意和列名不同
	private String address;
	//体现一对多的关联关系
	private List<StudentEntity> stuList=new ArrayList<StudentEntity>();

	public DeptEntity() {
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	
	public String getDeptName() {
		return deptName;
	}

	public int getDeptId() {
		return this.deptId;
	}

	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}

	
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	public void setStuList(List<StudentEntity> stuList) {
		this.stuList = stuList;
	}
	
	public List<StudentEntity> getStuList() {
		return stuList;
	}
	
}

======================DeptMapper.xml==========================

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "mybatis-3-mapper.dtd">
<mapper namespace="dept">
    <!-- 单张表的映射 -->
	<resultMap id="deptMap" type="DeptEntity" autoMapping="true">
            <!--id不要省-->
            <id property="deptId" column="deptId"/>
	    <result property="deptName" column="departName"/>
	</resultMap>
	<!-- 一对多关联 -->
	<resultMap id="deptJoinStu" type="DeptEntity" autoMapping="true">
                <!--id不要省-->
                <id property="deptId" column="deptId"/>
		<result property="deptName" column="departName"/>
		<!-- javaType是指关联属性多对应的类型,ofType是指arraylist里面所装数据的类型 -->
		<!-- resultMap是指关联表的映射类型 -->
		<collection property="stuList" javaType="arraylist" 
		      ofType="StudentEntity" resultMap="stu.stuMap">
		</collection>
	</resultMap>
	
	<select id="selectByDeptStu" parameterType="int" resultMap="deptJoinStu">
		select * from department d inner join student s 
		on d.deptId= s.deptIdd
		where d.deptId=#{deptId}
	</select>
</mapper>


测试代码:

package com.obtk.test2;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.obtk.entitys.DeptEntity;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;

public class TestOneToMany01 {
	public static void main(String[] args) {
		SqlSession session=null;
		try {
			//1.得到session
			session=MybatisUtil.getSession();
			DeptEntity dept=session.selectOne("dept.selectByDeptStu", 10);
			List<StudentEntity> stuList=dept.getStuList();
			for(StudentEntity stu : stuList){
				System.out.println(stu.getStuName()+","+stu.getGender()
						+","+dept.getDeptName()+","+dept.getAddress());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			MybatisUtil.closeSession();
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御前两把刀刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值