当前位置:   article > 正文

六步学会mybatis---------第四章:resultMap处理 多表联合之联表查询_extends=、baseresultmap

extends=、baseresultmap
<resultMap>标签下的子标签
<association>处理单个对象数据
<collection>处理多个对象
  • 1
  • 2
  • 3

1. 一对一

Student类

public class Student {
   
    private Integer id;
    private String name;
    private Integer age;
    // 一对一
    private Computer computer;
    //...省略
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Computer类

public class Computer {
   
    private Integer id;
    private String moudle;
    }
  • 1
  • 2
  • 3
  • 4
  • 5

数据库里学生和电脑信息在两张表里存储
在这里插入图片描述
在这里插入图片描述
要求查出学生时,把电脑信息也查出来

<select id="selAll" resultMap="StudentResultMap">
        select s.`s_id`,s.`s_name`,s.`s_age`,s.`c_id`,c.`c_moudle`
            from student s inner join computer c where s.`c_id`=c.`c_id`
    </select>

    <!--一对一-->
    <resultMap id="StudentResultMap" type="Student">
        <id column="s_id" property="id"></id>
        <result column="s_name" property="name"></result>
        <result column="s_age" property="age"></result>
        <association property="computer" javaType="Computer">
            <id column="c_id" property="id"></id>
            <result column="c_moudle" property=
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/241544
推荐阅读
相关标签
  

闽ICP备14008679号