关联查询分为五种:
1.一对一
一个中国公民对应一张身份证,在实际开发中,一对一这种情况基本很少用到,这里不做具体解释
2.一对多
例子:根据一个老师的编号来查询这个老师下所有学生的集合
一对多分为单条SQL和一对多多条SQL
一对多单条SQL:
//一对多单条sql public dept findAlldeptName(int deptNo);
<!--一对多单条SQL--> <resultMap id="deptMapper" type="dept"> <result property="deptName" column="deptname"></result> <collection property="emps" ofType="emp"> <id property="empNo" column="empno"></id> <result column="empname" property="empName"></result> </collection> </resultMap> <select id="findAlldeptName" resultMap="deptMapper"> SELECT deptName,empName FROM dept,emp WHERE dept.`deptNo`=emp.`deptNo` AND dept.`deptNo`=#{deptNo} </select>
一对多多条SQL:
//一对多条sql public dept findAllMoreDeptName(int deptNo);
<!--一对多多条SQL--> <resultMap id="deptMappers" type="dept"> <id property="deptNo" column="deptno"></id> <result property="deptName" column="deptname"></result> <collection property="emps" ofType="emp" select="empselect" column="deptNo"> </collection> </resultMap> <select id="empselect" resultType="emp"> SELECT * from emp where deptNo=#{deptNo} </select> <select id="findAllMoreDeptName" resultMap="deptMappers"> select * from dept where deptNo=#{deptNo} </select>
3.多对一
多对一分为单条SQL和多条SQL
多对一单条SQL:
//多对一单条sql public emp findAllemp(int empNo);
<!--多对一单条SQL--> <resultMap id="empMapper" type="emp"> <id property="empNo" column="empno"></id> <result property="empName" column="empname"></result> <association property="depts" javaType="dept" > <id property="deptNo" column="deptno"></id> <result property="deptName" column="deptname"></result> </association> </resultMap> <select id="findAllemp" resultMap="empMapper"> SELECT deptName,empName,empno,emp.deptNo FROM dept,emp WHERE dept.deptNo=emp.deptNo AND empNo=#{empNo} </select>
多对一多条SQL:
//多对一多条sql public emp findAllemps(int empNo);
<!--多对一多条SQL--> <resultMap id="empMappers" type="emp"> <id property="empNo" column="empno"></id> <result property="empName" column="empname"></result> <association property="depts" javaType="dept" select="getdept" column="deptNo" > </association> </resultMap> <select id="getdept" resultType="dept"> SELECT * from dept where deptNo=#{deptNo} </select> <select id="findAllemps" resultMap="empMappers"> SELECT * from emp where empNo=#{empNo} </select>
4.多对多
同时满足一对多,多对一的条件就是多对多
例如:一个老师可以有多个学生,反过来则一个学生可以有多个老师
//根据老师编号查询所属学生集合 public Teacher findAllStudent(int tid);
<!--根据老师编号查询学生集合--> <resultMap id="TeacherMapper" type="Teacher"> <id property="tid" column="tid"></id> <result property="tname" column="tname"></result> <collection property="stu" ofType="Student"> <id property="sid" column="sid"></id> <result property="sname" column="sname"></result> </collection> </resultMap> <select id="findAllStudent" resultMap="TeacherMapper"> SELECT * FROM students AS s,teacher AS t,Teacher_Student AS ts WHERE s.`sid`=ts.`sid` AND t.`tid`=ts.`tid` AND t.`tid`=#{tid} </select>
5.自关联
既可以充当一方,也可以充当多方,是1:n或n:1的原型
例如:查询某个pid对应的所有分类的集合
//自关联 public List<category> findAllById(int pid);
<!--自关联--> <resultMap id="categoryMapper" type="category"> <id property="cid" column="cid"></id> <result property="cname" column="cname"></result> <collection property="sets" ofType="category" select="findAllById" column="cid"> </collection> </resultMap> <select id="findAllById" resultMap="categoryMapper"> select * from category where pid=#{pid} </select>
测试类的代码书写:
//自关联 @Test public void test15(){ SqlSession sqlSession= MyBatisUtil.getSession(); IdeptDao mapper = sqlSession.getMapper(IdeptDao.class); List<category> lists = mapper.findAllById(0); for (category item:lists){ System.out.println(item); } sqlSession.close(); }
根据一个一级id查询到此id下的所有子id