Solution for springboot+mybatis+mysql to store single field in array format

1. Usage scenario

The requirement is: a field passed by the front end is an array type, but mysql does not support direct storage of arrays, it can only be converted to string storage, and when taken out, it must be converted back to an array, so whether it is converted at the front end or the back end. Time-consuming and laborious.

In this way, we can use the BaseTypeHandler class of mybatis to help us complete the automatic conversion.

first step:

Change the field in the object to JSONArray type.

Step 2: Inherit BaseTypeHandler and rewrite some methods inside.

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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;

/**
 * @author wusong
 *
 * @description 用以mysql中varchar格式的字段,进行转换的自定义转换器,转换为实体类的JSONArray属性
 */
@MappedTypes(JSONArray.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class JsonTypeHandler extends BaseTypeHandler<JSONArray> {

    /**
     * 重写设置参数
     * @param ps
     * @param i
     * @param parameter
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, JSONArray parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, String.valueOf(parameter.toJSONString()));
    }

    /**
     * 根据列名,获取可以为空的结果
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */
    @Override
    public JSONArray getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String sqlJson = rs.getString(columnName);
        if (null != sqlJson){
            return JSONObject.parseArray(sqlJson);
        }
        return null;
    }

    /**
     * 根据列索引,获取可以为空的结果
     * @param rs
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public JSONArray getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String sqlJson = rs.getString(columnIndex);
        if (null != sqlJson){
            return JSONObject.parseArray(sqlJson);
        }
        return null;
    }

    @Override
    public JSONArray getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String sqlJson = cs.getString(columnIndex);
        if (null != sqlJson){
            return JSONObject.parseArray(sqlJson);
        }
        return null;
    }
}

Step 3: The fields mapped in the resultMap of mabatis need to add typeHandler="the file path just rewritten"

Step 4: TypeHandler also needs to be added to insert and update statements

 

In this way, the front-end uses an array to transmit and receive, and the back-end uses a string to store, without interfering with each other, and you can play happily! ! !

 

Guess you like

Origin blog.csdn.net/qq_36961530/article/details/100117375