


[Compilation and sharing] 8 development tools to improve work efficiency and never be an overtime worker again!
Sep 15, 2022 am 11:10 AMAre you still bald because of working overtime and staying up late? Are you still building wheels for strange needs? Then you’ve found the right person! ! This article has personally experienced the pain of programmers. I vomited blood behind the scenes and compiled an article. I hope it will be helpful to everyone. Go, go, go! !
If you want to do your job well, you must first sharpen your tools
As a programmer, every day The thing I faced was writing code and bragging. But I always feel that I have not reached a balance between these two things. I always feel that I spend too much time writing code every day, and there is not much time for myself to brag. I wonder if you all have these questions and doubts?
We know that programmers are the most lazy creatures! Why! So the question is, why do you still have so little time for fishing? Are we too good at it? No, no, no, don’t underestimate yourself. What could be the reason?
The answer is, of course, you haven’t read this article yet. This article has personally experienced the pain of programmers. I vomited blood behind my back and compiled an article. Now I share it with everyone. I hope it will be helpful to everyone.
Directory
Overall preview
JSON Parsing tool
HTTP network request tool
String processing tool
Collection processing tool
File stream processing tool
Encryption and decryption tool
JAVA bean object conversion tool
Caching and current limiting tool
Get started
Overall preview
This article will classify triggers from the picture, introduce related tool kits, and briefly introduce their use. Because the length of this article is limited, it is only used as an introduction. The specific details still need to be understood slowly when writing code.
JSON parsing tool
json I believe I don’t need to say more about how commonly used parsing tools are in development. Well, it can be said to be a tool that programmers use every day. This is why I put it first. Let’s take a look at it, summarize it and use it, GO! The author I use more often is Fastjson, which is a tool for JSON parsing from Alibaba Open Source, and its usage is also quite simple.
1. maven import pom coordinates
<dependency> ????<groupid>com.alibaba</groupid> ????<artifactid>fastjson</artifactid> ????<version>1.2.83</version> </dependency>
2. Let’s see how to use it
- JSON Conversion between strings and entity objects
//?字符串轉(zhuǎn)對(duì)象 Studen?student?=?JSON.parseObject("{"name":"小明","age":18}",?Student.class); //?對(duì)象轉(zhuǎn)字符串 String?str?=?JSON.toJSONString(student);
- JSON Strings and JSONObject Mutual conversion
JSONObject is just a data structure, which can be understood as a data structure in JSON format (key-value structure), you can use the put method to add elements to the json object. JSONObject can be easily converted into string, and other objects can also be easily converted into JSONObject objects
//?字符串轉(zhuǎn)JSONObject對(duì)象 JSONObject?jsonObject?=?JSONObject.parseObject("{"name":"小明","age":18}"); //?JSONObject對(duì)象轉(zhuǎn)字符串 String?str?=?jsonObject.toJSONString();
- JSON String Convert to Collection class
//?定義解析字符串 String?studentListStr?=?"[{"name":"小明","age":18},{"name":"小牛","age":24}]"; //?解析為?List<student> List<student>?studentList?=?JSON.parseArray(studentListStr,?Student.class); //?定義解析字符串 String?studentMapStr?=?"{"name":"小明","age":18}"; //?解析為?Map<string> Map<string>?stringStringMap?=? JSONObject.parseObject(studentMapStr,?new?TypeReference<map>>(){});</map></string></string></student></student>
fastjson That’s it. Here we only introduce simple usage. For more detailed usage, please refer to the official website Document, there are more usages waiting for you~~
HTTP 網(wǎng)絡(luò)請(qǐng)求工具
除了 JSON 工具,作為一個(gè)優(yōu)秀的互聯(lián)網(wǎng)打工人,不學(xué)會(huì)網(wǎng)絡(luò)請(qǐng)求,怎么能夠在這個(gè)行業(yè)占有一席之地呢?HTTP 網(wǎng)絡(luò)請(qǐng)求工具你值得擁有~~ 根據(jù)我的個(gè)人意愿,我簡(jiǎn)單介紹 httpclient的用法,因?yàn)槲覍?duì)這個(gè)比較熟悉
1、maven 導(dǎo)入 pom 坐標(biāo)
2、如何使用
- GET 請(qǐng)求(無(wú)參)
/** *?無(wú)參的?GET?請(qǐng)求 */ public?static?void?noArgsGetHttp()?throws?IOException?{ ????//?定義?httpclient ????CloseableHttpClient?httpClient?=?HttpClientBuilder.create().build(); ????//?創(chuàng)建?httpGet ????HttpGet?httpGet?=?new?HttpGet("http://www.baidu.com"); ????//?定義返回結(jié)果 ????CloseableHttpResponse?execute?=?null; ????//?發(fā)送執(zhí)行 ????execute?=?httpClient.execute(httpGet); ????//?獲取返回值 ????HttpEntity?entity?=?execute.getEntity(); ????System.out.println("響應(yīng)狀態(tài)為:"?+?execute.getStatusLine()); ????if?(Objects.nonNull(entity))?{ ????????System.out.println("響應(yīng)內(nèi)容長(zhǎng)度為:"?+?entity.getContentLength()); ????????System.out.println("響應(yīng)內(nèi)容為:"?+?EntityUtils.toString(entity)); ????} ????//?釋放資源 ????if?(httpClient?!=?null)?{ ????????httpClient.close(); ????} ????if?(execute?!=?null)?{ ????????execute.close(); ????} }
響應(yīng)狀態(tài)為:HTTP/1.1 200 OK 響應(yīng)內(nèi)容長(zhǎng)度為:-1 響應(yīng)內(nèi)容為:
- GET 請(qǐng)求(有參)
?/** *?有參的?GET?請(qǐng)求 */ public?static?void?haveArgsGetHttp()?throws?IOException,?URISyntaxException?{ ????//?定義?httpclient ????CloseableHttpClient?httpClient?=?HttpClientBuilder.create().build(); ????//?創(chuàng)建參數(shù)列表 ????List<namevaluepair>?valueParamsList?=?new?ArrayList(); ????valueParamsList.add(new?BasicNameValuePair("studentId","1")); ????//?創(chuàng)建對(duì)應(yīng)請(qǐng)求?Uri ????URI?uri?=?new?URIBuilder() ????????.setScheme("http") ????????.setHost("localhost") ????????.setPath("/getStudentInfo") ????????.setParameters(valueParamsList) ????????.build(); ????//?根據(jù)?Uri?創(chuàng)建?httpGet ????HttpGet?httpGet?=?new?HttpGet(uri); ????//?定義返回結(jié)果 ????CloseableHttpResponse?execute?=?null; ????//?發(fā)送執(zhí)行 ????execute?=?httpClient.execute(httpGet); ????//?獲取返回值 ????HttpEntity?entity?=?execute.getEntity(); ????System.out.println("響應(yīng)狀態(tài)為:"?+?execute.getStatusLine()); ????if?(Objects.nonNull(entity))?{ ????????System.out.println("響應(yīng)內(nèi)容長(zhǎng)度為:"?+?entity.getContentLength()); ????????System.out.println("響應(yīng)內(nèi)容為:"?+?EntityUtils.toString(entity)); ????} ????//?釋放資源 ????if?(httpClient?!=?null)?{ ????????httpClient.close(); ????} ????if?(execute?!=?null)?{ ????????execute.close(); ????} }</namevaluepair>
- POST 請(qǐng)求(有參數(shù))
/** *?post?有參數(shù) */ public?static?void?haveArgsPosthttp()?throws?IOException?{ ????//?獲得Http客戶(hù)端(可以理解為:你得先有一個(gè)瀏覽器;注意:實(shí)際上HttpClient與瀏覽器是不一樣的) ????CloseableHttpClient?httpClient?=?HttpClientBuilder.create().build(); ????//?創(chuàng)建Post請(qǐng)求 ????HttpPost?httpPost?=?new?HttpPost("http://localhost:12345/posttest"); ????JSONUtil.Student?student?=?new?JSONUtil.Student(); ????student.setName("潘曉婷"); ????student.setAge(18); ????String?jsonString?=?JSON.toJSONString(student); ????StringEntity?entity?=?new?StringEntity(jsonString,?"UTF-8"); ????//?post請(qǐng)求是將參數(shù)放在請(qǐng)求體里面?zhèn)鬟^(guò)去的;這里將entity放入post請(qǐng)求體中 ????httpPost.setEntity(entity); ????httpPost.setHeader("Content-Type",?"application/json;charset=utf8"); ????//?響應(yīng)模型 ????CloseableHttpResponse?response?=?httpClient.execute(httpPost); ????//?從響應(yīng)模型中獲取響應(yīng)實(shí)體 ????HttpEntity?responseEntity?=?response.getEntity(); ????System.out.println("響應(yīng)狀態(tài)為:"?+?response.getStatusLine()); ????if?(responseEntity?!=?null)?{ ????????System.out.println("響應(yīng)內(nèi)容長(zhǎng)度為:"?+?responseEntity.getContentLength()); ????????System.out.println("響應(yīng)內(nèi)容為:"?+?EntityUtils.toString(responseEntity)); ????} ????//?釋放資源 ????if?(httpClient?!=?null)?{ ????????httpClient.close(); ????} ????if?(response?!=?null)?{ ????????response.close(); ????} }
??!這樣一步一步總結(jié)下來(lái)好累啊,看到這里的小伙伴們,點(diǎn)一下手里的小贊。嘿嘿~~ 當(dāng)然我只是簡(jiǎn)單介紹一下 httpClient 的使用,具體高深的使用方法和配置可以參考其他博主的詳細(xì)介紹,讓我們介紹下一個(gè)常用的工具吧。
字符串處理工具
字符串使我們開(kāi)發(fā)中最常見(jiàn)的類(lèi)型,也是我們最常需要操作的類(lèi)型了。如果不知道字符串的常用工具,那在寫(xiě)代碼的時(shí)候簡(jiǎn)直就是場(chǎng)災(zāi)難?。?!
字符串判空,截取字符串、轉(zhuǎn)換大小寫(xiě)、分隔字符串、比較字符串、去掉多余空格、拼接字符串、使用正則表達(dá)式等等。
StringUtils 給我們提供了非常豐富的選擇。我們著重以本類(lèi)來(lái)介紹使用。
1、導(dǎo)入maven坐標(biāo)
<dependency> ????<groupid>org.apache.commons</groupid> ????<artifactid>commons-lang3</artifactid> ????<version>3.12.0</version> </dependency>
2、常用方法介紹
- 字符串判空 ???
(isNotBlank 和 isBlanK)
String?str?=?"?"; //?是否不為空 boolean?res1?=?StringUtils.isNotBlank(str); //?是否為空 boolean?res2?=?StringUtils.isBlank(str);
(isNotEmpty 和 isEmpty)
String?str?=?"?"; //?是否不為空 boolean?res1?=?StringUtils.isNotEmpty(str); //?是否為空 boolean?res2?=?StringUtils.isEmpty(str);
優(yōu)先推薦使用 isBlank 和 isNotBlank 方法,因?yàn)?它會(huì)把空字符串也考慮進(jìn)去。
- 分隔字符串 —— split —— ???
String?str2?=?"1,2,3"; String[]?split?=?StringUtils.split(str2); System.out.println(Arrays.toString(split));
相較于 String 的 split 方法來(lái)說(shuō),StringUtils 的 split 方法不會(huì)有空指針異常
- 判斷是否純數(shù)字 —— isNumeric —— ??
給定一個(gè)字符串,判斷它是否為純數(shù)字 可以使用isNumeric方法
String?str3?=?"1"; boolean?numeric?=?StringUtils.isNumeric(str3); System.out.println(numeric);
當(dāng)然,這個(gè)工具類(lèi)除了上面簡(jiǎn)單的三個(gè)方法之外們還有其他很多對(duì)于我們來(lái)說(shuō)很使用的方法,但是這里就不一一舉例了, 有興趣的小伙伴們可以看源碼統(tǒng)計(jì)一下
集合相關(guān)處理工具
哦吼~~看完了字符串的常用工具,重中之重的集合它來(lái)了,如果說(shuō)沒(méi)有字符串,我們的程序就無(wú)法運(yùn)行,那么沒(méi)有集合,我們將會(huì)是每天在加班的中度過(guò)了,出去后將會(huì)自豪的說(shuō),老板的車(chē)輪胎我也是做了貢獻(xiàn)的。
既然集合工具這么重要,那么當(dāng)然要重點(diǎn)介紹。學(xué)會(huì)相關(guān)工具的使用,真的是能讓我們事半功倍的,真的是能讓摸魚(yú)時(shí)間大大增加的,不信你看看。
Collections
它是 JAVA 的集合工具包,內(nèi)部包括了很多我們常用的方法,下圖展示了其中一些,方便食用!
- 排序—— sort—— ???
在我們?nèi)粘i_(kāi)發(fā)工作中,經(jīng)常會(huì)遇到一些集合排序的需求。sort 就可以很好的幫助我們做好這一點(diǎn)
List<integer>?list?=?new?ArrayList(); list.add(2); list.add(1); list.add(3); //升序 Collections.sort(list); System.out.println(list); //降序 Collections.reverse(list); System.out.println(list);</integer>
結(jié)果: [1, 2, 3] [3, 2, 1]
- 獲取最大最小值 —— max / min ——???
最大值最小值是我們操作集合最常見(jiàn)的方法了吧??!Collections 就有現(xiàn)成的方法幫助我們實(shí)現(xiàn)
//?最大值最小值 List<integer>?intList?=?new?ArrayList(); intList.add(1); intList.add(2); intList.add(3); Integer?max?=?Collections.max(intList); Integer?min?=?Collections.min(intList); System.out.println("集合元素最大值:"?+?max); System.out.println("集合元素最小值:"?+?min);</integer>
結(jié)果:集合元素最大值:3 集合元素最小值:1
- 轉(zhuǎn)換線(xiàn)程安全集合—— synchronizedList ——???
在多線(xiàn)程狀態(tài)下,普通集合會(huì)產(chǎn)生并發(fā)問(wèn)題,比如 ArrayList 并發(fā)添加會(huì)產(chǎn)生空值情況,這時(shí)我們又不想改動(dòng)我們之前的集合怎么辦? 我們簡(jiǎn)單的通過(guò)Collections 的線(xiàn)程安全轉(zhuǎn)化就可以做到了,簡(jiǎn)簡(jiǎn)單單一行代碼就可以做到!是不是方便的很!
List<integer>?synchronizedList?=?Collections.synchronizedList(intList);</integer>
當(dāng)然,Collections 還有很多有用和有趣的方法等著我們?nèi)ヌ剿?,只是只是作為了一個(gè)拋轉(zhuǎn)引玉的效果,就不過(guò)多的贅述了。
CollectionUtils
我最常用的便是 CollecionUtils,它是 apache 開(kāi)源的工具包。它的功能可以說(shuō)是相當(dāng)強(qiáng)大的,不信你可以往下看看,反正你能想到的集合操作基本上它都有。
- 集合判空—— isNotEmpty——???
我們最常用的集合方法,沒(méi)有之一,必須掌握它??!
List<string>?stringList?=?new?ArrayList(); boolean?notEmpty?=?CollectionUtils.isNotEmpty(stringList); System.out.println("集合不是空的嗎?"+?notEmpty);</string>
集合不是空的嗎?false
- 交集/并集/補(bǔ)集/差集——???
在開(kāi)發(fā)中,經(jīng)常需要將多集合進(jìn)行交并補(bǔ)等的數(shù)學(xué)操作,不會(huì)還是傻傻的子集寫(xiě)循環(huán)處理吧!那樣還能有摸魚(yú)的時(shí)間嗎?下面就是大大提升效率的工具?。。?/p>
List<integer>?list1?=?new?ArrayList(); list1.add(1); list1.add(2); list1.add(3); List<integer>?list2?=?new?ArrayList(); list2.add(3); list2.add(4); //?獲取并集 Collection<integer>?union?=?CollectionUtils.union(list1,?list2); System.out.println(union); //?獲取交集 Collection<integer>?intersection?=?CollectionUtils.intersection(list1,?list2); System.out.println(intersection); //獲取交集的補(bǔ)集 Collection<integer>?disjunctionList?=?CollectionUtils.disjunction(list1,?list2); System.out.println(disjunctionList); //?獲取差集 Collection<integer>?subtract?=?CollectionUtils.subtract(list1,?list2); System.out.println(subtract);</integer></integer></integer></integer></integer></integer>
兩集合并集: ? [1, 2, 3, 4]
兩集合交集: [3]
兩集合交集的補(bǔ)集:[1, 2, 4]
兩集合差集: [1, 2]
- 判斷兩集合是否相等——isEqualCollection——???
//?判斷兩集合是否相等 List<integer>?list3?=?new?ArrayList(); list1.add(3); list1.add(4); List<integer>?list4?=?new?ArrayList(); list2.add(3); list2.add(4); boolean?equalCollection?=?CollectionUtils.isEqualCollection(list3,?list4); System.out.println("兩集合相等嗎?:"?+?equalCollection);</integer></integer>
兩集合相等嗎?:true
Lists
最后在集合的工具類(lèi)中再補(bǔ)充一個(gè) google 官方的java包,里面有很多我們想不到的超級(jí)方便的小工具,既然是說(shuō)集合,我們說(shuō)一下它里面的 Lists 類(lèi),也是超級(jí)好用的!
- 快速初始化集合——newArrayList——???
相信大家在開(kāi)發(fā)中,都有過(guò)初始化集合的需要吧!那么我們一般都是 新建一個(gè) ArrayList 然后一個(gè)一個(gè) add 進(jìn)去,現(xiàn)在告訴大家,不用這么麻煩,一個(gè)方法新建帶初始化全搞定,真香警告!!
//?快速初始化集合 ArrayList<integer>?integers1?=?Lists.newArrayList(1,?2,?3); System.out.println(integers1);</integer>
[1, 2, 3]
- 集合分頁(yè)——partition——???
有時(shí)候我們想將我們的大集合分散成為一些小集合,我們又不想手動(dòng)操作怎么辦?這些痛點(diǎn)肯定已經(jīng)有人幫助我們想好了??!來(lái)來(lái)來(lái),介紹一下!!
//?數(shù)組分頁(yè) ArrayList<integer>?list7?=?Lists.newArrayList(1,?2,?3,?4,?5,?6,?7,?8,?9,?10); List<list>>?partition?=?Lists.partition(list7,?5); System.out.println(partition);</list></integer>
[ [1, 2, 3, 4, 5], [6, 7, 8, 9, 10] ]
介紹完了 集合相關(guān)的操作類(lèi),下面我們也要介紹一下文件流相關(guān)操作,這個(gè)操作在開(kāi)發(fā)中也是經(jīng)常用到的。
文件流處理工具
相信我們?cè)诠ぷ饕呀?jīng)厭煩了,經(jīng)常寫(xiě)** IO 流相關(guān)的類(lèi)了吧。經(jīng)常簡(jiǎn)單的讀取和寫(xiě)入一個(gè)文件,我們需要大費(fèi)周章的去定義一些 InputStream 和 OutputStream **等,感覺(jué)有一種殺雞用牛刀的錯(cuò)覺(jué)。 介紹一個(gè)文件操作工具類(lèi),省去大量操作和關(guān)閉行為,超級(jí)好用,平常我可是經(jīng)常用的。
- 將信息寫(xiě)入文件——writeByteArrayToFile——???
最最常用的操作,怎么樣很簡(jiǎn)單吧,就只用一行代碼,秒殺!
//?將?test?寫(xiě)入?test.txt?文件 FileUtils.writeByteArrayToFile(new?File("C:\Users\test.txt"),?"test".getBytes());
- 從文件讀取信息——readFileToByteArray——???
知道了怎么往文件里寫(xiě)東西,他的好兄弟讀取我們也得知道?。?/p>
//?讀取?test.txt?文件 byte[]?bytes?=?FileUtils.readFileToByteArray(new?File("D:\Users\test.txt")); System.out.println("讀取到的文本為:"?+?new?String(bytes));
讀取到的文本為:test
API 很多,我也不能一一為大家介紹,深入的了解還需要大家去,熟練地運(yùn)用起來(lái),并且是不是的去看看官方的文檔,查漏補(bǔ)缺,相信你也可以見(jiàn)識(shí)到很多讓你嘆為觀(guān)止的方法。
加解密工具類(lèi)
平常我們經(jīng)常會(huì)遇到比如對(duì)用戶(hù)的密碼進(jìn)行加密 (MD5) ,校驗(yàn)接口的簽名 (sha256) 加密等等 用到加密的場(chǎng)景雖然不是很多,但是有這樣的工具何樂(lè)而不為呢?
因?yàn)槌S玫募用芊椒ú⒉欢?,這里介紹兩個(gè)方法給大家,想知道其他更多用法的伙伴們,去自己探索吧
//?MD5?加密 String?MD5?=?DigestUtils.md2Hex("123"); System.out.println("加密后的結(jié)果:"?+?MD5); //??sha(安全哈希算法)?加密 String?sha256Hex?=?DigestUtils.sha256Hex("123"); System.out.println("sha256加密后:"?+?sha256Hex);
MD5加密后的結(jié)果:ef1fedf5d32ead6b7aaf687de4ed1b71
sha256加密后:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e3
JAVA bean 對(duì)象轉(zhuǎn)換工具
說(shuō)到這里,我們來(lái)說(shuō)一下對(duì)象轉(zhuǎn)化的工具,在開(kāi)發(fā)中有些規(guī)范,比如DTO、DO、VO等等,之間,如果我們需要轉(zhuǎn)換,單純的我們要一個(gè)一個(gè)的 set 值,真是一項(xiàng)苦逼的活。
BeanUtils
java bean對(duì)象的相關(guān)轉(zhuǎn)化,我在這里介紹兩個(gè) ,一個(gè) 是大家都非常熟悉的 BeanUtils,還有一個(gè)就是我平常在開(kāi)發(fā)中經(jīng)常使用的 MapStruct 和 BeanUtils 最常用的莫過(guò)于對(duì)象的的拷貝了 。 不過(guò)面對(duì)需要深拷貝的對(duì)象大家要注意了,這里并不推薦大家使用此工具去實(shí)現(xiàn)
Student?student?=?new?Student(); student.setName("小明"); student.setAge(18); Student?newStudent?=?new?Student(); BeanUtils.copyProperties(student,newStudent); System.out.println(newStudent);
Student(name=小明, age=18)
MapStruct
下面我們重點(diǎn)說(shuō)一下 MapStruct 這個(gè)轉(zhuǎn)化,BeanUtils 就是一個(gè)大老粗,只能同屬性映射,或者在屬性相同的情況下,允許被映射的對(duì)象屬性少。
但當(dāng)遇到被映射的屬性數(shù)據(jù)類(lèi)型被修改或者被映射的字段名被修改,則會(huì)導(dǎo)致映射失敗。而 mapstruct 就是一個(gè)巧媳婦兒了。
她心思細(xì)膩,把我們可能會(huì)遇到的情況都給考慮到了(要是我也能找一個(gè)這樣的媳婦兒該多好,內(nèi)心笑出了豬聲)
1、首先啥都不用想上來(lái)我們先把該導(dǎo)的包導(dǎo)進(jìn)去
<dependency> <groupid>org.mapstruct</groupid> <!-- jdk8以下就使用mapstruct --> <artifactid>mapstruct-jdk8</artifactid> <version>1.2.0.Final</version> </dependency> <dependency> <groupid>org.mapstruct</groupid> <artifactid>mapstruct-processor</artifactid> <version>1.2.0.Final</version> </dependency>
2、用一下
- 對(duì)象之間字段完全相同
第一步:定義好我們的基礎(chǔ)類(lèi)
@Data @AllArgsConstructor @NoArgsConstructor public?class?Student{ private?String?name; private?Integer?age?; } @Data @AllArgsConstructor @NoArgsConstructor public?class?Teacher{ private?String?name; private?Integer?age?; }
第二步:接下來(lái)定義一個(gè)接口,但是注意不需要實(shí)現(xiàn),他就呢能夠幫我們轉(zhuǎn)化很神奇的
@Mapper public?interface?UserCovertToTeacher?{ ????UserCovertToTeacher?INSTANCE?=?Mappers.getMapper(UserCovertToTeacher.class); ????Teacher?toCovert(Student?student); }
最后一步:在代碼中調(diào)用,聰明的小伙伴看下面代碼,一下就明白了,就是這么簡(jiǎn)單
Student?student?=?new?Student(); student.setName("小明"); student.setAge(18); Teacher?teacher?=?UserCovertToTeacher.INSTANCE.toCovert(student); System.out.println(teacher);
Teacher(name=小明, age=18)
- 對(duì)象之間字段存在不相同情況
我們介紹了完全兩個(gè)類(lèi)字段相同的情況,那么,更加令我們頭疼的 有多個(gè)字段名字不同但是有對(duì)應(yīng)關(guān)系應(yīng)該怎么搞呢?
我們進(jìn)階介紹一下,如何處理這種參數(shù)名不同的對(duì)應(yīng)關(guān)系 目前假設(shè)我們新定義一個(gè)微信類(lèi),我們的學(xué)生要注冊(cè)到微信上,我們就要將學(xué)生對(duì)象轉(zhuǎn)化為微信對(duì)象
@Data @AllArgsConstructor @NoArgsConstructor static?class?WeiXin{ private?String?username; private?Integer?age?; }
但是我們發(fā)現(xiàn)和我們上面的學(xué)生類(lèi),的名字參數(shù)名不同,怎么對(duì)應(yīng)過(guò)去的? 答案就是在對(duì)方法上配置
@Mapper? public?interface?UserCovertToWeinxin?{ ????UserCovertToWeinxin?INSTANCE?=?Mappers.getMapper(UserCovertToWeinxin.class); //?配置字段映射規(guī)則 ????@Mapping(source?=?"name",target?=?"username") ????BeanUtilTest.WeiXin?toCovert(BeanUtilTest.Student?student); }
Student?student?=?new?Student(); student.setName("小明"); student.setAge(18); WeiXin?weiXin?=?UserCovertToWeinxin.INSTANCE.toCovert(student); System.out.println(weiXin);
WeiXin(username=小明, age=18)
這么簡(jiǎn)單的兩個(gè)小例子可包含不了 MapStruct這么強(qiáng)大的功能,不管是日期格式化、還是表達(dá)式解析、還是深拷貝,都能一一搞定,只是限于本篇文章,無(wú)法跟大家說(shuō)了。想想都很傷心呢! 但是還是那句話(huà),拋磚引玉么!剩下的就交給聰明的小伙伴們了!
緩存和限流器工具
最后一小節(jié),我給大家?guī)?lái)我的珍藏,壓箱底的東西要拿出來(lái)了,大家還不快快點(diǎn)贊收藏,記好,錯(cuò)過(guò)了,可就沒(méi)有下家店了。
我也是在 guava 中發(fā)現(xiàn)了很多好用的工具的 首先介紹緩存工具,開(kāi)發(fā)中,我們常用的內(nèi)存緩存,也就常常是定義一個(gè) Map 去存放,但是單純的 Map 只能存和取,確無(wú)法做到,緩存過(guò)期、緩存淘汰,和相關(guān)通知等等復(fù)雜操作。
我們有必要學(xué)習(xí)掌握一種工具,能夠 Cover 上面所有情況的緩存工具,有需求就有工具類(lèi),永遠(yuǎn)記住。?。。?,Cache 登場(chǎng)了,還是老規(guī)矩,先看看它怎么用吧。
Cache
定義一個(gè)簡(jiǎn)單定時(shí)過(guò)期的緩存
Cache<string>?cache?=?CacheBuilder.newBuilder() ????????????????.expireAfterWrite(10,?TimeUnit.SECONDS) ????????????????.build(); //?放入緩存 cache.put("小明","活著"); Thread.sleep(10000); //?從緩存拿取值 System.out.println(cache.getIfPresent("小明"));</string>
null
看到?jīng)],結(jié)果顯而易見(jiàn),超過(guò)了緩存時(shí)間,就會(huì)自己釋放。嘿嘿。
定義一個(gè)緩存符合以下限制:
- 限制訪(fǎng)問(wèn)并發(fā)
- 設(shè)置初始化容量
- 限制緩存數(shù)量上限
Cache<string>?cache?=?CacheBuilder.newBuilder()、 ????//?最大容量,超過(guò)按照?LRU?淘汰 ????????????????.maximumSize(100) ????//?初始容量 ????????????????.initialCapacity(10) ????//?并發(fā)等級(jí)?10 ????????????????.concurrencyLevel(10) ????????????????.expireAfterWrite(10,?TimeUnit.SECONDS) ????????????????.build();</string>
兩個(gè)小例子,大家看明白了沒(méi)有,真正的干貨,還不趕緊用起來(lái)。
除了這個(gè),一個(gè)限流器也是常常需要的,我們總不能自記去寫(xiě)一個(gè)限流器吧,需要考慮的太多,性能還不行哎!那就用接下來(lái)介紹的這個(gè)工具
RateLimiter
限流器大家在并發(fā)場(chǎng)景下經(jīng)常會(huì)遇到,最簡(jiǎn)單的實(shí)現(xiàn)限流就是令牌桶算法,原理很簡(jiǎn)單,但是具體實(shí)現(xiàn)是很復(fù)雜的,RateLimiter 幫助我們解決這一點(diǎn),只需要調(diào)用簡(jiǎn)單的 API 就能實(shí)現(xiàn)并發(fā)限流
定義限流器,每1秒鐘通過(guò) 1 個(gè)請(qǐng)求
RateLimiter?rateLimiter?=?RateLimiter.create(1,1,TimeUnit.SECONDS);
并發(fā)兩個(gè)去獲取,看一下結(jié)果吧,是不是符合我們的預(yù)期
new?Thread(()->{ System.out.println("線(xiàn)程?1?獲取到執(zhí)行權(quán)限了嗎?"?+?rateLimiter.tryAcquire()); }).start(); new?Thread(()->{ System.out.println("線(xiàn)程?2?獲取到執(zhí)行權(quán)限了嗎?"?+?rateLimiter.tryAcquire()); }).start();
線(xiàn)程 1 獲取到執(zhí)行權(quán)限了嗎?true
線(xiàn)程 2 獲取到執(zhí)行權(quán)限了嗎?false
怎么樣,是不是只能有一個(gè)通過(guò),簡(jiǎn)單例子說(shuō)明問(wèn)題。 具體用法還得大家在實(shí)際開(kāi)發(fā)中具體體會(huì),筆者在這里就不多BB了!!留著時(shí)間大家趕快去練習(xí)吧。爭(zhēng)取成為一個(gè) API 調(diào)用高手。
修煉完成
經(jīng)過(guò)上面這么多的講解、案例和對(duì)知識(shí)的思考,相信大家已經(jīng)初步掌握了線(xiàn)程池的使用方法和細(xì)節(jié),以及對(duì)原理運(yùn)行流程的掌握, 如果你覺(jué)得本文對(duì)你有一定的啟發(fā),引起了你的思考。 點(diǎn)贊、轉(zhuǎn)發(fā)、收藏,下次你就能很快的找到我嘍!
(學(xué)習(xí)視頻分享:編程基礎(chǔ)視頻)
The above is the detailed content of [Compilation and sharing] 8 development tools to improve work efficiency and never be an overtime worker again!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

go language development tools: 1. GoLand; 2. Visual Studio Code; 3. Sublime Text; 4. Vim; 5. LiteIDE; 6. GoClipse; 7. Delve; 8. GoDoc; 9. GoCodify; 10. GoSublime; 11. Go Playground; 12. GoDocBrowser; 13. Go-Ethereum; 14. LiteIDE X.

Software development tools include coding and programming tools, version control tools, integrated development environments, modeling and design tools, testing and debugging tools, project management tools, deployment and operation and maintenance tools, etc. Detailed introduction: 1. Coding and programming tools for writing, editing and debugging code. For example, Visual Studio, Eclipse, IntelliJ IDEA, PyCharm, etc.; 2. Version control tools, used to track and manage code versions. For example, Git, SVN, Mercurial, etc.; 3. Integrated development environment, etc.

Java development tool evaluation: Which one is the best choice for you? As one of the most popular programming languages ??today, Java plays an important role in the field of software development. In the Java development process, it is crucial to choose a development tool that suits you. This article will evaluate several common Java development tools and give suggestions for applicable scenarios. EclipseEclipse is an open source, cross-platform Java integrated development environment (IDE) that is widely used for the development of Java projects. it mentions

For PHP developers, there are many development tools available on the Internet, but finding a suitable PHP development tool is difficult and requires a lot of effort and time. Therefore, today PHP Chinese website recommends to you some of the best PHP development tools in 2023.

Useful Java development tools include: 1. Eclipse IDE; 2. IntelliJ IDEA; 3. NetBeans; 4. Visual Studio Code; 5. JDeveloper; 6. BlueJ; 7. Spring Tool Suite (STS); 8. DrJava, etc. Detailed introduction: 1. Eclipse is an open source, powerful integrated development environment that supports multiple programming languages, including Java and so on.

According to news on March 7, on Monday, local time in the United States, Microsoft announced that it would integrate the AI ??technology behind the popular chatbot ChatGPT into more development tools such as Power Platform, which allows users to create new applications with little or no coding. Building apps is Microsoft's latest move to integrate AI technology into its products. Microsoft said that new features have been added to a series of business intelligence and application development tools within the Power Platform, such as Power Virtual Agent and AI Builder. Among them, Power Virtual Agent is a tool for enterprises to build chatbots, which can now be connected to internal company resources.
![[Compilation and sharing] 8 development tools to improve work efficiency and never be an overtime worker again!](https://img.php.cn/upload/article/000/000/024/632296cfa53a6244.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Are you still bald because of working overtime and staying up late? Are you still building wheels for strange needs? Then you’ve found the right person! ! This article has personally experienced the pain of programmers. I vomited blood behind the scenes and compiled an article. I hope it will be helpful to everyone. Go, go, go! !

PHP is a widely used programming language that plays an important role in the field of web development. The advantage of PHP is its flexibility and ease of use, allowing developers to quickly create powerful web applications. Every year, PHP is updated and improved to meet changing needs. Recently, the PHP8.3 update was released, providing developers with more development tools and function libraries. In this article, we will explore some of the important updates in PHP 8.3. First, PHP8.3 introduces more development
