package com.doumee.config.mybatis; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils; 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; /** * @author T14 */ public class JSONObjectHandler implements TypeHandler { @Override public void setParameter(PreparedStatement preparedStatement, int i, JSONObject o, JdbcType jdbcType) throws SQLException { preparedStatement.setString(i,toString()); } @Override public JSONObject getResult(ResultSet resultSet, String s) throws SQLException { String str = resultSet.getString(s); if (StringUtils.isBlank(str)){ return null; } if (!JSONUtil.isJsonObj(str)){ throw new SQLException("解析数据格式有误"); } return (JSONObject)JSONObject.parse(str); } @Override public JSONObject getResult(ResultSet resultSet, int i) throws SQLException { String str = resultSet.getString(i); if (StringUtils.isBlank(str)){ return null; } if (!JSONUtil.isJsonObj(str)){ throw new SQLException("解析数据格式有误"); } return (JSONObject)JSONObject.parse(str); } @Override public JSONObject getResult(CallableStatement callableStatement, int i) throws SQLException { String str = callableStatement.getString(i); if (StringUtils.isBlank(str)){ return null; } if (!JSONUtil.isJsonObj(str)){ throw new SQLException("解析数据格式有误"); } return (JSONObject)JSONObject.parse(str); } }