mybatis 基础

collocation :多对一   association :一对多

类似于一个班级对应多个学生 ,多个学生对应一个班级

public class Student implements Serializable {
	private Integer id;
	private String name;
	private String sex;
	private Integer age;
        //学生与班级是多对一的关系
	private Clazz clazz;
}
public class Clazz implements Serializable{
	private Integer id;
	private String code;
	private String name;
        //班级与学生是一对多的关系
	private List<Student> students;
}

<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>
<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>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值