广告
返回顶部
首页 > 资讯 > 后端开发 > Python >MyBatis查询数据,赋值给List集合时,数据缺少的问题及解决
  • 327
分享到

MyBatis查询数据,赋值给List集合时,数据缺少的问题及解决

2024-04-02 19:04:59 327人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录mybatis查询数据赋值给List集合数据缺少解决办法Mybatis查询时数据丢失的问题经过排查得出结论解决办法MyBatis查询数据赋值给List集合数据缺少 今天在使用My

MyBatis查询数据赋值给List集合数据缺少

今天在使用MyBatis查询数据时,发现查出来的数据和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查询,对应的查询如下图所示

   <!--对应于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>
    <!--根据文章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>

经过不断的百度,查资料,发现是因为我的查出来的数据没有主键,因为我查出来的数据格式类似这样:list: [1,a],[2,a],[3,b],这里的字母为实体类,所以当实体类重复的时候,MyBatis会自动去重,用最新的数据替换之前“重复”的数据。

解决办法

1.添加主键,用于区分重复数据,2.禁用二级缓存,否则虽然第一次查出来的数据是正常的,但是再次查询的时候会发现数据依然缺少。

经过修改,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查询时数据丢失的问题

公司里的实体类和mapper文件均由mybatis逆向工程生成

之前使用myabtis查询时直接使用注解@select(......)时遇到了一个问题。

结果显示数据库查询没有问题,但是有的数据缺没有插入到指定的字段中,如下图中ID成功存储,Z40_ID,Z40_103到Z40_113均失败。

经过排查得出结论

如果数据库命名很规范比如user_name,用逆向插件生成实体类时该字段会自动转换为userName

但是如果数据库命名形式为:字母(含数字)_字母(含数字)这种情况,自动映射就会失效,就会发生部分数据没有set到指定属性下;

解决办法

对于一些命名不规范的列需要加上注解手动映射

或者直接在mapper.xml文件里用xml方式写sql语句,一般逆向工程都自动生成列的映射规范了;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: MyBatis查询数据,赋值给List集合时,数据缺少的问题及解决

本文链接: https://www.lsjlt.com/news/163070.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作