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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

咖啡苦涩   2022-05-23 我要评论

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语句,一般逆向工程都自动生成列的映射规范了;

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们