查询导师 下面的所有 老师的信息!
创建实体类 和对应的数据库
/** *导师的实体类 */public class Teacher { private Integer id; private String name; //导师下所有的老师 private Setteachers; 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 getTeachers() { return teachers; } public void setTeachers(Set teachers) { this.teachers = teachers; } public Teacher(Integer id, String name, Set 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 { /** * 根据导师的编号查询所有的老师信息 * 相当于递归查询 */ ListselectTeahcerByTid(Integer tId);}
mybatis.xml中管理mapper文件
测试代码
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() { Listlist = dao.selectTeahcerByTid(1); for (Teacher teacher : list) { System.out.println(teacher); } } }
查询导师 的信息 以及 下面的所有 老师的信息!
修改dao 和 mapper文件
public interface TeacherDao { /** * 根据导师的编号查询所有的老师信息以及自身的信息 */ Teacher selectTeahcerById(Integer tId);}
修改测试类
/** * 根据导师的id查询出对应的所有老师信息 */ @Test public void test1() { Teacher teacher = dao.selectTeahcerById(1); System.out.println(teacher); }