当前位置: 首页 > news >正文

Mybatis分页查询,同时返回total

在垃圾项目中一般都是使用mybatis plus自动返回Page,但是涉及到多表联合或者等等情况最终还是要使用mybatis进行手写sql查询,所以有此文章以便后续使用查询.

首先mysql需要支持多条查询语句,在mysql配置url后加上:

&allowMultiQueries=true&useAffectedRows=true

Mapper查询数据的返回值用Object接收

 List<Object> getWarningDataFromPage(@Nullable @Param("userId") Long userId,@Nullable @Param("orderNum") String orderNum,@Nullable @Param("deviceId") String deviceId,@Param("pageSize") long pageSize,@Param("pageNo") long pageNo);

mapper xml语句

<!--需查询数据map-->
<resultMap id="BaseResultMap" type="com.jingli.modules.sys.warning.domain.Warning"><id property="id" column="id" jdbcType="INTEGER"/><result property="warningId" column="warning_id" jdbcType="VARCHAR"/><result property="warningType" column="warning_type" jdbcType="INTEGER"/><result property="warningDesc" column="warning_desc" jdbcType="VARCHAR"/><result property="warningContent" column="warning_content" jdbcType="VARCHAR"/><result property="warningTime" column="warning_time" jdbcType="TIMESTAMP"/><result property="operatorId" column="operator_id" jdbcType="INTEGER"/><result property="deviceId" column="device_id" jdbcType="VARCHAR"/><result property="port" column="port" jdbcType="VARCHAR"/><result property="orderNumber" column="order_number" jdbcType="VARCHAR"/><result property="status" column="status" jdbcType="INTEGER"/><result property="remark" column="remark" jdbcType="VARCHAR"/></resultMap><!--total数据--><resultMap type="java.lang.Integer" id="count"><result column="total"/></resultMap><!--进行多次查询获取到对应数据-->
<select id="getWarningDataFromPage" resultMap="BaseResultMap,count">SELECT SQL_CALC_FOUND_ROWScw.id, cw.warning_id, cw.warning_type, cw.warning_desc, cw.warning_content, cw.warning_time, cw.operator_id,cw.device_id, cw.port, cw.order_number, cw.status, cw.remarkFROM`c_warning` AS `cw`LEFT JOIN `c_charging_pile` AS `ccp` ON cw.device_id = ccp.IDWHERE 1 = 1<if test="null != userId">AND ccp.userId = #{userId}</if><if test="null != orderNum and orderNum !=''">AND cw.order_number = #{orderNum}</if><if test="null != deviceId and deviceId !=''">AND cw.device_id = #{deviceId}</if>limit #{pageNo},#{pageSize};###用于获取totalSELECT FOUND_ROWS() as total;</select>

从List<Object>中取出数据:

 @Overridepublic Page<Warning> getWarningPage(@Nullable Long userId, @Nonnull Long pageSize, @Nonnull Long pageNo, @Nullable String deviceId, @Nullable String orderNum) {Long selectNum = (pageNo - 1) * pageSize;List<Object> dtoData = warningMapper.getWarningDataFromPage(userId, orderNum, deviceId, pageSize, selectNum);List<Warning> warningList = (List<Warning>) dtoData.get(0);Integer total =((List<Integer>) dtoData.get(1)).get(0);Page<Warning> warningPage = new Page<>(pageNo, pageSize);warningPage.setRecords(warningList);warningPage.setTotal(total);warningPage.setSearchCount(true);warningPage.setCurrent(pageNo);warningPage.setSize(pageSize);return warningPage;}

至此可实现mybatis plus的Page查询,mysql中执行查询total并不会再次查询,而是获取到对应的分页查询到数据的结果.

http://www.lryc.cn/news/381969.html

相关文章:

  • JDK17新增语法特征
  • 2748. 美丽下标对的数目(Rust暴力枚举)
  • Vue中双向数据绑定是如何实现的
  • 桌面云和云桌面的区别联系
  • ECMAScript6介绍及环境搭建
  • 什么是Azure OpenAI?
  • 一个易于使用、与Android系统良好整合的多合一游戏模拟器
  • java spring注解的使用
  • 什么是数据同步?数据同步时发生中断怎么办?
  • HarmonyOS Next开发学习手册——ExtensionAbility
  • 如何开发企业微信SCRM
  • Java中的标准注解与数据校验:深入解析与实例
  • 模型泛化性测试
  • 智能推荐系统:技术解析与实践指南
  • 盛元广通数字孪生智能集控实验室管理系统
  • Spring Boot 优雅进行数据脱敏
  • Vue3 条件语句
  • 小白想要快速学好office办公软件,主要学习以下几个方面?
  • 【Linux文件系统】被打开的文件与文件系统的文件之间的关联刨析总结
  • 爱迪特两年创业板上市路:销售费用率远高同行,侵权风险引关注
  • SQLite 与 Python:集成与使用
  • 【vue scrollTo 数据无限滚动 】
  • MinIO在Linux环境中的使用
  • 免费内网穿透工具 ,快解析内网穿透解决方案
  • 踩坑——VS添加相对路径
  • HTML【介绍】
  • 网络安全:Web 安全 面试题.(XSS)
  • Ubuntu网络管理命令:netstat
  • CV预测:快速使用DenseNet神经网络
  • 竞赛选题 python 机器视觉 车牌识别 - opencv 深度学习 机器学习