java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Mybatis Collection属性

Mybatis使用Collection属性的示例代码

作者:保加利亚的风

本文主要介绍了Mybatis使用Collection属性的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

这篇文章实现一下不使用left join等连接关键字来实现Mybatis的联表查询功能。包括json类型数据映射到Java实体类中。

库表

父表db1_json

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zIuG3a9a-1689752532289)(C:\Users\Admin\AppData\Roaming\marktext\images\2023-07-19-15-19-09-image.png)]

子表db1_json_attach,子表parent_id对应副本的id

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LMlft32m-1689752532290)(C:\Users\Admin\AppData\Roaming\marktext\images\2023-07-19-15-19-47-image.png)]

实体类

新建Db1JsonDTO数据传递对象实体。

@Data
public class Db1JsonDTO {
    //父表id
    private Long id;
    //父表信息
    private JSONObject info;
    //子表数据
    private List<Db1JsonAttach> attaches;
}

查看子表实体属性

@TableName(value ="db1_json_attach")
@Data
public class Db1JsonAttach implements Serializable {
    private Long parentId;
    @TableId(type = IdType.ID_WORKER)
    private Long id;
    @TableField(typeHandler = JacksonTypeHandler.class)
    private JSONObject info;
    private static final long serialVersionUID = 1L;
}

Mapper.xml

父xml处理,sql没什么好看的,看一下<resultMap>中各个属性吧。

<resultMap id="Db1JsonDTOResultMap" type="com.it.dto.Db1JsonDTO">
    <result column="info" property="info" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
    <collection property="attaches" column="{parentId = id}"
                select="com.it.mapper.Db1JsonAttachMapper.getAttachList"/>
</resultMap>
<select id="selectDb1JsonList" resultMap="Db1JsonDTOResultMap">
    select id,info from db1_json
</select>

子xml处理

<resultMap id="BaseResultMap" type="com.it.entity.Db1JsonAttach">
        <id property="parentId" column="parent_id" jdbcType="BIGINT"/>
        <result property="id" column="id" jdbcType="BIGINT"/>
    <result property="info" column="info"  typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
</resultMap>
<select id="getAttachList" resultMap="BaseResultMap">
    select * from db1_json_attach where parent_id = #{parentId}
</select>

注:Mapper中不需要定义查询方法,只在XML中定义即可。

测试

看一下测试结果

[
  {
    "id": 1681557955655049218,
    "info": {
      "address": "洛杉矶",
      "name": "科比",
      "hobby": "直升机"
    },
    "attaches": [
      {
        "parentId": 1681557955655049218,
        "id": 1681557956250640385,
        "info": {
          "address": "洛杉矶",
          "name": "科比",
          "hobby": "直升机"
        }
      }
    ]
  },
  {
    "id": 1681558109766361089,
    "info": {
      "address": "美国",
      "name": "蔡徐坤",
      "hobby": "唱跳rap篮球"
    },
    "attaches": [
      {
        "parentId": 1681558109766361089,
        "id": 1681558109766361090,
        "info": {
          "address": "美国",
          "name": "蔡徐坤",
          "hobby": "唱跳rap篮球"
        }
      }
    ]
  },
  {
    "id": 1681558181665120257,
    "info": {
      "address": "理塘",
      "name": "丁真",
      "hobby": "测码"
    },
    "attaches": [
      {
        "parentId": 1681558181665120257,
        "id": 1681558181732229122,
        "info": {
          "address": "理塘",
          "name": "丁真",
          "hobby": "测码"
        }
      }
    ]
  }
]

总结

这个方式的联表查询用的不算太多,但是在一些特殊情况可以使用这种方式来完成查询子表的某一下数据集合。

到此这篇关于Mybatis使用Collection属性的示例代码的文章就介绍到这了,更多相关Mybatis Collection属性内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文