App下載

分享兩款Java中非常好用的工具h(yuǎn)utool和lombok

宇宙冰可樂(lè) 2021-08-17 14:02:09 瀏覽數(shù) (2896)
反饋

在Java中有許多好用的工具,可以大大地提高開(kāi)發(fā)效率。下面,我分享給大家兩款Java里面非常好用的工具,hutool和lombok。希望能夠?qū)Υ蠹业膶W(xué)習(xí)或者工作有所幫助!

一.hutool工具

摘抄一段hutool工具的簡(jiǎn)介:

Hutool是一個(gè)小而全的Java工具類庫(kù),通過(guò)靜態(tài)方法封裝,降低相關(guān)API的學(xué)習(xí)成本,是項(xiàng)目中“util”包友好的替代,它節(jié)省了開(kāi)發(fā)人員對(duì)項(xiàng)目中公用類和公用工具方法的封裝時(shí)間,使開(kāi)發(fā)專注于業(yè)務(wù)。

  • hutool-aop JDK動(dòng)態(tài)代理封裝,提供非IOC下的切面支持
  • hutool-bloomFilter 布隆過(guò)濾,提供一些Hash算法的布隆過(guò)濾
  • hutool-cache 簡(jiǎn)單緩存實(shí)現(xiàn)
  • hutool-core 核心,包括Bean操作、日期、各種Util等
  • hutool-cron 定時(shí)任務(wù)模塊,提供類Crontab表達(dá)式的定時(shí)任務(wù)
  • hutool-crypto 加密解密模塊,提供對(duì)稱、非對(duì)稱和摘要算法封裝
  • hutool-db JDBC封裝后的數(shù)據(jù)操作,基于ActiveRecord思想
  • hutool-dfa 基于DFA模型的多關(guān)鍵字查找
  • hutool-extra 擴(kuò)展模塊,對(duì)第三方封裝(模板引擎、郵件、Servlet、二維碼、Emoji、FTP、分詞等)
  • hutool-http 基于HttpUrlConnection的Http客戶端封裝
  • hutool-log 自動(dòng)識(shí)別日志實(shí)現(xiàn)的日志門面
  • hutool-script 腳本執(zhí)行封裝,例如Javascript
  • hutool-setting 功能更強(qiáng)大的Setting配置文件和Properties封裝
  • hutool-system 系統(tǒng)參數(shù)調(diào)用封裝(JVM信息等)
  • hutool-json JSON實(shí)現(xiàn)
  • hutool-captcha 圖片驗(yàn)證碼實(shí)現(xiàn)
  • hutool-poi 針對(duì)POI中Excel和Word的封裝
  • hutool-socket 基于Java的NIO和AIO的Socket封裝

從上面我們也可看出,hutool的工具類十分豐富,個(gè)人感覺(jué)已涵蓋絕大多數(shù)中小公司80%的業(yè)務(wù),提升代碼效率不在話下。

安裝使用:

hutool官方文檔

在maven項(xiàng)目的pom.xml的dependencies中加入以下內(nèi)容:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.6.3</version>
</dependency>

ps:上面相當(dāng)于引入了hutool所有的工具類,我們也可以單獨(dú)引入需要的包

使用案例:

package com.example.demo;

import cn.hutool.json.JSONUtil;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class User {
    private String userEmail;

    private String userName;

    private List<UserExample> userExampleList;
}
@Data
class UserExample {
    protected String orderByClause;

    protected boolean distinct;

    public static void main(String[] args) {
        User user = getUser();
        String jsonStr = JSONUtil.toJsonStr(user);
        System.out.println("==============json數(shù)據(jù)格式==============");
        System.out.println(jsonStr);

        Object obj1 = JSONUtil.getByPath(JSONUtil.parse(user), "userName");
        System.out.println("========================================");
        System.out.println("通過(guò)hutool獲取User對(duì)象的屬性u(píng)serName值");
        System.out.println(obj1);

        Object obj2 = JSONUtil.getByPath(JSONUtil.parse(user), "userExampleList[1].orderByClause");
        System.out.println("========================================");
        System.out.println("通過(guò)hutool獲取User對(duì)象的屬性List<UserExample>中第二個(gè)對(duì)象的orderByClause值");
        System.out.println(obj2);
    }

    /**
     * 初始化測(cè)試數(shù)據(jù)
     * @return
     */
    private static User getUser() {
        User user = new User();
        user.setUserName("wook");
        user.setUserEmail("xxxx@qq.com");
        //設(shè)置一個(gè)UserExample的集合,方便看demo效果
        List<UserExample> list = new ArrayList<>();
        UserExample userExample = new UserExample();
        userExample.setDistinct(true);
        userExample.setOrderByClause("小吳測(cè)試1");
        UserExample userExample2 = new UserExample();
        userExample2.setDistinct(false);
        userExample2.setOrderByClause("小吳測(cè)試2");
        list.add(userExample);
        list.add(userExample2);
        user.setUserExampleList(list);
        return user;
    }
}

運(yùn)行結(jié)果:

==============json數(shù)據(jù)格式==============
{"userName":"wook","userExampleList":[{"distinct":true,"orderByClause":"小吳測(cè)試1"},{"distinct":false,"orderByClause":"小吳測(cè)試2"}],"userEmail":"xxxx@qq.com"}
========================================
通過(guò)hutool獲取User對(duì)象的屬性u(píng)serName值
wook
========================================
通過(guò)hutool獲取User對(duì)象的屬性List<UserExample>中第二個(gè)對(duì)象的orderByClause值
小吳測(cè)試2

從運(yùn)行結(jié)果可知,JSONUtil.getByPath()方法需要傳2個(gè)參數(shù),第1個(gè)是我們的json對(duì)象,第2個(gè)是表達(dá)式(寫(xiě)法類似于我們?cè)趈s中,直接操作集合 list[0].propertyName),更多的功能本文章不再列舉~
ps:hutool的源碼中有很多值得我們學(xué)習(xí)的地方,方便自己的同時(shí),更要虛心學(xué)習(xí)。

二:lombok


簡(jiǎn)介

lombok注解在java進(jìn)行編譯時(shí)進(jìn)行代碼的構(gòu)建,對(duì)于java對(duì)象的創(chuàng)建工作它可以更優(yōu)雅,不需要寫(xiě)多余的重復(fù)的代碼。(lombok一直是個(gè)有爭(zhēng)議的產(chǎn)品,此處不做評(píng)論)

常用注解

@Getter或@Setter
用在屬性上,我們就可以不必寫(xiě)getter和setter方法了

@ToString
用在類上,可以自動(dòng)復(fù)寫(xiě)toString方法

@EqualsAndHashCode
用在類上,可以自動(dòng)生成equals和hashCode方法

@AllArgsConstructor
用在類上,自動(dòng)生成無(wú)參構(gòu)造和使用所有有參構(gòu)造函數(shù)

@Data
用在類上,(最常用)相當(dāng)于同時(shí)使用了@ToString、@EqualsAndHashCode、@Getter、@Setter和 @RequiredArgsConstrutor這些注解

@Builder
用在類上,構(gòu)造者模式,案例在后面

@Slf4j
用在類上,能幫助我們直接在方法使用log.info

安裝使用:

File --> Setting --> plugins --> lombok

在這里插入圖片描述

2.在maven項(xiàng)目的pom.xml的dependencies中加入以下內(nèi)容:

<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.10</version>
  </dependency>

使用案例:

@Date在上面hutool使用案例已經(jīng)體現(xiàn),此處只演示@Builder及@Slf4j的功能

@Data
@Builder
@Slf4j
 class BuilderExample {
    private String name;
    private int age;
    public static void main(String[] args) {
        BuilderExample builderExample = BuilderExample.builder().age(11).name("wook").build();
        log.info(JSONUtil.toJsonStr(builderExample));
    }
}

運(yùn)行結(jié)果:

21:57:03.702 [main] INFO com.example.demo.controller.BuilderExample - {"name":"wook","age":11}

關(guān)于Java里面hutool和lombok工具的分享就到此結(jié)束了,想要了解更多Java中實(shí)用工具的內(nèi)容,可以搜索W3Cschool相關(guān)內(nèi)容的文章,希望大家能夠多多支持!


0 人點(diǎn)贊