闲得发慌搞的东西,大家将就着看...
当前环境: spring boot 2.x , jdk1.8 , jpa
准备:
- 去纯真ip下载http://update.cz88.net/soft/setup.zip
- 安装完软件,进入点击解压,获得ip的txt文件,
- 用notepad++打开,拉到最后删除最后两行,
- 选择替换,勾选正则表达式 查找目标: +(要记得+号前面还有一个空格), 替换为:&,之后点击全部替换. 保存
ip实体类
@Data
@Entity
@Table(name = "sys_ip")
public class Ip {
@Id
@Column(name = "id", columnDefinition = "varchar(50) COMMENT '主键'")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@GeneratedValue(generator = "uuid")
public String id;
@Column(columnDefinition = "varchar(32) COMMENT '起始ip'")
private String startIp;
@Column(columnDefinition = "varchar(32) COMMENT '结束ip'")
private String endIp;
@Column(columnDefinition = "varchar(126) COMMENT '地区'")
private String area;
@Column(columnDefinition = "varchar(126) COMMENT '详细地址'")
private String detail;
}
工具类
public class ImportUtil {
public List<Ip> importMysql(String path) throws IOException {
File file = new File(path);
List<Ip> list = new ArrayList<>();
if (file.isFile() && file.exists()) {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "gbk");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lineText;
while ((lineText = bufferedReader.readLine()) != null) {
Ip ip = new Ip();
String[] ips = lineText.split("&");
ip.setStartIp(ips[0]);
ip.setEndIp(ips[1]);
ip.setArea(ips[2]);
ip.setDetail(ips[3]);
try {
ip.setDetail(ips[3]);
} catch (ArrayIndexOutOfBoundsException e) {
ip.setDetail("");
}
list.add(ip);
}
}
return list;
}
}
获得全部的List<Ip> list之后, 下面仅提供思路,懒得敲代码了
时间多就一股脑ipRepo.saveAll(list);
追求效率就分批插入.