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

Mybatis關(guān)聯(lián)查詢遇到的坑-無(wú)主鍵的關(guān)聯(lián)數(shù)據(jù)去重問題

 更新時(shí)間:2023年03月30日 10:19:49   作者:p7+  
這篇文章主要介紹了Mybatis關(guān)聯(lián)查詢遇到的坑-無(wú)主鍵的關(guān)聯(lián)數(shù)據(jù)去重問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

先說(shuō)下結(jié)論

使用Mybatis的association關(guān)聯(lián)查詢時(shí),例如 DeviceInfo 關(guān)聯(lián) RawData 查詢數(shù)據(jù),如果查詢的SQL不包含DeviceInfo的主鍵時(shí)

Mybatis會(huì)將查詢的結(jié)果集按照RawData數(shù)據(jù)進(jìn)行去重,與你有沒有使用group by無(wú)關(guān)。

解決方法1

如果你的查詢不需要關(guān)聯(lián)數(shù)據(jù),重寫resultMap,去掉association關(guān)聯(lián)的部分,此時(shí)無(wú)論你有沒有查詢主鍵,返回的數(shù)據(jù)都是正確數(shù)據(jù)

<resultMap id="DeviceTypeResultMap" type="DeviceInfo">
?? ?<!-- 有沒有查詢主鍵,返回的數(shù)據(jù)都是正確數(shù)據(jù) -->
?? ?<!--<id column="device_id" property="deviceId" jdbcType="BIGINT"></id>-->
?? ?<result column="device_type_name" property="deviceTypeName" jdbcType="VARCHAR" />
?? ?<result column="device_type_id" property="deviceTypeId" jdbcType="INTEGER" />
</resultMap>

解決方法2

如果你的查詢不需要關(guān)聯(lián)數(shù)據(jù),但是懶的再寫resultMap,那么SQL的查詢字段一定要加上主表的主鍵

例子:

DeviceInfo

public class DeviceInfo implements Serializable {
?? ?private Long deviceId;
?? ?private Integer deviceTypeId;
?? ?private String deviceTypeName;
?? ?private RawData rawData;
?? ?……
}

DeviceInfo 關(guān)聯(lián)查詢 RawData 返回的 resultMap

<resultMap id="BaseResultMap"
?? ?type="DeviceInfo">
?? ?<id column="device_id" property="deviceId" jdbcType="BIGINT"></id>
?? ?<result column="mac" property="mac" jdbcType="VARCHAR" />
?? ?<result column="device_type_id" property="deviceTypeId" jdbcType="BIGINT" />
?? ?<association property="rawData"
?? ??? ?javaType="RawData">
?? ??? ?<id column="device_id" property="deviceId" />
?? ??? ?<result column="data_time" property="dataTime" jdbcType="TIMESTAMP" />
?? ??? ?<result column="battery_power" property="batteryPower"
?? ??? ??? ?jdbcType="TINYINT" />
?? ?</association>
</resultMap>

根據(jù) device_type_id 分組查詢 device_type_id 和 device_type_name

<select id="getDeviceTypeList" parameterType="DeviceInfo" resultMap="BaseResultMap">
?? ?select device_type_id,device_type_name FROM tb_device_info group by device_type_id order by null
</select>

最后

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

诸城市| 宜阳县| 平顺县| 青神县| 景谷| 嘉禾县| 辽源市| 北安市| 常宁市| 太康县| 班玛县| 繁峙县| 哈密市| 山阳县| 沙河市| 桑日县| 中方县| 肇州县| 石家庄市| 陈巴尔虎旗| 文成县| 黎城县| 左贡县| 湖口县| 定州市| 平远县| 洛宁县| 永康市| 左贡县| 慈溪市| 冕宁县| 涿州市| 焦作市| 阜阳市| 潜江市| 长乐市| 冕宁县| 兴海县| 中山市| 博白县| 团风县|