当前位置:   article > 正文

MyBatis多表查询——resultType——resultMap_mybatis多表查询resultmap

mybatis多表查询resultmap

使用多表查询,就得将两个实体类放在一块:

  1. package com.quxiao.mybatis.pojo;
  2. public class textAndUser {
  3. private Text text;
  4. private User user;
  5. @Override
  6. public String toString() {
  7. String str=user.getId()+" "+user.getName()+" "+user.getAge()+" "+user.getGender()+" "+user.getEmail()+" "+text.getTime();
  8. return str;
  9. }
  10. }

这是我自己想的一个办法,然后来说说我们这次的两个多表查询的案例:

既然是多表查询,就得关联多个表,所以sql就和以前有一些不同了,而且要使用别名的方式将每个字段给定别名,别名为类名.属性

  1. <select id="selectTextAndUser" resultType="com.quxiao.mybatis.pojo.textAndUser">
  2. select u.id "user.id", u.name "user.name", u.gender "user.gender",
  3. t.time "text.time"
  4. from user u
  5. join text t on u.id = t.id
  6. where t.id = #{id};
  7. </select>

 返回值直接为多个实体类的关联类:selectTextAndUser

 然后另一种办法就是将所有的列一一对应:

  1. <select id="selectTextAndUser" resultMap="userAndtextMap">
  2. select u.id, u.name, u.gender, t.time
  3. from user u
  4. join text t on u.id = t.id
  5. where t.id = #{id};
  6. </select>
  7. <resultMap id="userAndtextMap" type="com.quxiao.mybatis.pojo.textAndUser">
  8. <result property="user.id" column="id"></result>
  9. <result property="user.name" column="name"></result>
  10. <result property="user.age" column="age"></result>
  11. <result property="user.gender" column="gender"></result>
  12. <result property="user.email" column="email"></result>
  13. <result property="text.time" column="time"></result>
  14. </resultMap>

这里有几点要注意一下:

property:表示接下来你在sql中将要使用的字段的key

column: 则可以理解为value,属于数据库的字段名,必须相同!

一对多关系:

 

非常经典的绑定关系展示,现在都是先查询父级,然后通过父级id查询子级。

其实可以通过mybatis的一对多来直接查询所有的数据:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.stock1Server.mapper.UserMapper">
  6. <resultMap id="getUserMap" type="com.stock1Server.pojo.vo.UserVO">
  7. <result property="id" column="id"/>
  8. <result property="userName" column="user_name"/>
  9. <result property="userCode" column="user_code"/>
  10. <collection property="workIds" javaType="ArrayList" ofType="com.stock1Server.entity.WorktableEntity">
  11. <result property="i1" column="i1"/>
  12. <result property="i2" column="i2"/>
  13. <result property="i3" column="i3"/>
  14. <result property="i4" column="i4"/>
  15. </collection>
  16. </resultMap>
  17. <select id="getUser" resultMap="getUserMap">
  18. select user.*,
  19. worktable.*
  20. from user
  21. left join worktable on worktable.user_id = user.id
  22. </select>
  23. </mapper>

返回就是这样:

  1. [
  2. {
  3. "id": 14,
  4. "userName": "aaa",
  5. "userCode": "111",
  6. "workIds": [
  7. {
  8. "i1": 2,
  9. "i2": "多了一个表",
  10. "i3": "2024-02-21T14:48:27.000+00:00",
  11. "i4": 1,
  12. "data": "dsaaa",
  13. "userId": 14
  14. },
  15. {
  16. "i1": 1,
  17. "i2": "20212904",
  18. "i3": "2024-02-20T14:48:27.000+00:00",
  19. "i4": 1,
  20. "data": "dsaaa",
  21. "userId": 14
  22. }
  23. ]
  24. },
  25. {
  26. "id": 15,
  27. "userName": "11",
  28. "userCode": "er",
  29. "workIds": []
  30. },
  31. {
  32. "id": 16,
  33. "userName": "11",
  34. "userCode": "aaa",
  35. "workIds": []
  36. }
  37. ]

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/830564
推荐阅读
相关标签
  

闽ICP备14008679号