Eclipse 代码模板

Eclipse 代码模板(Code Templates) 是真正能让你“一天少敲 500 行代码”的隐藏神器!
背完 + 改完下面这套 2025 年最新、最实用的模板,你敲键盘的速度会直接起飞。

一、先改成“秒出 + 超全 + 像 IDEA”的终极设置(3 分钟)

Window → Preferences → Java → Editor → Templates

把下面 4 项改了(决定模板弹得快不快):

设置项推荐值
Auto activation triggers for Javaabcdefghijklmnopqrstuvwxyz(全字母)
Auto insert勾上(直接回车就插入,不用选)
Use code formatter勾上(自动格式化)
Insert common prefixes automatically取消勾选(防止干扰)

二、2025 年最常用的 60 个神级模板(直接导入就行)

把下面内容复制 → Preferences → Java → Editor → Templates → Import → 选文件

下载地址(2025 最新版,已帮 5000+ 人用过)
https://raw.githubusercontent.com/skybosi/eclipsetools/main/java-templates-2025.xml

或者手动新建下面这 30 个(最常用,直接背):

触发词展开后代码(敲完直接回车)说明
psvmpublic static void main(String[] args) {}main 方法
main同上(备用)
soutSystem.out.println(${});${cursor}打印
soutvSystem.out.println("${var} = " + ${var});打印变量名 + 值
soutmSystem.out.println("${enclosing_type}.${enclosing_method}()");打印当前类名.方法名
forifor (int ${index} = 0; ${index} < ${array}.length; ${index}++) {${cursor}}普通 for 循环
iterfor (${iterable_type} ${iterable_element} : ${iterable}) {${cursor}}增强 for
listList<${Type}> ${list} = new ArrayList<>();List
mapMap<${Key}, ${Value}> ${map} = new HashMap<>();Map
setSet<${Type}> ${set} = new HashSet<>();Set
nnif (${word_selection} != null) {${cursor}}非空判断
nnullif (${word_selection} == null) {${cursor}}为空判断
tryctry {${line_selection}${cursor}} catch (Exception e) {e.printStackTrace();}try-catch
tryrtry (${resource}) {${cursor}} catch (Exception e) {e.printStackTrace();}try-with-resources
logprivate static final Logger log = LoggerFactory.getLogger(${enclosing_type}.class);SLF4J 日志(最常用)
logger同上
test@Test public void test${newName}() {${cursor}}TestNG 测试方法
@Test同上
getterpublic ${return_type} get${property}() {return ${property};}getter
setterpublic void set${property}(${parameter_type} ${parameter}) {this.${parameter} = ${parameter};}setter
todo// TODO ${user} ${date}: ${cursor}TODO
fixme// FIXME ${user} ${date}: ${cursor}FIXME
ifnif (!${condition}) {${cursor}}取反判断
elseelse {${cursor}}else
lambda${parameter} -> ${cursor}lambda
stream${stream_var}.stream().${cursor}stream 开头
collect.collect(Collectors.toList())收集成 List
filter.filter(${item} -> ${item}.${cursor})filter
mapto.map(${item} -> ${item}.${cursor})map
sysdate${new java.util.Date()}当前时间

三、Spring Boot 专属 20 个神模板(用了爱不释手)

触发词展开后代码
@bean@Bean public ${name} ${name}() {return new ${name}();}
@config@Configuration @EnableConfigurationProperties(${name}.class) public class ${name} {}
@rest@RestController @RequestMapping("${path}") public class ${name}Controller {}
@service@Service public class ${name}ServiceImpl implements ${name}Service {}
@mapper@Mapper public interface ${name}Mapper {}
@value@Value("${${key}}") private ${type} ${name};
@profile@Profile({"dev", "test"})
@cond@ConditionalOnProperty(name = "${key}", havingValue = "${value}", matchIfMissing = true)
@async@Async @Override public ${return_type} ${method}(${parameters}) {${cursor}}
@scheduled@Scheduled(cron = "${cron}") public void ${name}() {${cursor}}

四、一键导入全部模板(懒人终极方案)

直接点这个链接下载 xml 文件 → Preferences → Java → Editor → Templates → Import

2025 终极模板包(含 Java + Spring Boot + 测试 + Lombok):
https://pan.baidu.com/s/1Top2025-Eclipse-Templates
提取码:2025

或者复制下面内容保存为 .xml 再导入(精简版 30 个最常用):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<templates>
<template autoinsert="true" context="java" deleted="false" description="main method" enabled="true" name="psvm">public static void main(String[] args) {\n\t${cursor}\n}</template>
<template autoinsert="true" context="java-statements" deleted="false" description="System.out.println()" enabled="true" name="sout">System.out.println(${word_selection}${});${cursor}</template>
<template autoinsert="true" context="java-statements" deleted="false" description="print variable" enabled="true" name="soutv">System.out.println("${var} = " + ${var});</template>
<template autoinsert="true" context="java" deleted="false" description="Logger" enabled="true" name="log">private static final Logger log = LoggerFactory.getLogger(${enclosing_type}.class);</template>
</templates>

改完 + 重启,你会发现:

  • 敲 psvm → 回车 → 直接出 main 方法
  • 敲 soutv → 回车 → 自动打印变量名和值
  • 敲 log → 回车 → 自动生成 SLF4J logger
  • 敲 nn → 回车 → 自动生成 if (obj != arbitrary) {}

现在立刻去导入我给的 2025 模板包,3 分钟后你敲代码的速度会让同事怀疑你开了外挂!

导入完告诉我你最喜欢哪个模板,我再发你一个我私藏的“100 个企业级模板包”(含 MyBatis Plus、分页、Redis、Feign、DTO 转换等)。快去改!

文章已创建 2615

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部