MyBatis 조회 시 속성 중 한 쌍 이상 처리 (한 데이터 가 여러 데이터 에 대응)

22826 단어 Javamybatis자바
목차
  • 초기 준비
  • 조회 에 따라 끼 워 넣 기 처리
  • 결과 에 따라 끼 워 넣 기 처리
  • 소결
  • 다 대 일 처리:https://blog.csdn.net/weixin_44953227/article/details/112786245
    전기 준비
    데이터 시트
    CREATE TABLE `teacher`(
      id INT(10) NOT NULL,
      `name` VARCHAR(30) DEFAULT NULL,
      PRIMARY KEY (id)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    
    INSERT INTO `teacher`(id,`name`) VALUES(1,'  ');
    
    CREATE TABLE `student`(
      id INT(10) NOT NULL,
      `name` VARCHAR(30) DEFAULT NULL,
      `tid` INT(10) DEFAULT NULL,
      PRIMARY KEY(id),
      KEY `fktid` (`tid`),
      CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    INSERT INTO student(`id`,`name`,`tid`) VALUES(1,'  ',1);
    INSERT INTO student(`id`,`name`,`tid`) VALUES(2,'  ',1);
    INSERT INTO student(`id`,`name`,`tid`) VALUES(3,'  ',1);
    INSERT INTO student(`id`,`name`,`tid`) VALUES(4,'  ',1);
    INSERT INTO student(`id`,`name`,`tid`) VALUES(5,'  ',1);
    

    학생 실체 클래스
    public class Student {
         
        private int id;
        private String name;
    
        private int tid;
    }
    

    Teacher 실체 클래스
    public class Teacher {
         
        private int id;
        private String name;
    
        private List<Student> students;
    }
    

    인터페이스
    public interface TeacherMapper {
         
        //        -    
        Teacher getTeacherById(int id);
    
        //       
        Teacher getTeacherResultById(int id);
    }
    

    조회 에 따라 끼 워 넣 기 처리 하 다
  • collection: 집합 처리
  • property: 실체 클래스 의 속성 필드
  • column: 조회 결과 에서 하위 조회 필드
  • 에 전달 해 야 합 니 다.
  • javaType: 지정 한 속성의 유형
  • of Type: 지정 집합 중 범 형
  • select: 하위 쿼 리 SQL
  • <mapper namespace="com.pro.dao.TeacherMapper">
        
        <resultMap id="TeacherMap" type="com.pro.pojo.Teacher">
            <result property="id" column="id"/>
            <collection property="students" column="id" javaType="List" ofType="com.pro.pojo.Student" select="getStudentById"/>
        resultMap>
    
        <select id="getTeacherById" resultMap="TeacherMap">
            select * from teacher where id = #{id}
        select>
    
        
        <select id="getStudentById" resultType="com.pro.pojo.Student">
            select * from student where tid = #{id}
        select>
    mapper>
    

    결과 에 따라 끼 워 넣 기 처리 하 다
  • collection: 집합 처리
  • javaType: 지정 한 속성의 유형
  • of Type: 지정 집합 중 범 형
  • <mapper namespace="com.pro.dao.TeacherMapper">
        
        <resultMap id="TeacherResult" type="com.pro.pojo.Teacher">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
            
            <collection property="students" javaType="List" ofType="com.pro.pojo.Student">
                <result property="id" column="sid"/>
                <result property="name" column="sname"/>
            collection>
        resultMap>
    
        <select id="getTeacherResultById" resultMap="TeacherResult">
            select t.*, s.id sid, s.name sname  from teacher t, student s where t.id = #{id} and t.id = s.tid
        select>
    mapper>
    

    작은 매듭
  • 관련 - association 처리 다 대 일
  • 집합 - collection 처리 한 쌍 이상
  • javaType & ofType
  • javaType: 실체 클래스 의 속성 을 지정 하 는 형식
  • ogType: 집합 중의 유형, 일반적인 제약 유형
  • 을 지정 합 니 다.

    좋은 웹페이지 즐겨찾기