Hibernate Entity中最佳IP地址类型? [英] Best type for IP-address in Hibernate Entity?

查看:117
本文介绍了Hibernate Entity中最佳IP地址类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Hibernate在数据库中存储IP地址的最佳类型是什么?

What is the best type for storing IP-addresses in a database using Hibernate?

我通过Byte []或String,但是有更好的方法,或者你使用什么?

I though Byte[] or String, but is there a better way, or what do you use?

 @Column(name = "range_from",  nullable = false)
 public Byte[] getRangeFrom() {
  return rangeFrom;
 }
 public void setRangeFrom(Byte[] rangeFrom) {
  this.rangeFrom = rangeFrom;
 }


推荐答案

strong> long ,查找速度非常快。您可以使用以下函数进行转换。

I store it in a long, very fast for lookup. You can use the following functions for conversion.

public static string GetStandardIP(long numericIP)
{
    string w = Convert.ToString(Convert.ToInt64(numericIP / 16777216) % 256);
    string x = Convert.ToString(Convert.ToInt64(numericIP / 65536) % 256);
    string y = Convert.ToString(Convert.ToInt64(numericIP / 256) % 256);
    string z = Convert.ToString(Convert.ToInt64(numericIP) % 256);

    return w + "." + x + "." + y + "." + z;
}

而这一块

And this one

public static long GetNumericIP(string standardIP)
{
    if (standardIP != null && standardIP != string.Empty)
    {
        string[] ipParts = standardIP.Split('.');
        long numericIP = 16777216 * Convert.ToInt64(ipParts[0]) + 65536 * Convert.ToInt64(ipParts[1]) + 256 * Convert.ToInt32(ipParts[2]) + Convert.ToInt32(ipParts[3]);

        return numericIP;
    }
    return 0;
}

您可能希望通过检查参数来改进它们。

You may want to improve them by doing checks on the parameters.

这篇关于Hibernate Entity中最佳IP地址类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆