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>> {

	@Override
	public 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);
		}
	}

	@Override
	public List<String> getNullableResult(ResultSet resultSet, String s) throws SQLException {
		String result = resultSet.getString(s);
		return convertStringToList(result);
	}

	@Override
	public List<String> getNullableResult(ResultSet resultSet, int i) throws SQLException {
		String result = resultSet.getString(i);
		return convertStringToList(result);
	}

	@Override
	public 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_url
        FROM wh_vehicle_inspection_config_bind x
        WHERE 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关联,实现转译操作

在这里插入图片描述

在这里插入图片描述

相关推荐

  1. MybatisTypeHandler使用

    2024-06-07 00:50:03       40 阅读
  2. mybatis批量查询List实体类

    2024-06-07 00:50:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 00:50:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 00:50:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 00:50:03       82 阅读
  4. Python语言-面向对象

    2024-06-07 00:50:03       91 阅读

热门阅读

  1. Kubernetes 之 StatefulSet基本原理

    2024-06-07 00:50:03       36 阅读
  2. flutter 自动生成静态资源的引用

    2024-06-07 00:50:03       32 阅读
  3. GPT-4o全面解析:版本对比、技术革新与个人见解

    2024-06-07 00:50:03       31 阅读
  4. x264 参考帧管理原理:reference_build_list 函数

    2024-06-07 00:50:03       26 阅读
  5. Hadoop_hdfs介绍

    2024-06-07 00:50:03       31 阅读
  6. 通过Redis实现防止接口重复提交功能

    2024-06-07 00:50:03       30 阅读
  7. 适配器模式 Adapter Pattern

    2024-06-07 00:50:03       33 阅读
  8. JVM内存分析之JVM优化

    2024-06-07 00:50:03       36 阅读
  9. excel 转换MAC地址格式方法

    2024-06-07 00:50:03       28 阅读
  10. 求二叉树第k层结点的个数--c++【做题记录】

    2024-06-07 00:50:03       27 阅读