package com.cesgroup.mybatis.handler;

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

public class MyTypeHandler extends BaseTypeHandler<JSONObject> {

    @Override
    public JSONObject getNullableResult(ResultSet rs, String s) throws SQLException {
        String infoJsonString= rs.getString(s);
        return infoJsonString==null?null:JSONObject.parseObject(infoJsonString);
    }

    @Override
    public JSONObject getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
        return null;
    }

    @Override
    public void setNonNullParameter(PreparedStatement statement, int i, JSONObject infoObject, JdbcType jdbcType) throws SQLException {
        try {
            String userInfoString= JSONObject.toJSONString(infoObject);
            statement.setString(i, userInfoString);
        } catch (Exception e) {
            System.out.println("----将对象转化为字符串失败!----");
        }
    }

    @Override
    public JSONObject getNullableResult(ResultSet arg0, int arg1) throws SQLException {
        return null;
    }
}
