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

相关推荐
better_liang4 小时前
每日Java面试场景题知识点之-SpringBoot启动流程
java·面试·springboot·源码解析·启动流程
JAVA面经实录9176 小时前
MyBatis学习体系
java·mybatis
墨_风7 小时前
MyBatis时间区间查询异常排查(达梦数据库)
数据库·mybatis·达梦
霸道流氓气质12 小时前
MyBatis 分页查询 + Feign 数据补充实战指南
数据库·oracle·mybatis
隐退山林13 小时前
JavaEE进阶:MyBatis操作数据库(进阶)
数据库·java-ee·mybatis
苦逼的猿宝14 小时前
IT技术交流和分享平台的设计与实现(源码+论文)
java·毕业设计·springboot·计算机毕业设计
一条泥憨鱼18 小时前
深入理解Java反射(超详细)
java·开发语言·spring·mybatis·反射
不负岁月无痕18 小时前
STL-- C++ list类 模拟实现
开发语言·c++·list
海市公约18 小时前
Redis五大基础数据类型命令详解与经典应用场景
redis·list·set·hash·string·zset·缓存架构
一条泥憨鱼19 小时前
详解MyBatis 动态 SQL
java·数据库·sql·mysql·mybatis·动态sql