package com.yxw.zuoye.typehandler;

import com.alibaba.fastjson.JSONObject;
import com.yxw.zuoye.entity.User;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.alibaba.fastjson.JSON;

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

public class UserTypeHandler extends BaseTypeHandler<User> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, User user, JdbcType jdbcType) throws SQLException {

        preparedStatement.setString(i,JSON.toJSONString(user));
    }

    @Override
    public User getNullableResult(ResultSet resultSet, String s) throws SQLException {
        User user = JSON.parseObject(resultSet.getString(s),User.class);
        //JSONObject user = JSON.parseObject(resultSet.getString(s));
        return user;
    }

    @Override
    public User getNullableResult(ResultSet resultSet, int i) throws SQLException {
        User user = JSON.parseObject(resultSet.getString(i),User.class);
        //JSONObject user = JSON.parseObject(resultSet.getString(i));
        return user;
    }

    @Override
    public User getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        User user = JSON.parseObject(callableStatement.getString(i),User.class);
        //JSONObject user = JSON.parseObject(callableStatement.getString(i));
        return user;
    }

}
