mybatis09--自连接一对多查询
查询导师 下面的所有 老师的信息!
创建实体类 和对应的数据库
/** *导师的实体类 */ public class Teacher { private Integer id; private String name; //导师下所有的老师 private Set<Teacher> teachers; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } public Teacher(Integer id, String name, Set<Teacher> teachers) { super(); this.id = id; this.name = name; this.teachers = teachers; } public Teacher() { super(); } @Override public String toString() { return "Teacher [id=" + id + ", name=" + name + ", teachers=" + teachers + "]"; } }
创建对应的mapper文件和dao
public interface TeacherDao { /** * 根据导师的编号查询所有的老师信息 * 相当于递归查询 */ List<Teacher> selectTeahcerByTid(Integer tId); }
<?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="cn.bdqn.dao.TeacherDao"> <resultMap type="Teacher" id="teacherMap"> <id property="id" column="id"/> <result property="name" column="name"/> <!-- 设置关联集合的属性 select="selectTeahcerByTid" 因为查询的结果都是一样的 递归查询 column="id" 根据查询到的id再作为参数进行下一次的查询 --> <collection property="teachers" ofType="Teacher" select="selectTeahcerByTid" column="id"/> </resultMap> <select id="selectTeahcerByTid" resultMap="teacherMap"> select id,name from teacher where tid=#{xxx} </select> </mapper>
mybatis.xml中管理mapper文件
<!-- 加载映射文件信息 --> <mappers> <mapper resource="cn/bdqn/dao/TeacherMapper.xml" /> </mappers>
测试代码
package cn.bdqn.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import cn.bdqn.bean.Teacher; import cn.bdqn.dao.TeacherDao; import cn.bdqn.util.SessionUtil; public class TeacherTest { TeacherDao dao; SqlSession session; @Before public void before() { // 因为需要关闭session 需要把session提取出去 session = SessionUtil.getSession(); dao = session.getMapper(TeacherDao.class); } @After public void after() { if (session != null) { session.close(); } } /** * 根据导师的id查询出对应的所有老师信息 */ @Test public void test1() { List<Teacher> list = dao.selectTeahcerByTid(1); for (Teacher teacher : list) { System.out.println(teacher); } } }
查询导师 的信息 以及 下面的所有 老师的信息!
修改dao 和 mapper文件
public interface TeacherDao { /** * 根据导师的编号查询所有的老师信息以及自身的信息 */ Teacher selectTeahcerById(Integer tId); }
<?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="cn.bdqn.dao.TeacherDao"> <select id="selectTeahcerByTid" resultMap="teacherMap"> select id,name from teacher where tid=#{xxx} <!--#{xxx} 就是再根据tid查询对应的子项 --> </select> <resultMap type="Teacher" id="teacherMap"> <id property="id" column="id"/> <result property="name" column="name"/> <!-- 设置关联集合的属性 select="selectTeahcerByTid" 因为查询的结果都是一样的 递归查询 column="id" 根据查询到的id再作为tid进行下一次的查询 --> <collection property="teachers" ofType="Teacher" select="selectTeahcerByTid" column="id"/> </resultMap> <select id="selectTeahcerById" resultMap="teacherMap"> <!--先查询出 自身的信息 --> select id,name from teacher where id=#{xxx} </select> </mapper>
修改测试类
/** * 根据导师的id查询出对应的所有老师信息 */ @Test public void test1() { Teacher teacher = dao.selectTeahcerById(1); System.out.println(teacher); }
作者:Rick__想太多先森
出处:http://www.cnblogs.com/xtdxs/
注意:本文仅代表个人理解和看法哟!和本人所在公司和团体无任何关系!