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

相关推荐
endswel2 小时前
Spring MVC HandlerInterceptor 拦截请求及响应体
springmvc·springboot
椰椰椰耶5 小时前
【MyBatis】XML实现,配置方法和增、删、改、查
xml·oracle·mybatis
GJCTYU8 小时前
spring中@Transactional注解和事务的实战理解附代码
数据库·spring boot·后端·spring·oracle·mybatis
DuelCode19 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
荔枝吻21 小时前
【沉浸式解决问题】idea开发中mapper类中突然找不到对应实体类
java·intellij-idea·mybatis
JAVA学习通21 小时前
Mybatis--动态SQL
sql·tomcat·mybatis
新world1 天前
mybatis-plus从入门到入土(二):单元测试
单元测试·log4j·mybatis
RainbowSea1 天前
问题 1:MyBatis-plus-3.5.9 的分页功能修复
java·spring boot·mybatis
JAVA学习通1 天前
Mybatis----留言板
mybatis
双力臂4042 天前
MyBatis动态SQL进阶:复杂查询与性能优化实战
java·sql·性能优化·mybatis