溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中的hutool和lombok工具怎么使用

發(fā)布時間:2022-02-28 10:51:53 來源:億速云 閱讀:183 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Java中的hutool和lombok工具怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Java中的hutool和lombok工具怎么使用”吧!

一.hutool工具

摘抄一段hutool工具的簡介:

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

  • hutool-aop JDK動態(tài)代理封裝,提供非IOC下的切面支持

  • hutool-bloomFilter 布隆過濾,提供一些Hash算法的布隆過濾

  • hutool-cache 簡單緩存實現(xiàn)

  • hutool-core 核心,包括Bean操作、日期、各種Util等

  • hutool-cron 定時任務(wù)模塊,提供類Crontab表達式的定時任務(wù)

  • hutool-crypto 加密解密模塊,提供對稱、非對稱和摘要算法封裝

  • hutool-db JDBC封裝后的數(shù)據(jù)操作,基于ActiveRecord思想

  • hutool-dfa 基于DFA模型的多關(guān)鍵字查找

  • hutool-extra 擴展模塊,對第三方封裝(模板引擎、郵件、Servlet、二維碼、Emoji、FTP、分詞等)

  • hutool-http 基于HttpUrlConnection的Http客戶端封裝

  • hutool-log 自動識別日志實現(xiàn)的日志門面

  • hutool-script 腳本執(zhí)行封裝,例如Javascript

  • hutool-setting 功能更強大的Setting配置文件和Properties封裝

  • hutool-system 系統(tǒng)參數(shù)調(diào)用封裝(JVM信息等)

  • hutool-json JSON實現(xiàn)

  • hutool-captcha 圖片驗證碼實現(xiàn)

  • hutool-poi 針對POI中Excel和Word的封裝

  • hutool-socket 基于Java的NIO和AIO的Socket封裝

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

安裝使用:

hutool官方文檔

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

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

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

使用案例:

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("通過hutool獲取User對象的屬性userName值");
        System.out.println(obj1);

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

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

運行結(jié)果:

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

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

二:lombok


簡介

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

常用注解

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

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

@EqualsAndHashCode
用在類上,可以自動生成equals和hashCode方法

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

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

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

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

安裝使用:

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

Java中的hutool和lombok工具怎么使用

2.在maven項目的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));
    }
}

運行結(jié)果:

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

到此,相信大家對“Java中的hutool和lombok工具怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI