package com.Config;

import com.Domain.Context;
import com.Domain.User;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by lqt
 */
@MappedTypes({Context.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class MyTypeHandler implements TypeHandler<Context> {

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Context user, JdbcType jdbcType) throws SQLException {
        System.out.println("自定义类型转换");
        String s = JSONObject.toJSONString(user);
        preparedStatement.setString(i, s);
    }

    @Override
    public Context getResult(ResultSet rs, String columnName) throws SQLException {
        System.out.println("自定义类型转换");
        String string = rs.getString(columnName);
        return JSONObject.parseObject(string, Context.class);
    }

    @Override
    public Context getResult(ResultSet rs, int columnIndex) throws SQLException {
        System.out.println("自定义类型转换");
        String string = rs.getString(columnIndex);
        return JSONObject.parseObject(string, Context.class);
    }

    @Override
    public Context getResult(CallableStatement cs, int columnIndex) throws SQLException {
        System.out.println("自定义类型转换");
        String string = cs.getString(columnIndex);
        return JSONObject.parseObject(string, Context.class);
    }
}
