Mybatis 에서 collection 과 association 의 사용 차이 점 에 대한 상세 한 설명

최근 에 collection 과 association 을 헷 갈 리 게 하고 있 기 때문에 자신의 기억 을 강화 하기 위해 관 계 를 없 애 는 것 은 총 결 일 뿐이다.
1.관련-association
2.집합-콜렉션
예 를 들 어 User.java 와 Card.java 두 가지 종류 가 동시에 있 습 니 다.
User.java 는 다음 과 같 습 니 다.

public class User{

private Card card_one;

private List<Card> card_many;

}

비 추 는 카드one 속성 시 association 태그 로 카드 매 핑many 시 collection 탭 을 사용 합 니 다.
그래서 association 은 1 대 1 과 다 대 1 에 사용 되 고 collection 은 1 대 다 의 관계 에 사용 된다.
다음은 예 를 들 어 설명 하 겠 습 니 다.
association-일대일
사람과 신분증 의 관계
다음은 포 조.

public class Card implements Serializable{
 private Integer id;
 private String code;
//  set get  .
}

public class Person implements Serializable{
 private Integer id;
 private String name;
 private String sex;
 private Integer age;
 //            
 private Card card;
//  set/get  .
}
다음은 mapper 와 실 현 된 인터페이스 입 니 다.

package com.glj.mapper;

import com.glj.poji.Card;

public interface CardMapper {
 Card selectCardById(Integer id);
}


<?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="com.glj.mapper.CardMapper">
 <select id="selectCardById" parameterType="int" resultType="com.glj.poji.Card">
 select * from tb_card where id = #{id} 
 </select>
</mapper>

package com.glj.mapper;
 
import com.glj.poji.Person;
 
public interface PersonMapper {
 Person selectPersonById(Integer id);
}

<?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="com.glj.mapper.PersonMapper">
 <resultMap type="com.glj.poji.Person" id="personMapper">
 <id property="id" column="id"/>
 <result property="name" column="name"/>
 <result property="sex" column="sex"/>
 <result property="age" column="age"/>
 <association property="card" column="card_id" 
  select="com.glj.mapper.CardMapper.selectCardById"
  javaType="com.glj.poji.Card">
 </association>
 </resultMap>
 <select id="selectPersonById" parameterType="int" resultMap="personMapper">
 select * from tb_person where id = #{id}
 </select>
</mapper>
PersonMapper.xml 는 association 의 단계별 조회 도 사용 합 니 다.
도리 가 같 으 면 다 대 일 도 마찬가지다.
그 포 조 에 프 라이 빗 카드 만 나 오 면one;
association 사용
collection 한 쌍 은 association 과 의 다 대 일 관계 입 니 다.
학생 과 학급 의 일대일로 많은 예
pojo 류

package com.glj.pojo;
 
import java.io.Serializable;
import java.util.List;
 
public class Clazz implements Serializable{
 private Integer id;
 private String code;
 private String name;
    //            
 private List<Student> students;
//  set/get  
}

package com.glj.pojo;

import java.io.Serializable;

public class Student implements Serializable {
 private Integer id;
 private String name;
 private String sex;
 private Integer age;
    //            
 private Clazz clazz;
//  set/get  
}

<?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="com.glj.mapper.ClazzMapper">
 <select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
 select * from tb_clazz where id = #{id}
 </select>
 <resultMap type="com.glj.pojo.Clazz" id="clazzResultMap">
 <id property="id" column="id"/>
 <result property="code" column="code"/>
 <result property="name" column="name"/>
 <!-- property:          , ofType:            -->
 <collection property="students" ofType="com.glj.pojo.Student"
 column="id" javaType="ArrayList"
 fetchType="lazy" select="com.glj.mapper.StudentMapper.selectStudentByClazzId">
  <id property="id" column="id"/>
  <result property="name" column="name"/>
  <result property="sex" column="sex"/>
  <result property="age" column="age"/>
 </collection>
 </resultMap>
</mapper>

package com.glj.mapper;

import com.glj.pojo.Clazz;

public interface ClazzMapper {
 Clazz selectClazzById(Integer id);
}
ClazzMapper 는 집합-collection 을 사용 하여 한 쌍 이 많 고 한 반 이 여러 학생 을 마주 하고 있 습 니 다.

<?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="com.glj.mapper.StudentMapper">
 <select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
 select * from tb_clazz c,tb_student s where c.id = s.id and s.id = #{id}
 </select>
 <select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">
 select * from tb_student where clazz_id = #{id}
 </select>
 <resultMap type="com.glj.pojo.Student" id="studentResultMap">
 <id property="id" column="id"/>
 <result property="name" column="name"/>
 <result property="sex" column="sex"/>
 <result property="age" column="age"/>
 <association property="clazz" javaType="com.glj.pojo.Clazz">
  <id property="id" column="id"/>
  <result property="code" column="code"/>
  <result property="name" column="name"/>
 </association>
 </resultMap>
</mapper>

package com.glj.mapper;

import com.glj.pojo.Student;

public interface StudentMapper {
 Student selectStudentById(Integer id); 
}
Student Mapper 는 학급 과 일대일 관 계 를 가지 기 때문에 관련-association 을 사 용 했 습 니 다.
네,앞으로 도 두 사람의 관 계 를 기억 하지 못 할 때 지금 정리 한 자신 에 게 감사 할 수 있 기 를 바 랍 니 다.
my batis 형식 별명 그림 을 첨부 합 니 다.

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기