MyBatis类型关联多对多

DAO层

ICategoryDAO接口

package cn.happy.dao;

import cn.happy.entity.Category;

import java.util.List;

/**
 * Created by Happy on 2017-07-14.
 */
public interface ICategoryDAO {
    //01.根据pid=查找所有子分类的集合
    public List<Category> getChildrenByPid(int pid);
}

ITeacherDAO.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="cn.happy.dao.ITeacherDAO">
     <resultMap id="teacherMapper" type="Teacher">
          <id column="tid" property="tid"></id>
         <result column="tname" property="tname"/>
         <!--集合-->
         <collection property="stus" ofType="Student">
             <id column="sid" property="sid"></id>
             <result column="sname" property="sname"/>
         </collection>
     </resultMap>

    <select id="getStudentsByTeaderId" resultMap="teacherMapper">
         select student.sid,sname,teacher.tid,tname
        from student,teacher_student,teacher
        where student.sid=teacher_student.sid
         and  teacher.tid=teacher_student.tid
         and  teacher.tid=#{tid}
    </select>
</mapper>
单侧
public class MyBatisTest0716 {
@Test
    public void testSelf() {
        SqlSession session = MyBatisUtil.getSession();
        ITeacherDAO dao = session.getMapper(ITeacherDAO.class);
        Teacher teacher = dao.getStudentsByTeaderId(1);
        System.out.println(teacher.getTname());
        for (Student stu:teacher.getStus()) {
            System.out.println(stu.getSname());
        }
    }

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值