In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use hutool and lombok tools in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the hutool and lombok tools in Java.
One. Hutool tool
An excerpt of a brief introduction to the hutool tool:
Hutool is a small and comprehensive Java tool class library, which reduces the learning cost of related API through static method encapsulation, and is a friendly alternative to the "util" package in the project. It saves developers time to encapsulate common classes and common tool methods in the project, and makes the development focus on the business.
Hutool-aop JDK dynamic proxy encapsulation, providing section support under non-IOC
Hutool-bloomFilter Bloom filtering, which provides some Hash algorithm Bloom filtering
Hutool-cache simple cache implementation
Hutool-core core, including Bean operations, dates, various Util, etc.
Hutool-cron timing task module, which provides timing tasks for Crontab-like expressions
Hutool-crypto encryption and decryption module, providing symmetrical, asymmetric and digest algorithm encapsulation
Data operation after hutool-db JDBC encapsulation, based on ActiveRecord idea
Hutool-dfa Multi-keyword search based on DFA Model
Hutool-extra extension module, encapsulated to third parties (template engine, mail, Servlet, QR code, Emoji, FTP, word segmentation, etc.)
Http client Encapsulation of hutool-http based on HttpUrlConnection
Hutool-log automatically identifies the log facade implemented by the log
Hutool-script script execution wrapper, such as Javascript
Hutool-setting more powerful Setting configuration files and Properties encapsulation
Hutool-system system parameter call encapsulation (JVM information, etc.)
Hutool-json JSON implementation
Implementation of hutool-captcha Picture CAPTCHA
Encapsulation of Excel and Word in POI by hutool-poi
Java-based NIO and AIO Socket package for hutool-socket
We can also see from the above that hutool has a rich range of tools, and I feel that it already covers 80% of the business of most small and medium-sized companies, so it is easy to improve the efficiency of the code.
Installation uses:
Hutool official documentation
Add the following to the dependencies of the pom.xml of the maven project:
Cn.hutool hutool-all 5.6.3
Ps: the above is equivalent to introducing all the utility classes of hutool, or we can introduce the required packages separately
Use case: package com.example.demo;import cn.hutool.json.JSONUtil;import lombok.Data;import java.util.ArrayList;import java.util.List;@Datapublic class User {private String userEmail; private String userName; private List userExampleList;} @ Dataclass 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 data format ="); System.out.println (jsonStr); Object obj1 = JSONUtil.getByPath (JSONUtil.parse (user), "userName"); System.out.println ("= ="); System.out.println ("get property userName value of User object through hutool"); System.out.println (obj1) Object obj2 = JSONUtil.getByPath (JSONUtil.parse (user), "userExampleList [1] .orderByClause"); System.out.println ("= ="); System.out.println ("get the orderByClause value of the second object in the User object's property List through hutool"); System.out.println (obj2) } / * initialize test data * @ return * / private static User getUser () {User user = new User (); user.setUserName ("wook"); user.setUserEmail ("xxxx@qq.com"); / / set a collection of UserExample to facilitate viewing the demo effect List list = new ArrayList (); UserExample userExample = new UserExample () UserExample.setDistinct (true); userExample.setOrderByClause ("Xiao Wu Test 1"); UserExample userExample2 = new UserExample (); userExample2.setDistinct (false); userExample2.setOrderByClause ("Xiao Wu Test 2"); list.add (userExample); list.add (userExample2); user.setUserExampleList (list); return user;}} run results:
= json data format =
{"userName": "wook", "userExampleList": [{"distinct": true, "orderByClause": "Xiao Wu Test 1"}, {"distinct": false, "orderByClause": "Xiao Wu Test 2"}], "userEmail": "xxxx@qq.com"}
= =
Get the property username value of the User object through hutool
Wook
= =
Get the orderByClause value of the second object in the property List of the User object through hutool
Xiao Wu Test 2
From the running results, we can see that the JSONUtil.getByPath () method needs to pass two parameters, the first is our json object, and the second is an expression (similar to how we directly manipulate the collection list [0] .propertyName in js). More functions are not listed in this article.
Ps:hutool source code there are many places for us to learn, to facilitate their own at the same time, but also to learn modestly.
Two: brief introduction to lombok
The lombok annotation builds the code when java compiles, and it can be more elegant in creating java objects without having to write redundant and repetitive code. (lombok has always been a controversial product, there is no comment here.)
Common notes
@ Getter or @ Setter
Using it on attributes, we don't have to write getter and setter methods
@ ToString
Used on classes, toString methods can be automatically overridden
@ EqualsAndHashCode
Used on classes, equals and hashCode methods can be generated automatically
@ AllArgsConstructor
Used on classes, automatically generate no-parameter constructions and use all parameter constructors
@ Data
Used on classes, (most commonly used) is equivalent to using @ ToString, @ EqualsAndHashCode, @ Getter, @ Setter and @ RequiredArgsConstrutor annotations at the same time
@ Builder
Used in classes, constructor pattern, case at the end
@ Slf4j
Used in classes, it can help us to use log.info directly in methods.
Installation uses:
File-- > Setting-- > plugins-- > lombok
two。 Add the following to the dependencies of the pom.xml of the maven project:
Org.projectlombok lombok 1.16.10 use case:
@ Date is already reflected in the above hutool use cases. Only @ Builder and @ Slf4j functions are demonstrated here.
@ 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));}} run result:
21 name 57 wook 03.702 [main] INFO com.example.demo.controller.BuilderExample-{"name": "wook", "age": 11}
At this point, I believe you have a deeper understanding of "how to use hutool and lombok tools in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.