最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis使用Collection屬性的示例代碼

 更新時間:2023年07月24日 09:54:10   作者:保加利亞的風(fēng)  
本文主要介紹了Mybatis使用Collection屬性的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

這篇文章實現(xiàn)一下不使用left join等連接關(guān)鍵字來實現(xiàn)Mybatis的聯(lián)表查詢功能。包括json類型數(shù)據(jù)映射到Java實體類中。

庫表

父表db1_json

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-zIuG3a9a-1689752532289)(C:\Users\Admin\AppData\Roaming\marktext\images\2023-07-19-15-19-09-image.png)]

子表db1_json_attach,子表parent_id對應(yīng)副本的id。

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-LMlft32m-1689752532290)(C:\Users\Admin\AppData\Roaming\marktext\images\2023-07-19-15-19-47-image.png)]

實體類

新建Db1JsonDTO數(shù)據(jù)傳遞對象實體。

@Data
public class Db1JsonDTO {
    //父表id
    private Long id;
    //父表信息
    private JSONObject info;
    //子表數(shù)據(jù)
    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>
  • id:自定義id,在SQL查詢后使用resultMap屬性并指定id返回。
  • type:返回字段對應(yīng)的實體類,也可以是DTO,實體需要有Getter、Setter方法。
  • <result column:數(shù)據(jù)庫中的字段名稱。
  • <result property:與數(shù)據(jù)庫字段對應(yīng)的屬性名。
  • <result typeHandler:對特殊類型進行處理,例如當(dāng)前為json類型,指定將返回的結(jié)果進行轉(zhuǎn)化后映射到實體屬性中。
  • <collection property:父實體中的list集合數(shù)據(jù),即為子實體類的集合。
  • <collection column:id字段賦值,將父id字段賦值給子id。
  • <collection select :指定子xmlSQL查詢的方法id。

子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中定義即可。

測試

看一下測試結(jié)果

[
  {
    "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": "測碼"
        }
      }
    ]
  }
]

總結(jié)

這個方式的聯(lián)表查詢用的不算太多,但是在一些特殊情況可以使用這種方式來完成查詢子表的某一下數(shù)據(jù)集合。

到此這篇關(guān)于Mybatis使用Collection屬性的示例代碼的文章就介紹到這了,更多相關(guān)Mybatis Collection屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

大余县| 正定县| 新巴尔虎右旗| 兴安县| 宁国市| 民权县| 库伦旗| 普兰店市| 徐水县| 教育| 通州区| 庄河市| 彩票| 富宁县| 伊吾县| 临颍县| 泗水县| 本溪市| 城口县| 黑水县| 凤城市| 新巴尔虎左旗| 满洲里市| 微博| 大港区| 聊城市| 南投市| 绥德县| 福贡县| 鄂托克前旗| 探索| 安溪县| 贵州省| 安阳县| 宜章县| 平罗县| 康乐县| 松阳县| 塘沽区| 平利县| 达拉特旗|