• mybatis查询多对一、一对多


    现在数据库mybatis中存在两个表:student与teacher,结构与数据分别为:

    student:

    teacher:

     其中:表student的stid字段为外键,约束参考表teacher中的tid字段。

    两个javabean:student、teacher:

    student:

    @Data
    public class Student {
        private int sid;
        private String sname;
        private Teacher teacher;
    }

    teacher:

    @Data
    public class Teacher {
        private int tid;
        private String tname;
    }

     studentMapper接口:

    public interface StudentMapper {
        List<Student> getStudents();
        List<Student> getStudents1();
    }

    如何查询学生所有信息加上所关联的是哪个老师?

    sql查询语句我们可以这样写:

    select sid,sname,tname from mybatis.student,mybatis.teacher where stid = tid;

    那么使用mybatis该怎么查询呢?

    这里提供了两种方式:

    子查询方式:

    <select id="getStudents" resultMap="StudentTeacher">
        select * from mybatis.student
    </select>
    <resultMap id="StudentTeacher" type="pojo.Student">
        <result property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <association property="teacher" column="stid" javaType="pojo.Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="pojo.Teacher">
        select * from mybatis.teacher where tid = #{stid}
    </select>

    嵌套查询方式:

     <select id="getStudents1" resultMap="StudentTeacher1">
        select sid,sname,tname from mybatis.student,mybatis.teacher where stid = tid
    </select>
    <resultMap id="StudentTeacher1" type="pojo.Student">
        <result property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <association property="teacher" javaType="pojo.Teacher">
            <result property="tname" column="tname"/>
        </association>
    </resultMap>

    嵌套方式比较容易掌握。以上就是多对一的操作。

    下面来看看一对多的操作:

    javabean:

    @Data
    public class Student {
        private int sid;
        private String sname;
        private int stid;
    }
    @Data
    public class Teacher {
        private int tid;
        private String tname;
        private List<Student> students;
    }

    TeacherMapper接口:

    public interface TeacherMapper {
        //根据tid获取指定老师及该老师下所有的学生
        Teacher getTeacherById(@Param("tid") int tid);
        Teacher getTeacherById1(@Param("tid") int tid);
    }

    TeacherMapper.xml文件:

    嵌套查询处理:

    <select id="getTeacherById1" resultMap="TeacherStudent2">
            select * from teacher where tid = #{tid}
    </select>
    <resultMap id="TeacherStudent2" type="pojo.Teacher">
        <collection property="students" column="tid" ofType="pojo.Student"  javaType="java.util.ArrayList"  select="getStudentsByTid" />
    </resultMap>
    <select id="getStudentsByTid" resultType="pojo.Student">
        select * from student where stid = #{tid}
    </select>

    按照结果嵌套处理:

    <select id="getTeacherById" resultMap="TeacherStudent">
            select tid,tname,sid,sname from student,teacher where (stid=tid) and (tid=#{tid})
    </select>
    <resultMap id="TeacherStudent" type="pojo.Teacher">
        <result property="tid" column="tid"/>
        <result property="tname" column="tname"/>
        <collection property="students" ofType="pojo.Student">
            <result property="sid" column="sid"/>
            <result property="sname" column="sname"/>
        </collection>
    </resultMap>
  • 相关阅读:
    vue检查用户名是否重复
    后端注册接口完善
    django添加检查用户名和手机号数量接口
    Vue联调,图片及短信验证码
    swift webView 提出这样的要求你能忍吗?
    iOS 如何给Xcode7项目添加“.pch”文件
    swift 定制自己的Button样式
    Swift 为你的webView定制标题
    swift 如何获取webView的内容高度
    如何在MAC上使用SVN,简单几行命令搞定
  • 原文地址:https://www.cnblogs.com/wmskywm/p/13585956.html
Copyright © 2020-2023  润新知