package com.ces.mybatis.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.json.JSONObject;
/**
 * MyBatis类型转换
 */
public class JSONTypeHandler  extends BaseTypeHandler<JSONObject>{

	@Override	
	public JSONObject getNullableResult(ResultSet rs, String cloumName) throws SQLException {		
		// TODO Auto-generated method stub		
		String value = rs.getString(columnName);
		if (value != null) {
			try {                
				return new JSONObject(value);
			}catch (Exception e) {                
				e.printStackTrace();            
			}
		}
		return null;
	}

	@Override	
	public void setNonNullParameter(PreparedStatement statement, int i, JSONObject infoObject, JdbcType jdbcType) throws SQLException {		
		// TODO Auto-generated method stub
		if(infoObject != null){            
			statement.setString(i, infoObject.toString());       
		}
	}
	
	@Override	
	public JSONObject getNullableResult(CallableStatement arg0, int arg1) throws SQLException {		
		// TODO Auto-generated method stub		
		return null;	
	}
	

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

}
