Mybatis 에서 collection 과 association 의 사용 차이 점 에 대한 상세 한 설명
7592 단어 Mybatiscollectionassociation
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 형식 별명 그림 을 첨부 합 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SpringMVC와 Mybatis 집합은 호출 저장 프로세스, 사무 제어 실례를 실현한다SSM 프레임워크에서 호출 데이터베이스의 저장 프로세스와 사무 제어에 자주 사용되는데 다음은 증빙서류를 저장하는 예를 들어 소개한다. 1. Oracle에 저장된 프로세스 코드는 다음과 같습니다(주요 논리는 증빙 서류...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.