package com.cesgroup.practice.handler;

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

public class JSONHandler extends BaseTypeHandler<JSONObject> {

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

    }

    @Override
    public JSONObject  getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String infoJsonString= resultSet.getString(s);
        JSONObject infObject=null;
        try {
            infObject = JSONObject.parseObject(infoJsonString);
            //System.out.println("----将json串转成json对象成功！----");
        } catch (Exception e) {
            System.err.println("将json串转成json对象失败！");;
        }
        return infObject;

    }

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

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