MyBatis 多对多关联映射

示例【通过班级查询老师信息】

创建t_classes

创建t_classessTeacher

创建t_teacher

创建Classes

package com.po;
import java.util.List;
public class Classes {
	private Integer cid;
	private String cname;
	private List<Teacher> teachers;
	public Integer getCid() {
		return cid;
	}
	public void setCid(Integer cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public List<Teacher> getTeachers() {
		return teachers;
	}
	public void setTeachers(List<Teacher> teachers) {
		this.teachers = teachers;
	}
	@Override
	public String toString() {
		return "Classes [cid=" + cid + ", cname=" + cname + ", teachers=" + teachers + "]";
	}
}

创建Teacher

package com.po;
import java.util.List;
public class Teacher {
	private Integer tid;
	private String tname;
	private List<Classes> classes;
	public Integer getTid() {
		return tid;
	}
	public void setTid(Integer tid) {
		this.tid = tid;
	}
	public String getTname() {
		return tname;
	}
	public void setTname(String tname) {
		this.tname = tname;
	}
	public List<Classes> getClasses() {
		return classes;
	}
	public void setClasses(List<Classes> classes) {
		this.classes = classes;
	}
	@Override
	public String toString() {
		return "Teacher [tid=" + tid + ", tname=" + tname + "]";
	}
}

创建ClassesMapper.xml

<?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.mapper.ClassesMapper">
	<!-- 自定义结果映射 -->
	<resultMap type="com.po.Classes" id="ClassesWithTeacherResult">
		<id property="cid" column="cid"/>
		<result property="cname" column="cname"/>
		<!-- 多表关联映射 -->
		<collection property="teachers" ofType="com.po.Teacher">
			<id property="tid" column="tid"/>
			<result property="tname" column="tname"/>
		</collection>
	</resultMap>
	<select id="findClassesWithTeacher" parameterType="Integer" resultMap="ClassesWithTeacherResult">
		select * from t_classes c,t_teacher t,t_classessTeacher ct
		where ct.cid=c.cid
		and ct.tid=t.tid
		and c.cid=#{cid}
	</select>
</mapper>

配置mybatis-config.xml

<!-- 配置Mapper的位置 -->
	<mappers>
		<mapper resource="com/mapper/ClassesMapper.xml"/>			
	</mappers>

创建ClassesDao

package com.dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.factory.SessionFactory;
import com.po.Classes;
public class ClassesDao {
	private SqlSession session=SessionFactory.getSqlSession();
	@Test
	public void findClassesWithTeacher() {
		Classes classess=session.selectOne("com.mapper.ClassesMapper.findClassesWithTeacher", 1);
		System.out.println(classess);
	}
}

执行 findClassesWithTeacher()方法

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

云淡风轻58

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

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

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

打赏作者

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

抵扣说明:

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

余额充值