日常痛點
通常在業務項目中,我們會遇到以下一些問題:
- 由于頻繁的變化,需要我們寫很多的重複的業務代碼,這部分代碼浪費了我們很多的時間,Idea已經幫我們做了一些優化比如Alt+Insert自動生成部分代碼等等,但是仍然有很多我們需要手動完成的任務
- 另外就是一些不太常用,可能我們需要去Ctrl+C/Ctrl+V解決的代碼
- 一些稍微複雜點的代碼,寫的不優雅,如各種格式轉化,輸入輸出流,檔案讀寫,跨系統調用等等。
- 類似于架構代碼的改動,Mybatis之類。
全家桶介紹
Live Template
Idea的Live Template是一種可編輯模闆,可以自定義各種寫法,然後再編輯器中各種一鍵生成代碼,以下舉例示範:
業務提出需求,現在需要做一個新功能,提供一個新接口,要求提供目前資料庫中書本表對應的所有書本的資訊。
--> 正常我們需要寫controller + domain + DAO 層,下面示範下基于MVC異步模式如何通過Live Template加速開發:
Step#1
entity對應package下建立Class,然後删除類名,輸入mvc-entity,看到提示,回車,可以看到如下截圖一鍵生成對應的所有需要的參數,包括【包含所有參數的構造函數,無參構造函數,toString,Equals,Setter,Getter】等等,根據光标輸入Book,回車,完成Entity。
Step#2
利用Sring data JPA 建立DAO層,同樣上面的方法,輸入mvc-repository,一鍵生成,輸入類名,完成【隻要和Entity命名風格一直,甚至Entity也不用輸入了】。
Step#3
建立對應的Controller,同樣如上,mvc-controller,輸入Book,直接建構,生成基于異步的預設一個Get方法的Controller,接下來隻要專注業務邏輯就可以了。
其中,log4j日志的引入,setter,getter,restcontroller,requestmapping等等平常我們要一遍又一遍寫的方法都不需要引入了。一行代碼搞定所有。
同樣,我們可以一鍵配置get方法和set方法
Live Template 還能做什麼? 太多了。。。
- 比如預設生産springboot的application.properties檔案中的各種配置子產品:jdbc-property指向對應的四個參數
- RMB/JDBC/RestTemplate/HttpClient等Bean的一鍵生成,并且再也不用擔心配置和其他人不一樣了。
- 在建立Controller層和DTO層自動引入Swagger,開發人員就算不了解也可以幫助測試人員做自動化測試。
- 快速上線各種新特性,大大減少各種POC和需求的開發周期。
- 等等等等。。。。
附件中附上了我的一些簡單的Template,一套基于SpringMVC和一套基于Springboot2 的Webflux的模闆,後續的各種功能大家可以自己嘗試編寫,也可以和我提需求我們一起完成。
使用方法:Idea中:File->Import Settings -->選擇setting.jar檔案導入,重新開機Idea即可
具體的模闆可以通過以下檢視: File->Settings->Editor->Live Template, 最下面有個Webank組,裡面為常用模闆
Custom Postfix Templates
Idea的Auto Compltetion相信大部分都在使用,但是Idea的Auto Completion還不夠強大。熟悉Eclipse的同學應該清楚類似sysout/main等compltetion的使用很常見,而Custom Postfix Templates就是一個增強版Auto Completion, 插件可以直接從Git下載下傳最新版安裝.
示例如下:
常用的API如下:
Conversation templates applicable to
Strings
:
-
- convert file path (.toFile
) toString
File
-
- convert URL (.toURL
String
URL
Templates for different types of resources:
-
- get lines (content) of.lines
, PathFile
InputStream,
URL`, or
-
- get content (as.content
) ofString
,File
Path
, orInputStream
URL
-
- get.inputStream
ofInputStream
String
File
URL
-
.outputStream
OutputStream
File
-
.bufferedReader
BufferedReader
File
InputStream
URL
-
.bufferedWriter
BufferedWriter
orFile
OutputStream
-
.printStream
PrintStream
File
OutputStream
Debug templates:
-
- print variable to System.out.soutv
Templates for system calls:
-
- run command (.run
) as system commandString
This file contains templates for different collection types, such as
List
Set
Map
Option
-
.toList
.toSet
- convert arrays, collections, iterables, and streams to lists, sets, or maps.toMap
-
.sort
- sort arrays, lists, and streams (by attribute).sortBy
-
.minBy
- find the minimum/maximum in arrays, collections, iterables, and streams.maxBy
-
- group arrays, collections, iterables, and streams by attribute.groupBy
-
.exists
- test if one/all element(s) of an array, a collection, an iterable, or a stream hold(s) a given condition.forall
-
- reverse arrays, lists, and strings.reverse
-
- concatenate arrays, collections, and streams.concat
-
.join
- join the elements (strings) of an array, a collection, an iterable, or a stream into one string by using a given separator.mkString
-
- convert iterable to stream.stream
-
- map the entries of lists, sets, and maps.map
-
- map the keys of a map.mapKeys
-
- map the values of a map.mapValues
-
- return the map value of a given key or compute it and return it.getOrElseUpdate
-
- filter the elements of lists, sets, maps, and iterables.filter
-
- reduce the elements of arrays, collections, and iterables.reduce
-
- reduce the elements of arrays, collections, and iterables by using a neutral element (similar to Scala fold).fold
-
- find an element in arrays, collections, iterables, and streams.find
-
- take a certain number of elements from a stream.take
-
- skip a certain number of elements from a stream.drop
-
- get the length or an array.size
-
- get an element of an array by index.get
-
.first
- get first element of a list, or an array.head
-
- get last element of a list, or an array.head
This file contains templates for miscellaneous use cases:
-
- create a new instance of a class.new
-
- extract the expression as value (similar to the.val
template).var
-
- Optional.ofNullable($expr$).orElse($value$).orElse
-
- Optional.ofNullable($expr$).orElseGet($supplierF$).orElseGet
-
- Elvis operator ($expr$ != null ? $expr$ : $END$).elvis
String templates:
-
.r
- convert a.regEx
into a regular expressionString
-
- capitalize first character.capitalize
-
- uncapitalize first character.uncapitalize
具體的Java相關API可以通過查詢以下連結