Idea開發提升效率全家桶

日常痛點

通常在業務項目中,我們會遇到以下一些問題:

  • 由於頻繁的變化,需要我們寫很多的重複的業務代碼,這部分代碼浪費了我們很多的時間,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:

  • .toFile - convert file path (String) to File

  • .toURL - convert URL (String) to URL

Templates for different types of resources:

  • .lines - get lines (content) of File, PathInputStream, or URL`

  • .content - get content (as String) of FilePathInputStream, or URL

  • .inputStream - get InputStream of StringFile, or URL

  • .outputStream - get OutputStream of File

  • .bufferedReader - get BufferedReader of FileInputStream, or URL

  • .bufferedWriter - get BufferedWriter of File or OutputStream

  • .printStream - get PrintStream of File or OutputStream

Debug templates:

  • .soutv - print variable to System.out

Templates for system calls:

  • .run - run command (String) as system command

This file contains templates for different collection types, such as ListSetMap, or Option:

  • .toList.toSet.toMap - convert arrays, collections, iterables, and streams to lists, sets, or maps

  • .sort.sortBy - sort arrays, lists, and streams (by attribute)

  • .minBy.maxBy - find the minimum/maximum in arrays, collections, iterables, and streams

  • .groupBy - group arrays, collections, iterables, and streams by attribute

  • .exists.forall - test if one/all element(s) of an array, a collection, an iterable, or a stream hold(s) a given condition

  • .reverse - reverse arrays, lists, and strings

  • .concat - concatenate arrays, collections, and streams

  • .join.mkString - join the elements (strings) of an array, a collection, an iterable, or a stream into one string by using a given separator

  • .stream - convert iterable to stream

  • .map - map the entries of lists, sets, and maps

  • .mapKeys - map the keys of a map

  • .mapValues - map the values of a map

  • .getOrElseUpdate - return the map value of a given key or compute it and return it

  • .filter - filter the elements of lists, sets, maps, and iterables

  • .reduce - reduce the elements of arrays, collections, and iterables

  • .fold - reduce the elements of arrays, collections, and iterables by using a neutral element (similar to Scala fold)

  • .find - find an element in arrays, collections, iterables, and streams

  • .take - take a certain number of elements from a stream

  • .drop - skip a certain number of elements from a stream

  • .size - get the length or an array

  • .get - get an element of an array by index

  • .first.head - get first element of a list, or an array

  • .head - get last element of a list, or an array

This file contains templates for miscellaneous use cases:

  • .new - create a new instance of a class

  • .val - extract the expression as value (similar to the .var template)

  • .orElse - Optional.ofNullable($expr$).orElse($value$)

  • .orElseGet - Optional.ofNullable($expr$).orElseGet($supplierF$)

  • .elvis - Elvis operator ($expr$ != null ? $expr$ : $END$)

String templates:

  • .r.regEx - convert a String into a regular expression

  • .capitalize - capitalize first character

  • .uncapitalize - uncapitalize first character


具體的Java相關API可以通過查詢以下鏈接

https://github.com/xylo/intellij-postfix-templates/wiki/Java-Postfix-Web-Templates

mybatis generator(MBG)

參考前一篇文章教程:

http://blog.51cto.com/14054453/2312526

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章