【Mybaits】Mybatis一对一查询,结果只返回一条,问题记录及解决方案
问题描述:
MyBatis一对一查询时,打印结果只有一条数据
实施步骤:
Index 实体类
public class Index {private Integer id;private String health;private String status;private String indexName;private String uuid;}
IndexShardNumCheck 实体类
public class IndexShardNumCheck {private Index index;private String check;}
Mapper XML文件
<resultMap type="com.ruoyi.kirara.domain.entity.IndexShardNumCheck" id="IndexShardNumCheck"><result property="check" column="shard_check"/><association property="index" javaType="com.ruoyi.kirara.domain.entity.Index"></association>
</resultMap>
sql
<sql id="selectShardNumCheck">select shard_check, id, health, status, index_name, uuid, pri_shard_num, replicas, docs_count, docs_deleted, store_size, pri_store_sizefrom (select id, health, status, index_name, uuid, pri_shard_num, replicas, docs_count, docs_deleted, store_size, pri_store_size,(caseWHEN store_size <= 2 * 1024 * 1024 * 1024 and pri_shard_num = 1 THEN '0'WHEN store_size > 2 * 1024 * 1024 * 1024 and store_size < 10 * 1024 * 1024 * 1024 and pri_shard_num <= 2 THEN '0'WHEN store_size > 10 * 1024 * 1024 * 1024 and store_size < 50 * 1024 * 1024 * 1024 and pri_shard_num = 5 THEN '0'WHEN store_size > 50 * 1024 * 1024 * 1024 and ( pri_shard_num = 10 or pri_shard_num = 20) THEN '0'ELSE '1'END) shard_checkfrom es_index) a</sql>
select
<select id="selectShardNumCheck" resultMap="IndexShardNumCheck" useCache="false" flushCache="true"><include refid="selectShardNumCheck"/><where>shard_check = '1'<if test="indexName != null and indexName != ''">AND index_name like concat('%', #{indexName, jdbcType=VARCHAR}, '%')</if><if test="uuid != null and uuid != ''">AND uuid like concat('%', #{uuid, jdbcType=VARCHAR}, '%')</if><if test="priShardNum != null and priShardNum != ''">AND pri_shard_num = #{priShardNum}</if></where>
</select>
然后出现问题,只能返回一条结果。
问题原因
需要说明 select的列不需要和对应的resultMap的元素数量一一对应;mybatis使用association 时必须要保证key和association并列,简单来说就是select后面的列很多都可以省但a.m_id, a.s_id不可以省
然后顺藤摸瓜,必须至少有一个和association 并列同级的属性存在且不会重复的
解决方法
Index 实体类
public class Index {private Integer id;private String health;private String status;private String indexName;private String uuid;}
IndexShardNumCheck 实体类
public class IndexShardNumCheck extends Index {private String check;
}
Mapper XML文件
<resultMap type="com.ruoyi.kirara.domain.entity.IndexShardNumCheck" id="IndexShardNumCheck"><result property="check" column="shard_check"/><result property="id" column="id"/><result property="indexName" column="index_name"/><result property="status" column="status"/><result property="health" column="health"/><result property="uuid" column="uuid"/><result property="priShardNum" column="pri_shard_num"/><result property="replicas" column="replicas"/><result property="docsCount" column="docs_count"/><result property="storeSize" column="store_size"/></resultMap>
sql
<sql id="selectShardNumCheck">select shard_check, id, health, status, index_name, uuid, pri_shard_num, replicas, docs_count, docs_deleted, store_size, pri_store_sizefrom (select id, health, status, index_name, uuid, pri_shard_num, replicas, docs_count, docs_deleted, store_size, pri_store_size,(caseWHEN store_size <= 2 * 1024 * 1024 * 1024 and pri_shard_num = 1 THEN '0'WHEN store_size > 2 * 1024 * 1024 * 1024 and store_size < 10 * 1024 * 1024 * 1024 and pri_shard_num <= 2 THEN '0'WHEN store_size > 10 * 1024 * 1024 * 1024 and store_size < 50 * 1024 * 1024 * 1024 and pri_shard_num = 5 THEN '0'WHEN store_size > 50 * 1024 * 1024 * 1024 and ( pri_shard_num = 10 or pri_shard_num = 20) THEN '0'ELSE '1'END) shard_checkfrom es_index) a</sql>
select
<select id="selectShardNumCheck" resultMap="IndexShardNumCheck" useCache="false" flushCache="true"><include refid="selectShardNumCheck"/><where>shard_check = '1'<if test="indexName != null and indexName != ''">AND index_name like concat('%', #{indexName, jdbcType=VARCHAR}, '%')</if><if test="uuid != null and uuid != ''">AND uuid like concat('%', #{uuid, jdbcType=VARCHAR}, '%')</if><if test="priShardNum != null and priShardNum != ''">AND pri_shard_num = #{priShardNum}</if></where>
</select>
总结
这个是因为mybatis 需要对应,而我们的并不行,所以无法实现该功能。
【Mybaits】Mybatis一对一查询,结果只返回一条,问题记录及解决方案相关推荐
- es 查询一次性只返回10条数据的解决办法
设置索引的max_result_window属性值,可通过postman工具发送请求进行修改,调用方式如下: PUT http://ip:port/index/_settings { "ma ...
- Jav详细介绍的Mapper对应的Mybatis xml查询结果resultType返回值类型
一.返回一般数据类型 此实列总代用的是string类型 列:比如我们要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 ...
- php mysql只获取一条数据_php mysql 查询只返回第一条数据
php mysql 查询只返回第一条数据 $search = mysql_query("select * from `info`"); $search = mysql_fetch_ ...
- sql只返回一条数据 fetch frist 1 rowonly 跟rownum的区别
对于sql只返回一条数据不同的数据库有着不同的写法 1.DB as400数据库的写法 fetch frist 1 row only 示例 select * from table where name ...
- 2.4.3 Mybatis 高级查询, 复杂映射, 返回主键, 动态SQL if, set, foreach, 核心配置文件深入,plugins标签, 多表查询, 嵌套查询
目录 Mybatis 复杂映射&配置文件深入 一 Mybatis高级查询 1.1 ResutlMap属性 1.2 多条件查询(三种) 1.3 模糊查询 二 Mybatis映射文件深入 2.1 ...
- mysql resulttype_常见的MyBatis中查询结果resultType返回值类型
一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper (dao)接口: // 根据 id 获得数据库中的 username 字段的值 String getStuName ...
- SQL语句删除重复记录且只保留一条有效记录
1.查找表中多余的重复记录,重复记录是根据单个字段(name)来判断 select * from lib where name in (select name from lib group by na ...
- mysql 只返回第一条_mybatis 关联查询时,从表只返回第一条记录解决办法
如果两表联查,主表和明细表的主键都是id的话,明细表的多条只能查询出来第一条. 造成以上情况可能的原因: 1.级联查询的时候,主表和从表有一样的字段名的时候,在mysql上命令查询是没问题的.但在my ...
- DbVisualizer数据库连接工具默认查询结果只显示100条解决方法,dbvis如何展示更多行,如何显示全部数据
如图可看到默认只显示了 100 行数据.右上角的 Max Rows 就是指定最大显示行数的,把这个调大就好了,但也别太大,万一数据过多,你的客户端可能会崩溃掉,比如一张表几千万条数据的这种. 喜欢的点 ...
最新文章
- 洛谷P1456 Monkey King
- driver: linux2.6 内核模块导出函数实例(EXPORT_SYMBOL)
- vscode 支持 markdown 流程图
- 【Java】Java垃圾回收机制
- bzoj 1008: [HNOI2008]越狱
- web.xml filter 不包含_Elasticsearch 之 Filter 与 Query 有啥不同?
- STM32启动文件详解
- 怎样让网站显示在 Google 搜索结果中?
- linux系统自动获取ip地址,Linux系统怎么自动获取ip地址用什么命令
- 一图看全 · 知道创宇乌镇行
- Bugzilla 下载和安装
- jq怎么获取值与下拉框怎么获取值
- MySQL8.0备份与还原工具讲解----mysqlbackup篇
- 使用JS判断日期的有效性
- 如何使用爬虫采集搜狐汽车新车资讯
- 纯CSS画基本图形(矩形、圆形、三角形、多边形、爱心)
- 强化学习个人学习总结
- 基于SpringBoot + Vue的个人博客系统16——文章的修改和删除
- 最全的计算机会议排名
- 微信蓝牙设备服务器,微信又更新了 支持连接蓝牙设备
热门文章
- 18天精读掌握《费曼物理学讲义卷一》 第12天 2019/6/28
- 高效应用Python处理电子表格
- 非常详细的Series核心操作使用详解
- 折腾Transmission实现固定IP服务器BT做种教程
- 不正确的c语言语句是,【单选题】下列不正确的C语言语句是( )。
A. x=y=5;
B. x=1,y=2;
C. y=int x;
D. x++;...
- GBase 8c 权限说明
- mysql备份 1044_Navicat访问MySQL出现1044/1045错误的解决方法
- 【项目实战】C/C++轻松实现4399小游戏:围住神经猫
- python导出数据到excel文件_Python笔记:把数据导出到Excel文件上
- VBA实现KMP和LCS算法