MyBatis查詢數(shù)據(jù),賦值給List集合時(shí),數(shù)據(jù)缺少的問(wèn)題及解決
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)文章
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-01java中棧和隊(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