Mybatis将实体中数组元素转为JSON字符串存储
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.ibatis.type.JdbcType;
import org.util.JsonArrayTypeHandler;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
/**
* 业务流程相关信息表
* @author yangkaixiao
* @since 2023-12-22
*/
@Data
@TableName(value = "entity", autoResultMap = true)
@ApiModel(value = "entity对象", description = "业务流程相关信息表")
public class Entity{
/**
* ID
*/
@ApiModelProperty(value = "ID")
@TableId(value = "id", type = IdType.ID_WORKER_STR)
private String id;
/**
* 相关业务ID,批量操作会出现多个ID
*/
@NotEmpty(message = "相关业务ID为空")
@TableField(value = "relation_id", jdbcType = JdbcType.LONGNVARCHAR, typeHandler = JsonArrayTypeHandler.class)
@ApiModelProperty(value = "相关业务ID")
private String[] relationId;
/**
* 相关业务数据
*/
@NotEmpty(message = "相关业务数据为空")
@TableField(value = "relation_data", jdbcType = JdbcType.LONGNVARCHAR, typeHandler = JsonArrayTypeHandler.class)
@ApiModelProperty(value = "相关业务数据")
private Object[] relationData;
}
import cn.hutool.core.convert.ConvertException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* json数组类型转换器
* @author yangkaixiao
* @since 2023-12-22
*/
public class JsonArrayTypeHandler implements TypeHandler<Object[]> {
private final Class<Object[]> valueType;
public JsonArrayTypeHandler(Class<Object[]> valueType) {
if (valueType == null) {
throw new IllegalArgumentException("参数类型不能为空");
}
this.valueType = valueType;
}
@Override
public void setParameter(PreparedStatement preparedStatement, int index, Object[] value, JdbcType jdbcType) throws SQLException {
if (value == null) {
preparedStatement.setNull(index, jdbcType.TYPE_CODE);
} else {
try {
preparedStatement.setString(index, new JsonMapper().writeValueAsString(value));
} catch (Exception e) {
throw new ConvertException("对象数组转JSON字符串发生异常");
}
}
}
@Override
public Object[] getResult(ResultSet resultSet, String columnName) throws SQLException {
return this.convertValue(resultSet.getString(columnName));
}
@Override
public Object[] getResult(ResultSet resultSet, int index) throws SQLException {
return this.convertValue(resultSet.getString(index));
}
@Override
public Object[] getResult(CallableStatement callableStatement, int index) throws SQLException {
return this.convertValue(callableStatement.getString(index));
}
/**
* 转换json字符串为对象数组
* @param jsonValue json字符串
* @return 对象数组
*/
private Object[] convertValue(String jsonValue) {
try {
if (jsonValue == null) {
return null;
}
return new ObjectMapper().readValue(jsonValue, valueType);
} catch (Exception e) {
throw new ConvertException("JSON字符串转对象数组发生异常");
}
}
}