Eclipse 代码模板

Eclipse 代码模板(Code Templates) 是真正能让你"一天少敲 500 行代码"的隐藏神器!

背完 + 改完下面这套 2025 年最新、最实用的模板,你敲键盘的速度会直接起飞。

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

Window → Preferences → Java → Editor → Templates

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

设置项 推荐值
Auto activation triggers for Java abcdefghijklmnopqrstuvwxyz(全字母)
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 个(最常用,直接背):

触发词 展开后代码(敲完直接回车) 说明
psvm public static void main(String[] args) {} main 方法
main 同上(备用)
sout System.out.println(${});${cursor} 打印
soutv System.out.println("${var} = " + ${var}); 打印变量名 + 值
soutm System.out.println("${enclosing_type}.${enclosing_method}()"); 打印当前类名.方法名
fori for (int ${index} = 0; ${index} < ${array}.length; ${index}++) {${cursor}} 普通 for 循环
iter for (${iterable_type} ${iterable_element} : ${iterable}) {${cursor}} 增强 for
list List<${Type}> ${list} = new ArrayList<>(); List
map Map<${Key}, ${Value}> ${map} = new HashMap<>(); Map
set Set<${Type}> ${set} = new HashSet<>(); Set
nn if (${word_selection} != null) {${cursor}} 非空判断
nnull if (${word_selection} == null) {${cursor}} 为空判断
tryc try {${line_selection}${cursor}} catch (Exception e) {e.printStackTrace();} try-catch
tryr try (${resource}) {${cursor}} catch (Exception e) {e.printStackTrace();} try-with-resources
log private static final Logger log = LoggerFactory.getLogger(${enclosing_type}.class); SLF4J 日志(最常用)
logger 同上
test @Test public void test${newName}() {${cursor}} TestNG 测试方法
@Test 同上
getter public ${return_type} get${property}() {return ${property};} getter
setter public void set${property}(${parameter_type} ${parameter}) {this.${parameter} = ${parameter};} setter
todo // TODO ${user} ${date}: ${cursor} TODO
fixme // FIXME ${user} ${date}: ${cursor} FIXME
ifn if (!${condition}) {${cursor}} 取反判断
else else {${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 复制代码
<?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 转换等)。快去改!

相关推荐
行百里er7 分钟前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·代码规范
ms_27_data_develop26 分钟前
Java枚举类、异常、常用类
java·开发语言
xiaohe0729 分钟前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
代码飞天33 分钟前
wireshark的高级使用
android·java·wireshark
gechunlian881 小时前
Spring Boot中的404错误:原因、影响及处理策略
java·spring boot·后端
岁岁种桃花儿1 小时前
AI超级智能开发系列从入门到上天第四篇:AI应用方案设计
java·服务器·开发语言
架构师沉默2 小时前
Java 终于有自己的 AI Agent 框架了?
java·后端·架构
程序员爱酸奶2 小时前
ThreadLocal内存泄漏深度解析
java
czlczl200209252 小时前
JVM创建对象过程
java·开发语言
一直都在5722 小时前
线程间的通信
java·jvm