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关联,实现转译操作

相关推荐
Amagi.1 小时前
Redis的内存淘汰策略
数据库·redis·mybatis
我爱吃福鼎肉片3 小时前
【C++】——list
c++·vector·list
执键行天涯6 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
工业甲酰苯胺6 小时前
Spring Boot 整合 MyBatis 的详细步骤(两种方式)
spring boot·后端·mybatis
ggdpzhk8 小时前
Mybatis 快速入门(maven)
oracle·maven·mybatis
ahauedu9 小时前
案例分析-Stream List 中取出值最大的前 5 个和最小的 5 个值
数据结构·list
IT学长编程17 小时前
计算机毕业设计 教师科研信息管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·毕业设计·springboot·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·教师科研管理系统
Java小白笔记18 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
孟诸1 天前
计算机专业毕设-校园新闻网站
java·vue·毕业设计·springboot·课程设计
计算机学姐1 天前
基于SpringBoot+Vue的篮球馆会员信息管理系统
java·vue.js·spring boot·后端·mysql·spring·mybatis