Mybatis保存ip地址为整形handler

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vcshcn/article/details/48463869

resultmap使用

<result property="serverip" column="SERVERIP" javaType="IP" jdbcType="INTEGER"/>

语句使用

#{serverip, javaType=IP, jdbcType=INTEGER}

handler类

public class IPTypeHandler implements TypeHandler<String> {
	
	@Override
	public void setParameter(PreparedStatement ps, int index, String ip, JdbcType jdbcType) throws SQLException {
		if (ip == null) {
			ps.setNull(index, java.sql.Types.INTEGER);
		}
		else {
			ps.setInt(index, IPUtil.ip2int(ip));
		}
	}

	@Override
	public String getResult(ResultSet rs, String columnName) throws SQLException {
		int l = rs.getInt(columnName);
		if (rs.wasNull()) {
			return null;
		}
		return IPUtil.int2ip(l);
	}

	@Override
	public String getResult(ResultSet rs, int columnIndex) throws SQLException {
		int l = rs.getInt(columnIndex);
		if (rs.wasNull()) {
			return null;
		}
		return IPUtil.int2ip(l);
	}

	@Override
	public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
		int l = cs.getInt(columnIndex);
		if (cs.wasNull()) {
			return null;
		}
		return IPUtil.int2ip(l);
	}

}



猜你喜欢

转载自blog.csdn.net/vcshcn/article/details/48463869