MyBatis--resultMap

逃离我推掉我的手 2023-06-07 12:25 101阅读 0赞

resultMap:自定义结果集映射规则

1.级联属性封装结果集

 1.1 方式一

  1. <mapper namespace="dao.StudentMapper">
  2. <resultMap type="bean.Student" id="MyStudent">
  3. <!-column:指定哪一列,property:指定对应的javaBean属性-->
  4. <id column="student_id" property="id"/>
  5. <!-- 定义普通列封装规则 -->
  6. <result column="student_name" property="studentName"/>
  7. <!-- 其他不指定的列会自动封装,建议都写上 -->
  8. <result column="sex" property="sex"/>
  9. <!--联合查询:级联属性封装结果集-->
  10. <result column="school_id" property="school.id"/>
  11. <result column="school_name" property="school.schoolName"/>
  12. </resultMap>
  13. <select id="getStudent" resultMap="MyStudent">
  14. SELECT * FROM student,school WHERE student_id=#{
  15. id} AND student.school_name=school.school_name
  16. </select>
  17. </mapper>

 1.2 方式二(association)

  1. <mapper namespace="dao.StudentMapper">
  2. <resultMap type="bean.Student" id="MyStudent2">
  3. <id column="student_id" property="id"/>
  4. <result column="student_name" property="studentName"/>
  5. <result column="sex" property="sex"/>
  6. <!-- 使用association定义关联的单个对象的封装规则-->
  7. <association property="school" javaType="bean.School">
  8. <id column="school_id" property="id"/>
  9. <result column="school_name" property="schoolName"/>
  10. </association>
  11. </resultMap>
  12. <select id="getStudent" resultMap="MyStudent2">
  13. SELECT * FROM student,school WHERE student_id=#{
  14. id} AND student.school_name=school.school_name
  15. </select>
  16. </mapper>

2.分步查询

  根据学生的学号查询学校的信息
  分步–>1.根据学生学号查询学校姓名
    —>2.根据学校姓名查询学校信息

  1. <mapper namespace="dao.StudentMapper">
  2. <resultMap id="MyStudent3" type="bean.Student">
  3. <id column="student_id" property="id"/>
  4. <result column="student_name" property="studentName"/>
  5. <result column="sex" property="sex"/>
  6. <!-- association定义关联对象的封装规则
  7. select:表明当前属性是调用select指定的方法查出的结果
  8. column:指定将哪一列的值传给这个方法
  9. 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
  10. -->
  11. <association property="school" select="dao.SchoolMapper.getSchool" column="school_name"></association>
  12. </resultMap>
  13. <select id="getStudent2" resultMap="MyStudent3">
  14. SELECT * FROM student WHERE student_id=#{
  15. id}
  16. </select>
  17. </mapper>

3.延迟加载

  1. 延迟加载(懒加载):(按需加载)
  2. Student==>School
  3. 查询Student对象的时候,都将一起查询出来。
  4. 学校信息在我们使用的时候再去查询;
  5. 分段查询的基础之上在mybatis-config.xml加上两个配置:
  6. <settings>
  7. <!--是否延迟加载 -->
  8. <setting name="lazyLoadingEnabled" value="true"/>
  9. <!--是否立即全部加载-->
  10. <setting name="aggressiveLazyLoading" value="false"/>
  11. </settings>

4.collection标签定义的封装规则

  1. <!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 -->
  2. <resultMap id="MySchool2" type="bean.School">
  3. <id column="school_id" property="id"/>
  4. <result column="school.school_name" property="schoolName"/>
  5. <!-- collection定义关联集合类型的属性的封装规则 ofType:指定集合里面元素的类型 -->
  6. <collection property="students" ofType="bean.Student">
  7. <!-- 定义这个集合中元素的封装规则 -->
  8. <id column="student_id" property="id"/>
  9. <result column="student_name" property="studentName"/>
  10. <result column="sex" property="sex"/>
  11. </collection>
  12. </resultMap>
  13. 扩展:多列的值传递过去: 将多列的值封装map传递;
  14. column="{key1=column1,key2=column2}"
  15. <association property="school" select="dao.SchoolMapper.getSchool" column="{schoolName=school_name}"></association>
  16. fetchType="lazy":表示使用延迟加载; - lazy:延迟 - eager:立即

5.鉴别器(了解)
  鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为

  1. <resultMap id="MyStudent4" type="bean.Student">
  2. <id column="student_id" property="id"/>
  3. <result column="student_name" property="studentName"/>
  4. <!--column:指定判定的列名,javaType:列值对应的java类型 -->
  5. <discriminator javaType="string" column="student_name">
  6. <case value="litao" resultType="bean.Student">
  7. <result column="sex" property="studentName"/>
  8. </case>
  9. </discriminator>
  10. </resultMap>

 如果查询结果的student_name是litao则使其sex映射到student_name

发表评论

表情:
评论列表 (有 0 条评论,101人围观)

还没有评论,来说两句吧...

相关阅读