package com.cesgroup.mybatis2.Config;

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;

/**
 * <p>描述：</p>
 *
 * @Author lf
 * @Date 2020-1-2 11:30
 * @Version 1.0
 */
public class MyTypeHandler extends BaseTypeHandler<JSONObject> {

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


    @Override
    public JSONObject getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setNonNullParameter(PreparedStatement statement, int i, JSONObject infoObject, JdbcType jdbcType) throws SQLException {
        // TODO Auto-generated method stub
        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 {
        // TODO Auto-generated method stub
        return null;
    }

}
