MyBatis关联映射----一对多映射

在上篇一对一的基础之上我们来开发一对多的关联映射,下面的例子中一对一和一对多同时实现了。我们以学生和学生证为一对一的关系,学生和课程为一对多的关系。

1.数据库

2.实体类

科目实体类

package com.mvc.demo.pojo;


/**
 * 科目类
 * @author hjh
 */

public class Subject {
	private Integer subId;		//科目Id
	private String 	subName;	//科目名
	private Integer sid;		//学生Id
	public Integer getSubId() {
		return subId;
	}
	public void setSubId(Integer subId) {
		this.subId = subId;
	}
	public String getSubName() {
		return subName;
	}
	public void setSubName(String subName) {
		this.subName = subName;
	}
	
	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	@Override
	public String toString() {
		return "Subject [subId=" + subId + ", subName=" + subName + "]";
	}
	
}

在原来学生实体类配置一个多的集合,来存储课程

3.用ResultMap实现映射

首先配置课程的ResultMap,我们在学生映射文件那边需要用到:

<?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.mvc.demo.dao.subject.SubjectMapper">
	<resultMap type="Subject" id="subjectMap">
		<id property="subId" column="subId" />
		<result property="subName" column="subName" />
		<result property="sid" column="sid" />
	</resultMap>
 
	
</mapper>

然后在学生那边引用这个resultMap

<?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.mvc.demo.dao.student.StudentMapper">
	<resultMap type="Student" id="studentMap">
		<id property="id" column="id" />
		<result property="name" column="name" />
		<result property="sex" column="sex" />
		<result property="cardId" column="card_id" />
		<!--一对一没有在student1一的这边写sql查出来,所以要写上他的一个查询的sql的id  -->
		<association property="card" column="id"
			select="com.mvc.demo.dao.card.CardMapper.findCardBysid" />
		<!--一对多在sql直接有写,多的那边配上resultMap就行  -->
		<collection property="subjectList"
			resultMap="com.mvc.demo.dao.subject.SubjectMapper.subjectMap"
		/>	
	</resultMap>
 
	<select id="getStudent" parameterType="int" resultMap="studentMap">
		select s.id,s.name,s.sex,s.card_id,sub.sid,sub.subId,sub.subName from Student s
		left join Subject sub on s.id=sub.sid where s.id = #{id}
	</select>


	<select id="getStudentBySid" parameterType="int" resultType="Student">
		select s.id,s.name,s.sex,s.card_id as cardId,c.note as node from Student s,Card c where s.id=c.sid and s.id=#{id}
	</select>
	
</mapper>

4.控制层和输出的信息

	//学生和学生证一对一,和课程一对多查询
	@RequestMapping("/getStuInfo")		
	public String getStuInfo(String id){
		//通过级联查询出信息
		Student stu=studentService.getStdentInfo(Integer.parseInt(id));
		System.out.println("the student is :"+stu);
		//循环出这个学生的所有课程
		System.out.println(stu.getName()+"有以下几门课程:");
		for (int i = 0; i < stu.getSubjectList().size(); i++) {
			System.out.println("课程》》"+(i+1)+":"+stu.getSubjectList().get(i).getSubName());
		}
		//通过加字段的方式得到信息
		/*Student stu=studentService.getStdentInfoBysId(Integer.parseInt(id));
		stu.info();*/
		return "login";
	}

以上就是一对一和一对多的配置方法了

具体的示例代码在:https://download.csdn.net/download/weixin_42189604/10633257

猜你喜欢

转载自blog.csdn.net/weixin_42189604/article/details/82390504