zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    【LeetCode】543. 二叉树的直径
    红色的眼睛黑色的心
    WinForm
    Windows地址栏的妙用
    C#
    WPF
    配置Notepad++万能调试
    盗取连接你wifi的人的qq
    Windows去除开始菜单图标背景
    解决Windows下文件无法删除的问题
  • 原文地址:https://www.cnblogs.com/xp2h/p/12364057.html
Copyright © 2011-2022 走看看