Mybatis 结果集映射

结果映射(resultMap)

  • constructor - 用于在实例化类时,注入结果到构造方法中(一般不用)
    • idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能
    • arg - 将被注入到构造方法的一个普通结果
  • id – 一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能
  • result – 注入到字段或 JavaBean 属性的普通结果
  • association – 一个复杂类型的关联;许多结果将包装成这种类型
    • 嵌套结果映射 – 关联本身可以是一个 resultMap 元素,或者从别处引用一个
  • collection – 一个复杂类型的集合
    • 嵌套结果映射 – 集合本身可以是一个 resultMap 元素,或者从别处引用一个
  • discriminator – 使用结果值来决定使用哪个 resultMap(一般不用)
    • case – 基于某些值的结果映射
      • 嵌套结果映射 – case 本身可以是一个 resultMap 元素,因此可以具有相同的结构和元素,或者从别处引用一个

  id和result的属性 - 映射普通类型

    • property - 映射到列结果的字段或属性。如果用来匹配的 JavaBean 存在给定名字的属性,那么它将会被使用
    • column - 数据库中的列名,或者是列的别名
    • javaType - 一个 Java 类的完全限定名,或一个类型别名(关于内置的类型别名,可以参考上面的表格)。 如果你映射到一个 JavaBean,MyBatis 通常可以推断类型

  association的属性 - 映射对象

    • property - 用来匹配的 JavaBean 类型
    • javaType - 用来匹配的查询结果对应的java类型
    • column - 如果需要嵌套查询,用来传参,一般和select一起用
    • select - 嵌套查询时传select的id

  collection的属性 - 映射集合

    • property - 用来匹配的JavaBean类型
    • javaType - 用来匹配查询结果对应的java类型
    • ofType - 映射集合的泛型
    • select - 嵌套查询时的select ID
    • column - 如果需要嵌套查询,用来传参,一般和select一起用

 

一对多查询示例:

 <!--按照查询嵌套处理-->
    <select id="getList" resultMap="studentTeacher">
        select * from student
    </select>

    <resultMap id="studentTeacher" type="student">
        <id property="id" column="id"></id>
        <result column="name" property="name"></result>
        <association property="teacher" column="tid" javaType="teacher" select="getTeacher"></association>
    </resultMap>

    <select id="getTeacher" resultType="teacher">
        select * from teacher where id = #{tid}
    </select>

    <!--按照结果嵌套处理-->
    <select id="getList" resultMap="studentTeacher" >
        select s.id as sid ,s.name as sname,t.id as tid,t.name as tname from student as s inner join teacher as t
        on s.id = t.id
    </select>

    <resultMap id="studentTeacher" type="student">
        <id property="id" column="sid"></id>
        <result column="sname" property="name"></result>
        <association property="teacher" javaType="teacher" >
            <result property="id" column="tid"></result>
            <result column="tname" property="name"></result>
        </association>
    </resultMap>

 多对多查询示例:

 <!--按照结果嵌套处理-->
    <select id="getList" resultMap="teacherStudent">
        select t.id tid,t.name tname,s.id sid,s.name sname from teacher t,student s where t.id = s.tid
    </select>
    <resultMap id="teacherStudent" type="teacher">
        <id property="id" column="tid"></id>
        <result column="tname" property="name"></result>

        <collection property="students" ofType="student">
            <id property="id" column="sid"></id>
            <result column="sname" property="name"></result>
        </collection>
    </resultMap>

    <!--按照查询嵌套结果-->
    <select id="getList" resultMap="teacherStudent">
        select * from mybatis.teacher
    </select>
    <resultMap id="teacherStudent" type="teacher">
        <collection property="students" javaType="ArrayList" ofType="student" select="getStudents" column="id" >
        </collection>
    </resultMap>
    <select id="getStudents" resultType="student">
        select * from mybatis.student where tid = #{tid}
    </select>

猜你喜欢

转载自www.cnblogs.com/xp2h/p/12364057.html