MyBatis 一对多关联查询

sqlxml文件

<?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="ClassStudent">

    <!-- 1.联表查询SELECT * from class c ,teacher t where c.t_id = t.t_id and c.c_id=1 -->
    <select id="getClass" parameterType="int" resultMap="getClassMap">
        SELECT *
        from class c
        ,student s where c.c_id = s.c_id and c.c_id = #{id}
    </select>
    <resultMap type="com.stone.bean.ClassT" id="getClassMap">
        <id property="id" column="c_id" />
        <result property="name" column="c_name" />
        <collection property="list" ofType="com.stone.bean.Student">
            <id property="id" column="s_id" />
            <result property="name" column="s_name" />
        </collection>
    </resultMap>
    <!-- 2.查询两次select * from class where c_id = 1 ; SELECT * from teacher where 
        t_id = 1 -->

    <select id="getClass2" resultMap="getClass2Map">
        select * from class where c_id
        =#{id}
    </select>
    <select id="getTeacher" parameterType="int" resultType="com.stone.bean.Teacher">
        select
        t_id id,t_name name
        from teacher where t_id=#{id}
    </select>
    <select id="getStudents" parameterType="int" resultType="com.stone.bean.Student">
        select
        S_id id,s_name name from student where c_id=#{id}
    </select>
    <resultMap type="com.stone.bean.ClassT" id="getClass2Map">
        <id property="id" column="c_id" />
        <result property="name" column="c_name" />
        <association property="teacher" column="t_id" select="getTeacher">
        </association>
        <collection property="list" column="c_id" select="getStudents"></collection>
    </resultMap>
  <!--collection 可以进行嵌套-->
  <resultMap type="person" id="selectMulti" extends="BaseResultMap">
    <collection property="orderList" ofType="Orders">
      <id ...>
      <result ...>
      <collection property="detailList" ofType="OrderDetail">
        <id ...>
        <result ...>
      </collection>
    </collection>
  </resultMap>
</mapper>

 

javabean

package com.stone.bean;

import java.util.List;

public class ClassT {

    private int id;
    private String name;
    private Teacher teacher;
    private List<Student> list;
    
    
    public ClassT(int id, String name, Teacher teacher) {
        super();
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }
    public ClassT() {
        super();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    @Override
    public String toString() {
        return "ClassT [id=" + id + ", name=" + name + ", teacher=" + teacher
                + ", list=" + list + "]";
    }
    public List<Student> getList() {
        return list;
    }
    public void setList(List<Student> list) {
        this.list = list;
    }

    
}
package com.stone.bean;

public class Student {

    private int id;
    private String name;
    public Student(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Student() {
        super();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + "]";
    }
    
}
package com.stone.bean;

public class Teacher {
    private int id;
    private String name;
    public Teacher(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Teacher() {
        super();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + "]";
    }
    
}

 

test

package com.stone.dao;

import org.apache.ibatis.session.SqlSession;

import com.stone.bean.ClassT;
import com.stone.db.DBAccess;

public class DBDaoClassStudent {

    public static void main(String[] args) {
        DBAccess dbAccess = new DBAccess();
        SqlSession sqlSession = null;
        try {
            sqlSession = dbAccess.getSqlSession();
            String statement = "ClassStudent.getClass";
            Object parameter = 1;
            // 通过sqlSession执行SQL语句;
            ClassT class1 = sqlSession.selectOne(statement, parameter);
            System.out.println(class1);
            System.out.println("=======================");
            statement = "ClassStudent.getClass2";
            ClassT class2 = sqlSession.selectOne(statement, parameter);
            System.out.println(class2);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值