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;
/**
* 評論實(shí)體類
*/
private ArticleComment articleComment;ArticleCommentShow中包含了一個實(shí)體類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)是因?yàn)槲业牟槌鰜淼臄?shù)據(jù)沒有主鍵,因?yàn)槲也槌鰜淼臄?shù)據(jù)格式類似這樣:list: [1,a],[2,a],[3,b],這里的字母為實(shí)體類,所以當(dāng)實(shí)體類重復(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ù)丟失的問題
公司里的實(shí)體類和mapper文件均由mybatis逆向工程生成
之前使用myabtis查詢時直接使用注解@select(......)時遇到了一個問題。
結(jié)果顯示數(shù)據(jù)庫查詢沒有問題,但是有的數(shù)據(jù)缺沒有插入到指定的字段中,如下圖中ID成功存儲,Z40_ID,Z40_103到Z40_113均失敗。

經(jīng)過排查得出結(jié)論
如果數(shù)據(jù)庫命名很規(guī)范比如user_name,用逆向插件生成實(shí)體類時該字段會自動轉(zhuǎn)換為userName
但是如果數(shù)據(jù)庫命名形式為:字母(含數(shù)字)_字母(含數(shù)字)這種情況,自動映射就會失效,就會發(fā)生部分?jǐn)?shù)據(jù)沒有set到指定屬性下;
解決辦法
對于一些命名不規(guī)范的列需要加上注解手動映射

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

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
完美解決Server?returned?HTTP?response?code:403?for?URL報錯問題
在調(diào)用某個接口的時候,突然就遇到了Server?returned?HTTP?response?code:?403?for?URL報錯這個報錯,導(dǎo)致獲取不到接口的數(shù)據(jù),下面小編給大家分享解決Server?returned?HTTP?response?code:403?for?URL報錯問題,感興趣的朋友一起看看吧2023-03-03
解決Error:(5, 28) java: 程序包org.apache.ibatis.io
這篇文章主要介紹了解決Error:(5, 28) java: 程序包org.apache.ibatis.io不存在問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
springboot mybatis druid配置多數(shù)據(jù)源教程
這篇文章主要介紹了springboot mybatis druid配置多數(shù)據(jù)源教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
在java中使用SPI創(chuàng)建可擴(kuò)展的應(yīng)用程序操作
這篇文章主要介紹了在java中使用SPI創(chuàng)建可擴(kuò)展的應(yīng)用程序操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Java多線程鎖機(jī)制相關(guān)原理實(shí)例解析
這篇文章主要介紹了Java多線程鎖機(jī)制相關(guān)原理實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08

