欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis查詢數(shù)據(jù),賦值給List集合時(shí),數(shù)據(jù)缺少的問(wèn)題及解決

 更新時(shí)間:2022年01月20日 09:34:51   作者:咖啡苦澀  
這篇文章主要介紹了MyBatis查詢數(shù)據(jù),賦值給List集合時(shí),數(shù)據(jù)缺少的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

MyBatis查詢數(shù)據(jù)賦值給List集合數(shù)據(jù)缺少

今天在使用MyBatis查詢數(shù)據(jù)時(shí),發(fā)現(xiàn)查出來(lái)的數(shù)據(jù)和List集合的大小不一致,如下圖所示,Total為3,但是list集合size為2.

? List<ArticleCommentToShow> commentsByArticleId = articleCommentService.getCommentsByArticleId(article.getArticleId());
? ? ? ? ? ? logger.info("長(zhǎng)度:" + commentsByArticleId.size());
    /**
     * 評(píng)論用戶頭像
     */
    private String imagePath;
    /**
     * 評(píng)論用戶的用戶名
     */
    private String userName;
    /**
     * 評(píng)論實(shí)體類(lèi)
     */
    private ArticleComment articleComment;

ArticleCommentShow中包含了一個(gè)實(shí)體類(lèi)ArticleComment,在查詢的時(shí)候我使用了resultMap查詢,對(duì)應(yīng)的查詢?nèi)缦聢D所示

   <!--對(duì)應(yīng)于getCommentsByArticleId的需要字段-->
    <sql id="wholeCommon">
        user_name,image_path,article_comment_id,comment_content, comment_time, to_id,article_comment.user_id,article_comment.article_id,to_user_id
    </sql>
    <!--根據(jù)文章ID獲取評(píng)論-->
    <select id="getCommentsByArticleId" resultMap="CommentsResult">
        select
        <include refid="wholeCommon"/>
        from article_comment,user
        <where>
            (article_id = #{articleId} and article_comment.user_id =  user.user_id)
        </where>
    </select>
    <resultMap id="CommentsResult" type="com.molihub.entity.ArticleCommentToShow">
        <result property="userName" column="user_name"/>
        <result property="imagePath" column="image_path"/>
        <association property="articleComment" javaType="com.molihub.entity.ArticleComment">
            <id property="articleCommentId" column="article_comment_id"/>
            <result property="articleId" column="article_id"/>
            <result property="commentContent" column="comment_content"/>
            <result property="commentTime" column="comment_time"/>
            <result property="toId" column="to_id"/>
            <result property="userId" column="user_id"/>
            <result property="toUserId" column="to_user_id"/>
        </association>
    </resultMap>

經(jīng)過(guò)不斷的百度,查資料,發(fā)現(xiàn)是因?yàn)槲业牟槌鰜?lái)的數(shù)據(jù)沒(méi)有主鍵,因?yàn)槲也槌鰜?lái)的數(shù)據(jù)格式類(lèi)似這樣:list: [1,a],[2,a],[3,b],這里的字母為實(shí)體類(lèi),所以當(dāng)實(shí)體類(lèi)重復(fù)的時(shí)候,MyBatis會(huì)自動(dòng)去重,用最新的數(shù)據(jù)替換之前“重復(fù)”的數(shù)據(jù)。

解決辦法

1.添加主鍵,用于區(qū)分重復(fù)數(shù)據(jù),2.禁用二級(jí)緩存,否則雖然第一次查出來(lái)的數(shù)據(jù)是正常的,但是再次查詢的時(shí)候會(huì)發(fā)現(xiàn)數(shù)據(jù)依然缺少。

經(jīng)過(guò)修改,resultMap改為如下格式

   <resultMap id="CommentsResult" type="com.molihub.entity.ArticleCommentToShow">
        <id property="articleComment.articleCommentId" column="article_comment_id"/>
        <result property="userName" column="user_name"/>
        <result property="imagePath" column="image_path"/>
        <result property="articleComment.articleId" column="article_id"/>
        <result property="articleComment.commentContent" column="comment_content"/>
        <result property="articleComment.commentTime" column="comment_time"/>
        <result property="articleComment.toId" column="to_id"/>
        <result property="articleComment.userId" column="user_id"/>
        <result property="articleComment.toUserId" column="to_user_id"/>
    </resultMap>

Mybatis查詢時(shí)數(shù)據(jù)丟失的問(wèn)題

公司里的實(shí)體類(lèi)和mapper文件均由mybatis逆向工程生成

之前使用myabtis查詢時(shí)直接使用注解@select(......)時(shí)遇到了一個(gè)問(wèn)題。

結(jié)果顯示數(shù)據(jù)庫(kù)查詢沒(méi)有問(wèn)題,但是有的數(shù)據(jù)缺沒(méi)有插入到指定的字段中,如下圖中ID成功存儲(chǔ),Z40_ID,Z40_103到Z40_113均失敗。

經(jīng)過(guò)排查得出結(jié)論

如果數(shù)據(jù)庫(kù)命名很規(guī)范比如user_name,用逆向插件生成實(shí)體類(lèi)時(shí)該字段會(huì)自動(dòng)轉(zhuǎn)換為userName

但是如果數(shù)據(jù)庫(kù)命名形式為:字母(含數(shù)字)_字母(含數(shù)字)這種情況,自動(dòng)映射就會(huì)失效,就會(huì)發(fā)生部分?jǐn)?shù)據(jù)沒(méi)有set到指定屬性下;

解決辦法

對(duì)于一些命名不規(guī)范的列需要加上注解手動(dòng)映射

或者直接在mapper.xml文件里用xml方式寫(xiě)sql語(yǔ)句,一般逆向工程都自動(dòng)生成列的映射規(guī)范了;

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

相關(guān)文章

  • Java日志API管理最佳實(shí)踐詳解

    Java日志API管理最佳實(shí)踐詳解

    這篇文章主要介紹了Java日志API管理最佳實(shí)踐詳解,記錄日志只是有效地利用日志的第一步,更重要的是如何對(duì)程序運(yùn)行時(shí)產(chǎn)生的日志進(jìn)行處理和分析。,需要的朋友可以參考下
    2019-06-06
  • spring-cloud入門(mén)之eureka-server(服務(wù)發(fā)現(xiàn))

    spring-cloud入門(mén)之eureka-server(服務(wù)發(fā)現(xiàn))

    本篇文章主要介紹了spring-cloud入門(mén)之eureka-server(服務(wù)發(fā)現(xiàn)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Java集合系列之LinkedList源碼分析

    Java集合系列之LinkedList源碼分析

    這篇文章主要為大家詳細(xì)介紹了Java集合系列之LinkedList源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • SpringMVC上傳文件并保存到本地代碼實(shí)例

    SpringMVC上傳文件并保存到本地代碼實(shí)例

    這篇文章主要介紹了SpringMVC上傳文件并保存到本地代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java(enum)枚舉用法詳解

    Java(enum)枚舉用法詳解

    本篇文章主要介紹了Java 枚舉用法詳解,枚舉的好處:可以將常量組織起來(lái),統(tǒng)一進(jìn)行管理。有興趣的可以一起來(lái)了解一下。
    2016-11-11
  • springBoot項(xiàng)目常用目錄解讀

    springBoot項(xiàng)目常用目錄解讀

    這篇文章主要介紹了springBoot項(xiàng)目常用目錄解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 新手了解java 反射基礎(chǔ)知識(shí)

    新手了解java 反射基礎(chǔ)知識(shí)

    這篇文章主要介紹了Java反射機(jī)制的相關(guān)內(nèi)容,涉及了class類(lèi)的動(dòng)態(tài)加載,獲取成員變量、構(gòu)造函數(shù)信息等信息,需要的朋友可以參考下,希望對(duì)你有所幫助
    2021-07-07
  • 淺析Spring工廠的反射和配置文件

    淺析Spring工廠的反射和配置文件

    這篇文章主要介紹了淺析Spring工廠的反射和配置文件,spring是通過(guò)反射和配置文件的方式來(lái)獲取 JavaBean 對(duì)象,需要的朋友可以參考下
    2023-04-04
  • java中棧和隊(duì)列的實(shí)現(xiàn)和API的用法(詳解)

    java中棧和隊(duì)列的實(shí)現(xiàn)和API的用法(詳解)

    下面小編就為大家?guī)?lái)一篇java中棧和隊(duì)列的實(shí)現(xiàn)和API的用法(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • Java語(yǔ)言中Swing組件編程詳解

    Java語(yǔ)言中Swing組件編程詳解

    這篇文章主要為大家介紹了Java語(yǔ)言中Swing組件編程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10

最新評(píng)論