myBatis-类型关联

1.一对多 

collection
<resultMap id="deptsql" type="Dept">
    <id column="deptNo" property="deptNo"></id>
    <result column="deptName" property="deptName"/>
    <collection property="emps" ofType="Emp" select="selectby" column="deptNo"></collection>

</resultMap>
    <select id="selectby" resultType="Emp">
        SELECT *FROM emp WHERE deptNo=#{deptNo}
    </select>
    <select id="getEmpsByDeptNoS" resultMap="deptsql">
        SELECT deptNo,deptName
        from dept
        WHERE deptNo=#{deptNo}
    </select>

测试类
@Test
public void testOneToManys(){
SqlSession session= myBatis.getSqlSession();
IStudentInfoDao dao = session.getMapper(IStudentInfoDao.class);
Dept dept = dao.getEmpsByDeptNoS(5);
System.out.println(dept.getDeptName());
for (Emp emp:dept.getEmps()) {
System.out.println(emp.getEmpName());
}
session.close();
}

 2.多对一

<!--多对一-->
    <resultMap id="empMapper" type="Emp">
        <id column="empNo" property="empNo"></id>
        <result column="empName" property="empName"></result>
        <association property="dept" javaType="Dept">
            <id column="deptNo" property="deptNo"></id>
            <result column="deptName" property="deptName"></result>
        </association>
    </resultMap>
    <select id="getEmpNo" resultMap="empMapper">
        select dept.deptNo,deptName,empNo,empName
        from dept,emp
        where dept.deptNo=emp.deptNo
        and empNo=#{empNo}
    </select>

<!--多条件 多对一-->
<resultMap id="deptsqls" type="Emp">
    <id column="empNo" property="empNo"></id>
    <result column="empName" property="empName"></result>
    <association property="dept" javaType="Dept" select="selectbys" column="deptNo"></association>
</resultMap>
      <select id="selectbys" resultType="Dept">
          SELECT *FROM dept WHERE deptNo=#{deptNo}
        </select>
    <select id="getEmpNos" resultMap="deptsqls">
        SELECT  deptNo,empNo,empName
        from emp
        WHERE empNo=#{empNo}
    </select>

  3.自链接

<!--自链接-->
    <resultMap id="getCatery" type="Category">
        <id column="cid" property="cid"></id>
        <result column="cname" property="cname"></result>
        <result column="pid" property="pid"></result>
        <collection property="list" ofType="Category" select="getCate" column="cid"></collection>
    </resultMap>
    <select id="getCate" resultMap="getCatery">
        SELECT *FROM  category WHERE pid=#{pid}
    </select>

  4.多对多

 <resultMap id="sts" type="Teacher">
        <id column="tid" property="tid"></id>
        <result column="tname" property="tname"></result>
        <collection property="stus" ofType="Students">
            <id column="sid" property="sid"></id>
            <result column="sname" property="sname"></result>
        </collection>
    </resultMap>

    <select id="getts" resultMap="sts">
        SELECT  students.sid,sname,teacher.tid,tname FROM
        students,ts,teacher WHERE students.sid=ts.sid
        AND teacher.tid=ts.tid
        AND  teacher.tid=#{tid}
    </select>

测试类
//过呢根据老师编号查询对应学生
@Test
public void testtss(){
SqlSession session= myBatis.getSqlSession();
IStudentInfoDao dao = session.getMapper(IStudentInfoDao.class);
Teacher teacher=dao.getts(1);
System.out.println(teacher.getTname());
for (Students cate:teacher.getStus()
) {
System.out.println(cate.getSname());

}

session.close();
}

  

 

转载于:https://www.cnblogs.com/wangbenqing/p/7191327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值