例如班级表(Clazz)-和学生表(Student)的关系是一对多的关系,我们希望在加载一个班级的时候,能自动加载这个班级下的学生列表。
Java实体类如下(学生实体类省略):
@TableName("t_class") public class Clazz implements Serializable { @TableId private Long id; private Date createDate;// private Date updateDate; private String name;//班级名称 private List<Student> students; }
Mapper.xml:
<?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.terrynow.test.mapper.ClazzMapper"> <select id="selectClazzById" parameterType="int" resultMap="clazzResultMap"> select * from t_clazz where id = #{id} </select> <resultMap type="com.terrynow.test.pojo.Clazz" id="clazzResultMap"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="students" ofType="com.terrynow.test.pojo.Student" column="id" javaType="ArrayList"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> </collection> </resultMap> </mapper>
文章评论