常用工具及常用解决方案

本文介绍了Java中常用工具如Apache Commons Collections的判空方法,解决后端精度问题的技巧,以及利用Java 8新特性处理日期。涵盖MyBatisPlus查询、Objects.equals比较、StringUtils操作,还有阿里巴巴JSON库的实例应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


title: 常用工具及常用解决方案

1、集合判空方法

使用org.apache.commons.collections4.CollectionUtils包下的**CollectionUtils.isNotEmpty(equipmentInfoList)**的方式判空

import org.apache.commons.collections4.CollectionUtils;

List<EquipmentInfo> equipmentInfoList = equipmentInfoService.list(new QueryWrapper<EquipmentInfo>().in("id", params));
    if(CollectionUtils.isNotEmpty(equipmentInfoList)){
        for (EquipmentInfo equipmentInfo : equipmentInfoList) {
            Integer type = equipmentInfo.getType();
            if(type != null){
                String equipmentType = DictBizCache.getValue("safe_equipment_type", type);
                equipmentInfo.setEquipmentType(equipmentType);
            }
        }
}

源码:

CollectionUtils.isNotEmpty(equipmentInfoList)

public static boolean isEmpty(Collection<?> coll) {
        return coll == null || coll.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> coll) {
        return !isEmpty(coll);
    }

CollectionUtils.isEmpty(Collection<?> coll)

public static boolean isEmpty(Collection<?> coll) {
        return coll == null || coll.isEmpty();
    }

2、关于后端返回Long类型的数据到前端精度缺失的解决方法

找到对应的实体类,在Long类型的属性上使用注解

@JsonSerialize(using = ToStringSerializer.class)

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

@JsonSerialize(using = ToStringSerializer.class)
private Long projectId;

这样返回到前端的数据就是一个字符串类型,不会出现精度缺失的问题。

3、java8新特性 - 时间处理

1)获取日期

LocalDate

2)获取时间

LocalTime

3)获取日期时间

LocalDateTime

	@Test
    public void testTime(){
        /**
         * 获取当前日期和当前日期加3天日期
         */
        LocalDate now = LocalDate.now();
        LocalDate localDate1 = now.plusDays(1);
        LocalDate localDate2 = now.plusDays(2);

        System.out.println(now);
        System.out.println(localDate1);
        System.out.println(localDate2);
        System.out.println("------------------------");
        LocalDateTime now1 = LocalDateTime.now();
        System.out.println(now1);

        /**
         * 获取最大时间和最小时间
         */
        LocalTime min = LocalTime.MIN;
        LocalTime max = LocalTime.MAX;
        System.out.println(min); // 00:00
        System.out.println(max); // 23:59:59.999999999

        /**
         * 拼接日期和时间,时间格式化
         */
        LocalDateTime start = LocalDateTime.of(now, min);
        LocalDateTime end = LocalDateTime.of(localDate2, max);
        String startFormat = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endFormat = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(startFormat); // 2021-12-02 00:00:00
        System.out.println(endFormat); // 2021-12-04 23:59:59
    }

4、mybatisPlus中常用方法

1)通过条件查询一个表中多条数据,使用list方法

	/**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link 	com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    List<T> list(Wrapper<T> queryWrapper);

2)批量保存多条数据

	@Transactional(rollbackFor = {Exception.class})
    default boolean saveBatch(Collection<T> entityList) {
        return this.saveBatch(entityList, 1000);
    }

5、Objects.equals(Objects比较两个值是否相等)

尤其是包装类型的数据使用这种比较的方法比较好

if(Objects.equals(CommonConstant.IS_COMPLATE,isComplate)){
			log.info("当前节点已完成!");
			return true; 
}

6、StringUtils(Spring工具类)

coll:需要处理的集合

delim:表示分隔符

该方法将集合类型的数据转为String字符串

1)将List集合转为String字符串并用“,”分隔开

public static String collectionToCommaDelimitedString(@Nullable Collection<?> coll) {
        return collectionToDelimitedString(coll, ",");
    }

2)字符串之间添加特殊分隔符

StringUtils.collectionToDelimitedString(@Nullable Collection<?> coll, String delim);

3)演示代码

import org.springframework.util.StringUtils;

public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("111");
        list.add("222");
        list.add("333");
        list.add("444");
        String s = StringUtils.collectionToCommaDelimitedString(list);
        log.info("---->{}",s); // --->111,222,333,444

        String s1 = StringUtils.collectionToDelimitedString(list, ";");
        log.info("---->{}",s1); // ---->111;222;333;444
}

4)统计一个子字符串在字符串出现的次数

// 统计一个子字符串在字符串出现的次数
int i = StringUtils.countOccurrencesOf("erowoiueoiur", "oiur");
log.info("统计一个子字符串在字符串出现的次数:{}",i); // 1

int i1 = StringUtils.countOccurrencesOf("erowoiueoiur", "r");
log.info("统计一个子字符串在字符串出现的次数:{}",i1); // 2

int s2 = StringUtils.countOccurrencesOf("s", null);
log.info("统计一个子字符串在字符串出现的次数:{}",s2); // 0

int s3 = StringUtils.countOccurrencesOf(null, "s");
log.info("统计一个子字符串在字符串出现的次数:{}",s3); // 0

7、JSON(alibaba)常用方法

1)将实体类转为JSON字符串

import com.alibaba.fastjson.JSON;

String cartItemJson = JSON.toJSONString(cartItem);

2)将JSON字符串转为实体类(使用较多)

CartItem cartItem = JSON.parseObject(res, CartItem.class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值