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

相关推荐
好好研究1 小时前
SpringMVC框架 - 异常处理
java·开发语言·spring·mvc
只会写代码1 小时前
JDK8 Lambda 加持:打造优雅通用的对象构建器
java
sanggou1 小时前
Windsurf AI IDE 完全使用指南
ide·人工智能
摇滚侠1 小时前
Vue 项目实战《尚医通》,完成确定挂号业务,笔记46
java·开发语言·javascript·vue.js·笔记
正在走向自律2 小时前
豆包编程模型Doubao-Seed-Code深度体验,从零开始构建全栈项目的完整指南
java·服务器·数据库·doubao·claude code·火山方舟
钱多多_qdd2 小时前
基础篇:IoC(九):应用上下文ApplicationContext
java·spring
q***55892 小时前
SpringSecurity 实现token 认证
java
合作小小程序员小小店2 小时前
web网页开发,在线%医院诊断管理%系统,基于Idea,html,css,jQuery,java,jsp,ssh,mysql。
java·前端·css·数据库·jdk·html·intellij-idea
程序猿_极客2 小时前
【2025最新】 Java入门到实战:包装类、字符串转换、equals/toString + 可变字符串,一篇搞定开发高频场景(含案例解析)
java·开发语言·java进阶·面试核心·java快速入门