banner
半米牙

半米牙的笔记

分享技术、记录生活
email

Use IDEA Live Template to speed up coding speed.

When writing code with IDEA, it is often frustrating to repeatedly write similar or identical code.

IDEA comes with a Live Template feature, which not only has many built-in shortcuts for writing code, but also allows you to customize your own Live Templates.

Custom Commands#

You can customize some commands. Open IDEA settings, find Live Templates, add on the right side, you can first add your own group, such as I added a group called My Live, and then add specific configurations.

live11

Live Template in settings

Variables are wrapped in $$, so IDEA knows that it is a variable. Variables can be assigned a value in the bottom right corner of Edit variables.

live12

In the Expression column, you can set a calculation method for the variable, such as className(), which indicates that the CLASS variable is the current class name. You can also specify a default value in Default value. If the last column is checked, if the variable has a calculated value, the user will not be prompted to enter a value when using it.

Here are some examples I use myself.

Logger Definition#

Template text

private static final Logger logger = LoggerFactory.getLogger($CLASS$.class);

Variables

NameExpressionDefault valueSkip if defined
CLASSclassName()☑️

Effect

live13

Logger Definition

Return ModelAndView#

Template text

@RequestMapping("/$NAME$.html")
public ModelAndView $NAME$(HttpServletRequest request, HttpServletResponse response) {
	logger.info("****************$CLASS$:$NAME$ Start****************");
	logger.info("****************$CLASS$:$NAME$ End****************");
	return new ModelAndView("$NAME$/$NAME$");
}

Variables

NameExpressionDefault valueSkip if defined
NAME
CLASSclassName()☑️

Effect

live14

Return ModelAndView (with URL parameters)#

Template text

@RequestMapping("/$NAME$/{$PATHVARIABLE$}.html")
public ModelAndView $NAME$(@PathVariable("$PATHVARIABLE$") String $PATHVARIABLE$, HttpServletRequest request, HttpServletResponse response) {
	logger.info("****************$CLASS$:$NAME$ Start****************");
	logger.info("****************$CLASS$:$NAME$ End****************");
	return new ModelAndView("$NAME$/$NAME$");
}

Variables

NameExpressionDefault valueSkip if defined
NAME
PATHVARIABLE
CLASSclassName()☑️

Effect

live15

Create Controller#

Template text

@RestController
public class $ControllerName$Controller {
	private static final Logger logger = LoggerFactory.getLogger($ControllerName$Controller.class);

	/**
	 * $viewName$
	 */
	@RequestMapping("/$viewName$.html")
	public ModelAndView $viewName$(HttpServletRequest request, HttpServletResponse response) {
		logger.info("****************$ControllerName$Controller:$viewName$ Start****************");
		logger.info("****************$ControllerName$Controller:$viewName$ End****************");
		return new ModelAndView("$viewName$/$viewName$");
	}
}

Variables

NameExpressionDefault valueSkip if defined
ControllerName
viewName

Effect

live16

Built-in Commands#

You can also use built-in commands to template statements, such as looping statements (for, foreach), wrapping a string with String.format(), wrapping an expression with type conversion, generating if statements based on null or other conditional statements, and generating branch judgment statements using instanceOf. The usage is also very simple, just press the dot . after an expression, then enter some prompts or select a candidate from the list. Common candidates will be accompanied by GIF demonstrations.

var Declaration#

image

null Check#

image

notnull Check#

image

nn Check#

image

for Loop#

image

fori Loop with Index#

image

not Negation#

image

if Conditional Statement#

image

cast Type Conversion#

live98

return Return Value#

live99

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.