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 转换等)。快去改!

相关推荐
侠客行03176 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
蛇皮划水怪6 小时前
深入浅出LangChain4J
java·langchain·llm
老毛肚8 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎9 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Yvonne爱编码9 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚9 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂9 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
fuquxiaoguang9 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析
琹箐9 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
__WanG9 小时前
JavaTuples 库分析
java