要做的就是这个sql操作

P20 20、多对一的处理

 

 

P20 20、多对一的处理

 

P20 20、多对一的处理

 

P20 20、多对一的处理

执行

P20 20、多对一的处理

 

 

### 按照查询嵌套处理

```xml

<!--

    思路:

        1. 查询所有的学生信息

        2. 根据查询出来的学生的tid,寻找对应的老师!  子查询

    -->

 

<select id="getStudent" resultMap="StudentTeacher">

    select * from student

</select>

 

<resultMap id="StudentTeacher" type="Student">

    <result property="id" column="id"/>

    <result property="name" column="name"/>

    <!--复杂的属性,我们需要单独处理 对象: association 集合: collection -->

    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>

</resultMap>

 

<select id="getTeacher" resultType="Teacher">

    select * from teacher where id = #{id}

</select>

```

 

接口方法编写

P20 20、多对一的处理

 

P20 20、多对一的处理

结果

P20 20、多对一的处理

 

 

### 按照结果嵌套处理

```xml

<!--按照结果嵌套处理-->

<select id="getStudent2" resultMap="StudentTeacher2">

    select s.id sid,s.name sname,t.name tname

    from student s,teacher t

    where s.tid = t.id;

</select>

 

<resultMap id="StudentTeacher2" type="Student">

    <result property="id" column="sid"/>

    <result property="name" column="sname"/>

    <association property="teacher" javaType="Teacher">

        <result property="name" column="tname"/>

    </association>

</resultMap>

```

P20 20、多对一的处理

 

注:

P20 20、多对一的处理

上面的property就是这里的

P20 20、多对一的处理

注end

 

测试

P20 20、多对一的处理

结果

P20 20、多对一的处理

 

 

回顾Mysql 多对一查询方式:

- 子查询

- 联表查询

 

子查询就相当于

select id,name,tid from student where tid = (select…)

P20 20、多对一的处理

 

 

 

 

 

 

 

 

相关文章:

  • 2021-06-16
  • 2023-01-03
  • 2021-07-18
  • 2021-12-29
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2022-02-07
  • 2021-11-03
相关资源
相似解决方案