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

作者:咖啡苦涩 时间:2021-07-05 23:25:00 

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

今天在使用MyBatis查询数据时,发现查出来的数据和List集合的大小不一致,如下图所示,Total为3,但是list集合size为2.

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

  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会自动去重,用最新的数据替换之前&ldquo;重复&rdquo;的数据。

解决办法

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均失败。

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

经过排查得出结论

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

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

解决办法

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

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

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

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

来源:https://blog.csdn.net/qq_31063727/article/details/104334141

标签:MyBatis,查询,数据,赋值,List
0
投稿

猜你喜欢

  • JVM内存增强之逃逸分析

    2022-01-23 00:22:08
  • 详解Java设计模式之桥接模式

    2022-08-05 23:27:08
  • Spring Boot conditional注解用法详解

    2022-03-19 02:32:25
  • springboot 无法自动装配的问题

    2021-05-29 06:14:30
  • 基于java的opencv开发过程详解

    2022-03-31 20:02:59
  • Java实现简单的迷宫游戏详解

    2022-11-22 05:00:52
  • 通过java.util.TreeMap源码加强红黑树的理解

    2021-07-27 08:45:59
  • Java判断所给年份是平年还是闰年

    2023-10-21 17:48:59
  • SpringBoot 如何实时刷新静态文件

    2023-11-02 13:27:09
  • SpringBoot对Controller进行单元测试的实现代码 附乱码解决方案

    2021-11-26 17:57:22
  • PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例

    2023-11-26 15:05:59
  • C#实现员工ID卡的识别功能

    2021-08-11 23:49:25
  • MyBatis-Plus工具使用之EntityWrapper解析

    2021-10-09 06:13:48
  • C++实现的O(n)复杂度内查找第K大数算法示例

    2023-06-30 15:51:13
  • spring-boot-maven-plugin报红解决方案(亲测有效)

    2022-07-23 01:16:46
  • 解决SpringMVC Controller 接收页面传递的中文参数出现乱码的问题

    2021-08-06 20:52:42
  • IDEA 2021.2 激活教程及启动报错问题解决方法

    2023-11-14 14:10:27
  • Mybatis环境搭建及文件配置过程解析

    2021-07-04 22:37:03
  • Springboot @Validated和@Valid的区别及使用详解

    2023-05-30 18:40:25
  • Java数据结构之ArrayList从顺序表到实现

    2022-06-14 00:53:25
  • asp之家 软件编程 m.aspxhome.com