SpringBoot 整合Lombok/Hutool/MapStruct工具类

一、Lombok

  1. pom.xml Maven依赖

<!-- Lombok自动生成getter,setter等 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

  2. 常用注解

@Getter / @Setter 可以作用在类上和属性上,放在类上,会对所有的非静态(non-static)属性生成Getter / Setter方法,放在属性上,会对该属性生成Getter / Setter方法,并可以指定Getter / Setter方法的访问级别
@ToString 类注解,自动生成类的toString方法,可以做一些定制,比如不使用某个字段,不调用Getter等
@EqualsAndHashCode 默认情况下,会使用所有非瞬态(non-transient)和非静态(non-static)字段来生成equals和hascode方法,也可以指定具体使用哪些属性
@NoArgsConstructor / @RequiredArgsConstructor  / @AllArgsConstructor 无参构造器 / 为final字段和标记了@NotNull的字段生成构造函数 / 全参构造器
@Data 包含@ToString, @EqualsAndHashCode, 所有属性的@Getter, 所有non-final属性的@Setter和@RequiredArgsConstructor的组合,通常情况下,基本上使用这个注解就足够了
@Builder 产生复杂的构建器api类
@Slf4j 等同于:private final Logger logger = LoggerFactory.getLogger(XXX.class);
@Synchronized 同步方法安全的转化
@SneakyThrows 异常处理(谨慎使用)
@NonNull 让你不在担忧并且爱上NullPointerException
@CleanUp 自动资源管理:不用再在finally中添加资源的close方法
@Value 用于注解final类

   3. 举例:如@Builder注解赋值新对象

 DiskSpace newDiskSize = DiskSpace.builder()
                        .logTime(afterLogTime)
                        .diskSize(file.getUsableSpace())
                        .diskIncrSize(file.getUsableSpace() - diskSpace.getDiskSize())
                        .createTime(date)
                        .build();

    @Builder(toBuilder = true)注解修改原对象的属性值

newDiskSize = newDiskSize.toBuilder()
                        .logTime(afterLogTime)
                        .diskSize(file.getUsableSpace())
                        .diskIncrSize(file.getUsableSpace() - diskSpace.getDiskSize())
                        .createTime(date)
                        .build();

    @Builder注解对于继承不行,会导致找不到父类属性问题,如果希望这样用法,就需要用@SuperBuilder替代,注意是父类和子类都需要加上,但不需要加@Builder即可解决。

  4. 常见错误

    错误一:实体使用@Builder注解,用mybatis查询数据时报错

    原因:Mybatis找不到构造方法,无法将查询结果映射到绑定对象上

    解决:在实体类上加@NoArgsConstructor和@AllArgsConstructor,或者直接显示声明构造方法

               

 

二、Hutool

  1. pom.xml Maven依赖

<!-- Hutool工具类库 -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.5.4</version>
</dependency>
        

  2. 常用列举

    A. RuntimeUtil:系统运行时工具类,用于执行系统命令的工具,如:RuntimeUtil.exec("systemctl start nginx");

    B. FileUtil:文件工具类,如:FileUtil.readUtf8Lines("E:\file.txt");

    C. StopWatch:任务耗时记录工具类,参考springboot启动源码前几行。

  3. 时间工具类

    @Test
    public void testDateUtil() {
        Date date = new Date();
        System.out.println(String.format("格式化日期时间(yyyy-MM-dd HH:mm:ss):%s", DateUtil.formatDateTime(date)));
        System.out.println(String.format("格式化日期(yyyy-MM-dd):%s", DateUtil.formatDate(date)));
        System.out.println(String.format("根据特定格式格式化日期时间(yyyyMMddHHmmss):%s", DateUtil.format(date, DatePattern.PURE_DATETIME_PATTERN)));

        String dateStr = DateUtil.formatDateTime(date);
        System.out.println(String.format("解析日期时间(yyyy-MM-dd HH:mm:ss):%s", DateUtil.parseDateTime(dateStr)));
        System.out.println(String.format("解析日期(yyyy-MM-dd):%s", DateUtil.parseDate(dateStr)));

        System.out.println(String.format("当前日期时间(yyyy-MM-dd HH:mm:ss):%s", DateUtil.now()));
        System.out.println(String.format("当前日期(yyyy-MM-dd):%s", DateUtil.today()));

        System.out.println(String.format("计算两个日期相差的天数:%s天", DateUtil.betweenDay(date, DateUtil.yesterday(), true)));
    }

   4. 集合工具类

    @Test
    public void testCollUtil() {
        List<Integer> list = Arrays.asList(1, 2);
        System.out.println(String.format("判断集合是否为非空:%s", CollUtil.isNotEmpty(list)));

        list = null;
        System.out.println(String.format("如果提供的集合为null,返回一个不可变的默认空集合,否则返回原集合:%s", CollUtil.emptyIfNull(list)));

        List<Integer> list1 = Arrays.asList(2, 3, 4, 5);
        List<Integer> list2 = Arrays.asList(3, 6, 1, 4);
        System.out.println(String.format("计算多个集合的交集:%s", CollUtil.intersection(list1, list2)));
    }

 可参考:Java工具类库Hutool

 

三、MapStruct

  1. pom.xml Maven依赖

<!-- MapStruct属性映射工具类库 -->
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.4.1.Final</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.4.1.Final</version>
    <scope>compile</scope>
</dependency>

  2. 属性转换层

package com.ruhuanxingyun.web.manage.model.converter;

import com.ruhuanxingyun.web.manage.model.dto.UserDTO;
import com.ruhuanxingyun.web.manage.model.vo.UserVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
 * @description: 用户对象转换
 * @author: ruphie
 * @date: Create in 2021/1/2 20:49
 * @company: ruhuanxingyun
 */
@Mapper
public interface UserConverter {

    UserConverter INSTANCE = Mappers.getMapper(UserConverter.class);

    UserVO dto2Vo(UserDTO userDTO);

}

  3. 使用方式

public static void main(String[] args) {
        UserDTO userDTO = new UserDTO();
        userDTO.setId(1L);
        userDTO.setEntry("58");
        userDTO.setIp("192.168.1.2");

        UserVO userVO = UserConverter.INSTANCE.dto2Vo(userDTO);
        System.out.println("userDTO:"+userDTO);
        System.out.println("userVO:"+userVO);
}

可参考:MapStruct优化转换对象

 

四、其他

  1. AopLog

  可参考:老生常谈SpringAop日志收集与处理做的工具包

  2. 正则大全

 

posted @ 2019-05-31 10:25  如幻行云  阅读(1677)  评论(0编辑  收藏  举报