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

Mybatis 查询TypeHandler使用,转译查询数据(逗号分隔转List)

创建自定义的Hanndler

/*** @Package: com.datalyg.common.core.handler* @ClassName: CommaSeparatedStringTypeHandler* @Author: dujiayu* @Description: 用于mybatis 解析逗号拼接字符串* @Date: 2024/5/29 10:03* @Version: 1.0*/
public class CommaSeparatedStringTypeHandler extends BaseTypeHandler<List<String>> {@Overridepublic void setNonNullParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType)throws SQLException {if (strings != null) {preparedStatement.setString(i, String.join(",", strings));} else {preparedStatement.setNull(i, jdbcType.TYPE_CODE);}}@Overridepublic List<String> getNullableResult(ResultSet resultSet, String s) throws SQLException {String result = resultSet.getString(s);return convertStringToList(result);}@Overridepublic List<String> getNullableResult(ResultSet resultSet, int i) throws SQLException {String result = resultSet.getString(i);return convertStringToList(result);}@Overridepublic List<String> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {String result = callableStatement.getString(i);return convertStringToList(result);}private List<String> convertStringToList(String input) {if (input == null || input.isEmpty()) {return null;}return Arrays.asList(input.split(","));}
}

配置YML文件

# Spring
spring:mybatis:type-handlers-package: com.datalyg.common.core.handler

Mybatis XML查询写法

    <resultMap id="VehicleInspectionBindConfigResultMap"type="com.datalyg.integration.vo.vehicle_inspection.VehicleInspectionInfoConfigItemVO"><result property="configId" column="id"/><result property="sort" column="sort"/><result property="inspectionMode" column="inspection_mode"/><result property="inspectionContent" column="inspection_content"/><result property="accessCriteria" column="access_criteria"/><result property="inspectionResult" column="inspection_result"/><result property="causeNonconformity" column="cause_nonconformity"/><result property="imageUrls" column="image_url" javaType="java.util.List" jdbcType="VARCHAR"typeHandler="com.datalyg.common.core.handler.CommaSeparatedStringTypeHandler"/></resultMap><select id="selectVehicleInspectionBindConfigList" resultMap="VehicleInspectionBindConfigResultMap">SELECT x.id,x.sort,x.inspection_mode,x.inspection_content,x.access_criteria,x.inspection_result,x.cause_nonconformity,x.image_urlFROM wh_vehicle_inspection_config_bind xWHERE x.vehicle_inspection_id = #{id}ORDER BY x.sort</select>

其中x.image_url为逗号拼接的字符串,转为List<String>集合返回

 <result property="imageUrls" column="image_url" javaType="java.util.List" jdbcType="VARCHAR"typeHandler="com.datalyg.common.core.handler.CommaSeparatedStringTypeHandler"/>

通过此方法与自定义的TypeHandler关联,实现转译操作

在这里插入图片描述

在这里插入图片描述

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

相关文章:

  • 易查分小程序|如何开启个人密钥功能?
  • NoSQL是什么?NoSQL数据库存在SQL注入攻击?
  • 成功解决“ImportError: cannot import name ‘mapping‘ from ‘collections‘”错误的全面指南
  • apexcharts数据可视化之圆环柱状图
  • 2024ciscn初赛——easycms
  • 融合神话传说:构建公共开放平台的技术探索
  • 鸿蒙应用Stage模型【应用/组件级配置】
  • Python魔法之旅-魔法方法(05)
  • 机器学习笔记(1):sklearn是个啥?
  • C++与C语言 通过指针改变const变量的值
  • OpenJDK优化技术之标量替换(Scalar Replacement)
  • 优思学院|研发工程师获取六西格玛证书有用吗?
  • C++第二十二弹---vector深度剖析及模拟实现(下)
  • GD32F470+lwip 丢包问题分析及解决
  • 好用的电子杂志制作平台分享
  • “云原生安全:构建弹性且安全的云上环境的关键要素“
  • 燃气安全阀检验维修:守护家庭安全的必备知识
  • 【JavaEE】多线程(1)
  • 相对位姿估计
  • 记一次 .NET某工业设计软件 崩溃分析
  • 2020 6.s081——Lab5:Lazy page allocation
  • 华为认证学习笔记:生成树
  • leetcode 97.交错字符串
  • The Missing Semester ( Shell 工具和脚本 和 Vim)
  • 【Uniapp微信小程序】自定义水印相机、微信小程序地点打卡相机
  • SimPO: Simple Preference Optimization with a Reference-Free Reward
  • CDH6.3.2安装文档
  • Java实战入门:深入解析Java中的 `Arrays.sort()` 方法
  • JavaScript的垃圾回收机制
  • 小程序使用Canvas设置文字竖向排列