收集-小技巧

闲时小技巧的收集,备忘……

1 java接收socket数据时,参数直接以InputStream接收即可,就是request中的输入流

    @PostMapping(value = "/test")
    public String test(InputStream in){ ... }

2 批量更新,只update重复记录,不会改变其它字段

	INSERT INTO `user` (`id`, `name`)
	VALUES
		('2', '这是名字') 
	ON DUPLICATE KEY 
	UPDATE name = VALUES (name);

3 推荐工具类依赖:hutool。官方文档: https://hutool.cn/docs/


    cn.hutool
    hutool-all
    RELEASE

4 格式化为字符串……基础阿

 // 数字前面补0。 %07d代表:在num前面补0,补足7位长度
        int num = 231;
        String str = String.format("%07d", num);
        System.out.println(str);

 

5 并发环境下,线程安全的原子类: AtomicInteger /AtomicLong

new AtomicInteger().incrementAndGet();
        new AtomicLong().incrementAndGet();

 

6 将JSON格式的字符串,直接转化成集合对象(泛型)

// com.alibaba.fastjson
String str = "{}";
Map map = JSON.parseObject(str, new TypeReference>(){});

7 根据IP,获取IP归属地 。http请求,JSON数据交互

# ip归属地地址
ip.addr.taobao.url = http://ip.taobao.com/service/getIpInfo.php?ip=1.1.1.1
# ip归属地地址
ip.addr.webse.url = https://api.webse.cn/getip?ip=1.1.1.1

 

 

你可能感兴趣的:(Java服务端)