美文网首页
MyBatis联合查询(多对多)

MyBatis联合查询(多对多)

作者: _FireFly_ | 来源:发表于2020-11-06 09:28 被阅读0次

sql.txt



#设计一个老师的表格
create table teacher(
    tid int(10),
    tname varchar(20),
    tsex varchar(4),
    tage int(3)
) character set utf8;

#添加主键约束
alter table teacher add constraint pk_teacher primary key(tid);


#设计一个学生的表格

create table student(

    sid int(10),
    sname varchar(20),
    ssex varchar(4),
    sage int(3)
) character set utf8;

#添加主键约束

alter table student add constraint pk_student primary key(sid);

#设计一张中间表 老师-学生的关系
create table tea_stu(
    tid int(10),

    sid int(10)
) character set utf8;

#分别设置两个列的外键约束
alter table tea_stu add constraint fk_teacher foreign key(tid) references teacher(tid);

alter table tea_stu add constraint fk_student foreign key(sid) references student(sid);

#设置联合主键
alter table tea_stu add constraint pk_tea_stu primary key(tid,sid);


#添加测试数据

insert into teacher values(1,'zzt','男',18);

insert into teacher values(2,'panda','男',58);
insert into teacher values(3,'艾薇','女',16);

insert into student values(1,'赵一','男',18);

insert into student values(2,'钱一','女',17);

insert into student values(3,'孙一','女',19);

insert into student values(4,'李一','男',18);

insert into student values(5,'周一','男',17);

insert into student values(6,'吴一','女',19);

insert into student values(7,'郑一','女',18);

insert into student values(8,'王一','男',16);


insert into tea_stu values(1,1);
insert into tea_stu values(1,2);
insert into tea_stu values(1,3);
insert into tea_stu values(1,4);
insert into tea_stu values(1,5);
insert into tea_stu values(1,6);
insert into tea_stu values(1,7);
insert into tea_stu values(1,8);
insert into tea_stu values(2,1);
insert into tea_stu values(2,2);
insert into tea_stu values(2,4);
insert into tea_stu values(2,7);
insert into tea_stu values(3,1);
insert into tea_stu values(3,2);
insert into tea_stu values(3,5);
insert into tea_stu values(3,8);



select * from teacher;
select * from student;

select t.*,s.* from teacher t,tea_stu ts,student s where t.tid = ts.tid and ts.sid = s.sid;

一个学生可以有多个老师授课,一个老师也可以带多个学生(多对多 需要一张中间表 tea_stu 为中间表)

Student

package domain;

import java.util.List;

public class Student {

    //自有属性
    private Integer sid;
    private String sname;
    private String ssex;
    private Integer sage;
    //关联属性   老师集合
    private List<Teacher> teacherList;


    public Student() {
    }

    public Student(Integer sid, String sname, String ssex, Integer sage, List<Teacher> teacherList) {
        this.sid = sid;
        this.sname = sname;
        this.ssex = ssex;
        this.sage = sage;
        this.teacherList = teacherList;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", ssex='" + ssex + '\'' +
                ", sage=" + sage +
                ", teacherList=" + teacherList +
                '}';
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }

    public Integer getSage() {
        return sage;
    }

    public void setSage(Integer sage) {
        this.sage = sage;
    }

    public List<Teacher> getTeacherList() {
        return teacherList;
    }

    public void setTeacherList(List<Teacher> teacherList) {
        this.teacherList = teacherList;
    }
}

Teacher

package domain;

import java.util.List;

public class Teacher {

    //自有属性
    private Integer tid;
    private String tname;
    private String tsex;
    private Integer tage;
    //关联属性  学生集合
    private List<Student> studentList;


    public Teacher() {
    }

    public Teacher(Integer tid, String tname, String tsex, Integer tage, List<Student> studentList) {
        this.tid = tid;
        this.tname = tname;
        this.tsex = tsex;
        this.tage = tage;
        this.studentList = studentList;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "tid=" + tid +
                ", tname='" + tname + '\'' +
                ", tsex='" + tsex + '\'' +
                ", tage=" + tage +
                ", studentList=" + studentList +
                '}';
    }

    public Integer getTid() {
        return tid;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public String getTsex() {
        return tsex;
    }

    public void setTsex(String tsex) {
        this.tsex = tsex;
    }

    public Integer getTage() {
        return tage;
    }

    public void setTage(Integer tage) {
        this.tage = tage;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
}

StudentDao

public class StudentDao {

    //需要一个SqlSession对象支持所有的JDBC操作
    private SqlSession sqlSession = MyUtil.getSqlSession(true);

    //设计一个方法 根据sid学生编号 查询学生信息+这个学生所学老师的信息
    public Student selectOne(Integer sid){
        return sqlSession.selectOne("selectOne",sid);
    }
    //设计一个方法 查询所有学生信息+每个学生对应的老师信息
    public List<Student> selectAll(){
        return sqlSession.selectList("selectAll");
    }
}

StudentMapper.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="dao.StudentDao">

    <!-- 方式一规则 先给学生对象赋值 再查询学生对应的所有老师对象 -->
    <resultMap id="selectStudent" type="domain.Student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <result property="ssex" column="ssex"></result>
        <result property="sage" column="sage"></result>
        <collection property="teacherList" javaType="list" ofType="domain.Teacher" select="selectTeacherForStudent" column="sid"></collection>
    </resultMap>
    <select id="selectTeacherForStudent" resultType="domain.Teacher">
        select t.* from tea_stu ts inner join teacher t on t.tid = ts.tid where ts.sid = #{sid}
    </select>

    <!-- 方式二规则 所有数据都查询出来 直接赋值 -->
    <!--<resultMap id="selectStudent" type="domain.Student">-->
        <!--<id property="sid" column="sid"></id>-->
        <!--<result property="sname" column="sname"></result>-->
        <!--<result property="ssex" column="ssex"></result>-->
        <!--<result property="sage" column="sage"></result>-->
        <!--<collection property="teacherList" javaType="list" ofType="domain.Teacher">-->
            <!--<id property="tid" column="tid"></id>-->
            <!--<result property="tname" column="tname"></result>-->
            <!--<result property="tsex" column="tsex"></result>-->
            <!--<result property="tage" column="tage"></result>-->
        <!--</collection>-->
    <!--</resultMap>-->

    <!--=====================================================-->
    <select id="selectOne" resultMap="selectStudent">
        <!-- 方式一 先查询学生自己的信息 再查询老师信息进行关联 -->
        <!--select sid,sname,ssex,sage from student where sid = #{sid}-->

        <!-- 方式二 通过三个表格联合查询 将所有数据一次性查出来 直接赋值 -->
        select t.*,s.* from teacher t,tea_stu ts,student s where t.tid = ts.tid and ts.sid = s.sid and s.sid = #{sid}
    </select>
    <select id="selectAll" resultMap="selectStudent">
        <!-- 方式一 先查询学生自己的信息 再查询老师信息进行关联 -->
        select sid,sname,ssex,sage from student
        <!-- 方式二 通过三个表格联合查询 将所有数据一次性查出来 直接赋值 -->
        <!--select t.*,s.* from teacher t,tea_stu ts,student s where t.tid = ts.tid and ts.sid = s.sid-->
    </select>
</mapper>

TeacherDao

public class TeacherDao {

    //需要一个SqlSession对象作为属性 让他负责JDBC操作
    private SqlSession sqlSession = MyUtil.getSqlSession(true);


    //设计一个方法 负责根据tid老师编号 查询一个老师的信息+他所有学生的信息
    public Teacher selectOne(Integer tid){
        return sqlSession.selectOne("selectOne",tid);
    }

    //设计一个方法 查询所有老师和每个老师对应的所有学生
    public List<Teacher> selectAll(){
        return sqlSession.selectList("selectAll");
    }
}

TeacherMapper.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="dao.StudentDao">

    <!-- 方式一规则 两次查询 第二次查询学生的时候 需要两张表格联合 -->
    <resultMap id="selectTeacher" type="domain.Teacher">
        <id property="tid" column="tid"></id>
        <result property="tname" column="tname"></result>
        <result property="tsex" column="tsex"></result>
        <result property="tage" column="tage"></result>
        <collection property="studentList" javaType="list" ofType="domain.Student" select="selectStudentForTeacher" column="tid"></collection>
    </resultMap>
    <select id="selectStudentForTeacher" resultType="domain.Student">
        select s.* from tea_stu ts inner join student s on ts.sid = s.sid where ts.tid = #{tid}
    </select>

    <!-- 方式二规则 一次三表联合查询 将老师和学生信息都获取到了 可以直接赋值 不需要再次查询 -->
    <!--<resultMap id="selectTeacher" type="domain.Teacher">-->
        <!--<id property="tid" column="tid"></id>-->
        <!--<result property="tname" column="tname"></result>-->
        <!--<result property="tsex" column="tsex"></result>-->
        <!--<result property="tage" column="tage"></result>-->
        <!--<collection property="studentList" javaType="list" ofType="domain.Student">-->
            <!--<id property="sid" column="sid"></id>-->
            <!--<result property="sname" column="sname"></result>-->
            <!--<result property="ssex" column="ssex"></result>-->
            <!--<result property="sage" column="sage"></result>-->
        <!--</collection>-->
    <!--</resultMap>-->

    <!--=============================================================-->
    <select id="selectOne" resultMap="selectTeacher">
        <!-- 方式一 先查询老师信息 再查询学生信息-->
        <!--select tid,tname,tsex,tage from teacher where tid = #{tid}-->

        <!-- 方式二 一次性将一个老师和学生的所有信息都查出来 -->
        select t.*,s.* from teacher t inner join tea_stu ts on t.tid = ts.tid inner join student s on ts.sid = s.sid where t.tid = #{tid}
    </select>

    <select id="selectAll" resultMap="selectTeacher">
        <!-- 方式一 先查询老师信息 再查询学生信息-->
        select tid,tname,tsex,tage from teacher

        <!-- 方式二 一次性将一个老师和学生的所有信息都查出来 -->
        <!--select t.*,s.* from teacher t inner join tea_stu ts on t.tid = ts.tid inner join student s on ts.sid = s.sid-->
    </select>
</mapper>

configuration.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/testmybatis?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--<mapper resource="mapper/TeacherMapper.xml"></mapper>-->
        <mapper resource="mapper/StudentMapper.xml"></mapper>
    </mappers>
</configuration>

相关文章

网友评论

      本文标题:MyBatis联合查询(多对多)

      本文链接:https://www.haomeiwen.com/subject/ovgavktx.html