07 mybatis多对多

本文将阐述mybatis中的表在多对多关系的情况下完成查询操作。

1、环境约束

  • win10 64位操作系统
  • idea2018.1.5
  • jdk-8u162-windows-x64
  • mybatis3.2.8
  • mysql 6.5

2、前提约束

3、操作步骤

  • 创建三张表并插入数据
create table t_student(id int,name varchar(20));
insert into t_student(id,name) values(1,'ali');
insert into t_student(id,name) values(2,'xiaoli');
create table t_course(id int,name varchar(20));
insert into t_course(id,name) values(1,'java');
insert into t_course(id,name) values(2,'python');
create table t_student_course(id int,studentid int, courseid int);
insert into t_student_course(id,studentid ,courseid) values(1,1,1);
insert into t_student_course(id,studentid ,courseid) values(2,1,2);
insert into t_student_course(id,studentid ,courseid) values(3,2,2);
  • 创建以上三张表对应的实体类
public class Student{
    private int id;
    private String name;
    //无参构造方法,有参构造方法,get方法, set方法
}

public class Course{
    private int id;
    private String name;
    //无参构造方法,有参构造方法,get方法, set方法
}

public class StudentCourse{
    private int id;
    private Student student;
    private Course course;
    //无参构造方法,有参构造方法,get方法, set方法
}
  • 在UserMapper.java中增加一个方法声明
List<StudentCourse> getStudentCourses();
  • 在UserMapper.xml中增加以下标签
    <select id="selectStudentCourse" resultMap="studentCourseMap">
        select * from t_student_course
    </select>

    <resultMap id="studentCourseMap" type="net.wanho.entity.StudentCourse">
        <id column="id" property="id"></id>
        <association column="studentid" property="student"  select="selectStudent"></association>
        <association column="courseid" property="course"  select="selectCourse"></association>
    </resultMap>

    <select id="selectStudent" resultMap="studentMap">
        select * from t_student where id=#{studentid}
    </select>

    <resultMap id="studentMap" type="net.wanho.entity.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
    </resultMap>

    <select id="selectCourse" resultMap="courseMap">
        select * from t_course where id=#{courseid}
    </select>

    <resultMap id="courseMap" type="net.wanho.entity.Course">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
    </resultMap>
  • 测试
//关键代码
String resource ="mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<StudentCourse>  scList = userMapper.getStudentCourses();

以上就是在mybatis中完成多对多查询的过程。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值