MyBatis查詢數(shù)據(jù),賦值給List集合時,數(shù)據(jù)缺少的問題及解決
MyBatis查詢數(shù)據(jù)賦值給List集合數(shù)據(jù)缺少
今天在使用MyBatis查詢數(shù)據(jù)時,發(fā)現(xiàn)查出來的數(shù)據(jù)和List集合的大小不一致,如下圖所示,Total為3,但是list集合size為2.
? List<ArticleCommentToShow> commentsByArticleId = articleCommentService.getCommentsByArticleId(article.getArticleId()); ? ? ? ? ? ? logger.info("長度:" + commentsByArticleId.size());
/** * 評論用戶頭像 */ private String imagePath; /** * 評論用戶的用戶名 */ private String userName; /** * 評論實體類 */ private ArticleComment articleComment;
ArticleCommentShow中包含了一個實體類ArticleComment,在查詢的時候我使用了resultMap查詢,對應(yīng)的查詢?nèi)缦聢D所示
<!--對應(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獲取評論--> <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)過不斷的百度,查資料,發(fā)現(xiàn)是因為我的查出來的數(shù)據(jù)沒有主鍵,因為我查出來的數(shù)據(jù)格式類似這樣:list: [1,a],[2,a],[3,b],這里的字母為實體類,所以當實體類重復(fù)的時候,MyBatis會自動去重,用最新的數(shù)據(jù)替換之前“重復(fù)”的數(shù)據(jù)。
解決辦法
1.添加主鍵,用于區(qū)分重復(fù)數(shù)據(jù),2.禁用二級緩存,否則雖然第一次查出來的數(shù)據(jù)是正常的,但是再次查詢的時候會發(fā)現(xiàn)數(shù)據(jù)依然缺少。
經(jīng)過修改,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ù)據(jù)丟失的問題
公司里的實體類和mapper文件均由mybatis逆向工程生成
之前使用myabtis查詢時直接使用注解@select(......)時遇到了一個問題。
結(jié)果顯示數(shù)據(jù)庫查詢沒有問題,但是有的數(shù)據(jù)缺沒有插入到指定的字段中,如下圖中ID成功存儲,Z40_ID,Z40_103到Z40_113均失敗。
經(jīng)過排查得出結(jié)論
如果數(shù)據(jù)庫命名很規(guī)范比如user_name,用逆向插件生成實體類時該字段會自動轉(zhuǎn)換為userName
但是如果數(shù)據(jù)庫命名形式為:字母(含數(shù)字)_字母(含數(shù)字)這種情況,自動映射就會失效,就會發(fā)生部分數(shù)據(jù)沒有set到指定屬性下;
解決辦法
對于一些命名不規(guī)范的列需要加上注解手動映射
或者直接在mapper.xml文件里用xml方式寫sql語句,一般逆向工程都自動生成列的映射規(guī)范了;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring-cloud入門之eureka-server(服務(wù)發(fā)現(xiàn))
本篇文章主要介紹了spring-cloud入門之eureka-server(服務(wù)發(fā)現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01