Mybatis custom list collection parser

1. Write type parser

scheme 1: implement TypeHandler rewriting method
package com.aliyun.kuxuandemo.typeHandler;

import org.apache.ibatis.type.*;

import java.sql.*;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wb-zcx696752
 * @description:
 * @data: 2020/10/13 11:19 PM
 */

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListTypeHandler implements TypeHandler<List<String>> {

    @Override
    public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
        StringBuffer sb = new StringBuffer();
        for (String pa : parameter) {
            sb.append(pa).append(",");
        }

        ps.setString(i, sb.toString().substring(0, sb.toString().length() - 1));
    }

    @Override
    public List<String> getResult(ResultSet resultSet, String s) throws SQLException {
        String[] split = resultSet.getString(s).split(",");
        return Arrays.asList(split);
    }

    @Override
    public List<String> getResult(ResultSet resultSet, int i) throws SQLException {
        String[] split = resultSet.getString(i).split(",");
        return Arrays.asList(split);
    }

    @Override
    public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException {
        String[] split = callableStatement.getString(i).split(",");
        return Arrays.asList(split);
    }

}
scheme 2: inherit BaseTypeHandler class, override method
package com.aliyun.kuxuandemo.typeHandler;

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

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

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wb-zcx696752
 * @description:
 * @data: 2020/10/13 12:56 PM
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListTypeHandler extends BaseTypeHandler<List<String>> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
        //1.List集合转字符串
        StringBuffer sb = new StringBuffer();
        for (String string : strings) {
            sb.append(string).append(",");
        }
        //2.设置给ps
        preparedStatement.setString(i, sb.toString().substring(0, sb.toString().length() - 1));
    }

    @Override
    public List<String> getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String[] split = resultSet.getString(s).split(",");
        return Arrays.asList(split);
    }

    @Override
    public List<String> getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String[] split = resultSet.getString(i).split(",");
        return Arrays.asList(split);
    }

    @Override
    public List<String> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String[] split = callableStatement.getString(i).split(",");
        return Arrays.asList(split);
    }
}

2. Register the parser in mybatis config. XML

<typeHandlers>
        <typeHandler handler="com.aliyun.kuxuandemo.typeHandler.ListTypeHandler"/>
    </typeHandlers>

3. Use custom type parser

query
@Select("select video_id,corp_id,oss_url,title,category_list,description from video_basic_info " +
            "where video_id=#{VideoID} and corp_id=#{CorpID}")
    @Results({
            @Result(property = "videoId", column = "video_id"),
            @Result(property = "corpId", column = "corp_id"),
            @Result(property = "ossUrl", column = "oss_url"),
            @Result(property = "categoryList", column = "category_list", typeHandler = com.aliyun.kuxuandemo.typeHandler.ListTypeHandler.class)
    })
insert modification
@Update({
            "<script> ",
            "update video_basic_info ",
            "<set> ",
            "<if test = \"Title != null and Title != '' \"> ",
            "title=#{Title}, ",
            "</if> ",
            "<if test = \"Desc != null and Desc != '' \"> ",
            "description=#{Desc}, ",
            "</if> ",
            "<if test = \"Tag != null and Tag.size() > 0 \"> ",
            "category_list=#{Tag,javaType=List,jdbcType=VARCHAR,typeHandler=com.aliyun.kuxuandemo.typeHandler.ArrayTypeHandler} ",
            "</if> ",
            "</set> ",
            "where video_id=#{VideoID} and corp_id=#{CorpID}",
            "</script>"
    })
    int updateVideoBasicInfoTitleDescTag(BaseRequestParameterValidationModel parameter);

Read More: