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

创建自定义的Hanndler

java 复制代码
/**
 * @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文件

yaml 复制代码
# Spring
spring:
  mybatis:
    type-handlers-package: com.datalyg.common.core.handler

Mybatis XML查询写法

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>集合返回

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

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

相关推荐
怡人蝶梦30 分钟前
Spring Boot启动慢?Redis缓存击穿?Kafka消费堆积?——Java后端常见问题排查实战
java·jvm·redis·kafka·springboot·prometheus·microservices
十五年专注C++开发3 小时前
CMake指令:list()
数据结构·c++·list·cmake·自动化编译工具
abcnull14 小时前
mybatis的mapper对应的xml写法
xml·sql·spring·mybatis·mapper
JAVA坚守者14 小时前
Java 对接 Office 365 邮箱全攻略:OAuth2 认证 + JDK8 兼容 + Spring Boot 集成(2025 版)
springboot·oauth2·office365·java 开发·企业级开发·jdk8 兼容
卿着飞翔17 小时前
Spring Boot3.4.1 集成 mybatis plus
spring boot·后端·mybatis
程序员Bears18 小时前
Spring Boot深度解析:自动配置、Starter依赖与MyBatis Plus集成指南
java·spring boot·mybatis
爱上语文1 天前
MyBatisPlus(1):快速入门
java·开发语言·数据库·后端·mybatis
Jaising6661 天前
Mybatis Plus 多租户实现思路分析
spring boot·mybatis·orm
花伤情犹在2 天前
Mybatis Plus 拦截器忽略机制全解:InterceptorIgnoreHelper 源码与实战
java·tomcat·mybatis
黎黎黎明⁠⁢2 天前
SpringBoot整合Sa-Token:实现RBAC权限模型
java·sa-token·springboot·idea