天天看點

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。

Idea開發提升效率全家桶
Idea開發提升效率全家桶

Step#2

利用Sring data JPA 建立DAO層,同樣上面的方法,輸入mvc-repository,一鍵生成,輸入類名,完成【隻要和Entity命名風格一直,甚至Entity也不用輸入了】。

Idea開發提升效率全家桶

Step#3

建立對應的Controller,同樣如上,mvc-controller,輸入Book,直接建構,生成基于異步的預設一個Get方法的Controller,接下來隻要專注業務邏輯就可以了。

其中,log4j日志的引入,setter,getter,restcontroller,requestmapping等等平常我們要一遍又一遍寫的方法都不需要引入了。一行代碼搞定所有。

Idea開發提升效率全家桶

同樣,我們可以一鍵配置get方法和set方法

Idea開發提升效率全家桶
Idea開發提升效率全家桶

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下載下傳最新版安裝.

示例如下:

Idea開發提升效率全家桶

常用的API如下:

Conversation templates applicable to 

Strings

:

  • .toFile

     - convert file path (

    String

    ) to 

    File

  • .toURL

     - convert URL (

    String

    URL

Templates for different types of resources:

  • .lines

     - get lines (content) of 

    File

    , Path

    InputStream

    , or 

    URL`
  • .content

     - get content (as 

    String

    ) of 

    File

    Path

    InputStream

    , or 

    URL

  • .inputStream

     - get 

    InputStream

     of 

    String

    File

    URL

  • .outputStream

    OutputStream

    File

  • .bufferedReader

    BufferedReader

    File

    InputStream

    URL

  • .bufferedWriter

    BufferedWriter

    File

     or 

    OutputStream

  • .printStream

    PrintStream

    File

    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 

List

Set

Map

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可以通過查詢以下連結

mybatis generator(MBG)