自定义TypeHandler 将mysql返回的逗号分隔的String转换到List

sql执行如下:
在这里插入图片描述
这里我定义的接受类:
在这里插入图片描述
但是这里报了错JSON parse error: Cannot deserialize value of type java.util.ArrayList<java.lang.String>from Object value (token JsonToken.START_OBJECT); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.util.ArrayList<java.lang.String> from Object value (token JsonToken.START_OBJECT)at [Source: (PushbackInputStream); line: 1, column: 1]",

后来才发现,mysql的GROUP_CONCAT返回的是字符串String类型,无法映射到定义的list里面,MyBatis 返回的结果集与接收结果的对象类型不匹配。

方法一:这里可以修改接受类:,用String接受,在用String的split方法分割处理成List

    private String pdbId;
    private String uniprotId;
    private String geneSymbol;

方法二:自定义一个 TypeHandler 来处理逗号分隔的字符串到列表的转换:

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

//自定义一个 TypeHandler 来处理逗号分隔的字符串到列表的转换
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(","));
    }
}

在xml文件里自定义结果集映射并指定类型转换器:

 <resultMap id="pdbMap" type="com.herb.system.api.model.resp.analysis.PdbSearchResponse">
        <id property="pdbId" column="pdbId"/>
        <result property="uniprotId" column="uniprotId" javaType="java.util.List" jdbcType="VARCHAR" typeHandler="com.herb.entity.config.CommaSeparatedStringTypeHandler"/>
        <result property="geneSymbol" column="geneSymbol" javaType="java.util.List" jdbcType="VARCHAR" typeHandler="com.herb.entity.config.CommaSeparatedStringTypeHandler"/>
    </resultMap>

    <select id="getUniportSymbolOfPdb" resultMap="pdbMap">

        SELECT up.pdb_id as pdbId, GROUP_CONCAT(up.uniprot) AS uniprotId , GROUP_CONCAT(ug.gene_symbol) AS geneSymbol
        FROM `uniprot_pdb`  up  LEFT JOIN uniprot_gene ug on up.uniprot =  ug.uniprot
        WHERE up.pdb_id in
        <foreach collection="pdbIdList" item="id"  open="(" close=")" separator=",">
            #{
   id}
        </foreach>
        GROUP BY up.pdb_id
    </select>

需要注意的是,一般需要在 MyBatis 配置文件中注册这个自定义的 TypeHandler,在 src/main/resources 目录下创建一个 mybatis-config.xml 配置文件文件,在 配置文件中添加如下的配置:

<configuration>
    <typeHandlers>
        <!-- 注册自定义的 TypeHandler -->
        <typeHandler handler="your.package.path.CommaSeparatedStringTypeHandler"/>
    </typeHandlers>
</configuration>

然后在 application.properties 或 application.yml 文件中添加 MyBatis 的配置mybatis.config-location=classpath:mybatis-config.xml

但是在springboot里面,我没有加入上面的注册,也能运行,原因是MyBatis 可以通过自动扫描的方式发现自定义的 TypeHandler 而无需显式地在 中进行注册。这是因为 MyBatis 会默认扫描某些特定的包路径,例如 org.apache.ibatis.type。如果你的自定义 TypeHandler 的包路径在默认扫描路径下,MyBatis 可能会自动发现并注册它。

运行结果:
在这里插入图片描述

最近更新

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

    2023-12-06 07:40:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 07:40:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 07:40:03       82 阅读
  4. Python语言-面向对象

    2023-12-06 07:40:03       91 阅读

热门阅读

  1. MacOS 14挂载NTFS 硬盘的最佳方式(免费)

    2023-12-06 07:40:03       101 阅读
  2. 基于方向键实现矩阵选择

    2023-12-06 07:40:03       51 阅读
  3. CoreDNS实战(二)-CoreDNS+ETCD实现DNS服务发现

    2023-12-06 07:40:03       60 阅读
  4. asp.net core webpi 结合jwt实现登录鉴权

    2023-12-06 07:40:03       56 阅读
  5. MFC与qt的区别

    2023-12-06 07:40:03       54 阅读