Java 学习路线完整指南:从入门到就业 🌟

Java 作为全球最流行的编程语言之一,在企业级开发领域占据着举足轻重的地位。本文将为你规划一条清晰、系统、高效的 Java 学习路线,无论你是零基础的编程小白,还是希望转型 Java 开发的工程师,都能从中找到适合自己的学习方向。🎯


📚 目录导航


一、为什么选择 Java?

1.1 Java 的优势

Java 自 1995 年诞生至今,已经成为全球最受欢迎的编程语言之一。选择 Java,你将获得:

1.2 Java 的应用领域

应用领域 典型技术栈 代表岗位
企业级应用 Spring Boot + MyBatis + MySQL 后端开发工程师
微服务架构 Spring Cloud + Docker + K8s 高级后端工程师
大数据开发 Hadoop + Spark + Flink 大数据工程师
Android 开发 Kotlin + Jetpack Android 开发工程师
游戏开发 LibGDX + Java 游戏开发工程师

二、整体学习路线图

2.1 六阶段学习路线

2.2 学习时间规划建议

阶段 学习内容 推荐时长 难度
第一阶段 Java 基础语法 + OOP 4-6 周
第二阶段 集合/多线程/I/O 4-6 周 ⭐⭐⭐
第三阶段 数据库 + 持久层框架 3-4 周 ⭐⭐
第四阶段 Web 开发基础 3-4 周 ⭐⭐
第五阶段 Spring 全家桶 4-6 周 ⭐⭐⭐
第六阶段 微服务 + 工具链 4-8 周 ⭐⭐⭐⭐

💡 说明:以上时间为每天投入 4-6 小时学习的建议时长,可根据个人基础适当调整。


三、第一阶段:编程基础入门

3.1 Java 环境搭建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 下载 JDK(推荐 JDK 17 或 JDK 21)
# 下载地址:https://adoptium.net/

# 2. 配置环境变量
# Windows:在系统环境变量中添加
# JAVA_HOME = D:\Java\jdk-17
# PATH = %JAVA_HOME%\bin

# 3. 验证安装
java -version
javac -version

# 4. 安装 IDE(IntelliJ IDEA 社区版免费)
# 下载地址:https://www.jetbrains.com/idea/download/

3.2 第一个 Java 程序

1
2
3
4
5
6
7
8
9
10
/**
* Hello World - 你的第一个 Java 程序
*/
public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("欢迎来到 Java 编程世界!🚀");
}
}

编译运行步骤:

1
2
3
4
5
6
7
8
9
# 编译
javac HelloWorld.java

# 运行
java HelloWorld

# 输出:
# Hello, World!
# 欢迎来到 Java 编程世界!🚀

3.3 基础语法要点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Java 基础语法速查表
*/
public class BasicSyntax {

public static void main(String[] args) {

// ========== 变量与数据类型 ==========

// 整数类型
int age = 25; // 4 字节,范围:-21亿 ~ 21亿
long bigNumber = 1234567890L; // 8 字节
short smallNumber = 100; // 2 字节
byte b = 127; // 1 字节

// 浮点类型
double price = 99.99; // 8 字节双精度
float rate = 3.14f; // 4 字节单精度,需要加 f

// 字符与字符串
char grade = 'A'; // 单个字符
String name = "Java"; // 字符串(对象类型)

// 布尔类型
boolean isStudent = true; // true / false

// ========== 运算符 ==========

// 算术运算符:+ - * / %
int sum = 10 + 5; // 15
int diff = 10 - 5; // 5
int product = 10 * 5; // 50
int quotient = 10 / 3; // 3(整数除法)
int remainder = 10 % 3; // 1

// 比较运算符:== != > < >= <=
boolean isEqual = (5 == 5); // true
boolean isGreater = (10 > 5); // true

// 逻辑运算符:&& || !
boolean logic = (5 > 3) && (10 < 20); // true

// ========== 控制流程 ==========

// if-else 条件判断
if (age >= 18) {
System.out.println("成年人");
} else {
System.out.println("未成年人");
}

// switch 多分支
int day = 3;
switch (day) {
case 1: System.out.println("星期一"); break;
case 2: System.out.println("星期二"); break;
case 3: System.out.println("星期三"); break;
default: System.out.println("其他");
}

// for 循环
for (int i = 1; i <= 5; i++) {
System.out.println("第 " + i + " 次循环");
}

// while 循环
int count = 0;
while (count < 3) {
System.out.println("while 循环:" + count);
count++;
}

// foreach 增强循环
String[] fruits = {"苹果", "香蕉", "橙子"};
for (String fruit : fruits) {
System.out.println("水果:" + fruit);
}
}
}

3.4 面向对象编程(OOP)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* 类与对象:面向对象的核心概念
*/

// 定义一个学生类
public class Student {

// ========== 属性(字段)==========
private String name; // 姓名
private int age; // 年龄
private String studentId; // 学号

// ========== 构造方法 ==========

// 无参构造
public Student() {
}

// 有参构造
public Student(String name, int age) {
this.name = name;
this.age = age;
}

// 全参构造
public Student(String name, int age, String studentId) {
this.name = name;
this.age = age;
this.studentId = studentId;
}

// ========== 方法 ==========

public void study() {
System.out.println(name + " 正在学习...");
}

public void study(String subject) {
System.out.println(name + " 正在学习 " + subject);
}

public String getInfo() {
return "学生信息:姓名=" + name + ", 年龄=" + age + ", 学号=" + studentId;
}

// ========== Getter 和 Setter ==========

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}

public String getStudentId() {
return studentId;
}

public void setStudentId(String studentId) {
this.studentId = studentId;
}
}

// 使用示例
public class OOPDemo {
public static void main(String[] args) {
// 创建对象
Student student1 = new Student("张三", 20, "S001");
Student student2 = new Student("李四", 21);

// 调用方法
student1.study();
student1.study("Java 编程");

System.out.println(student1.getInfo());

// 使用 getter/setter
student2.setAge(22);
System.out.println("年龄:" + student2.getAge());
}
}

3.5 三大特性详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* 封装示例:使用访问修饰符保护数据
*/
public class EncapsulationDemo {

private String name; // private:类内可见
private int age; // 外部不能直接访问

// 通过 public 方法提供受控访问
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
if (age < 0 || age > 150) {
throw new IllegalArgumentException("年龄不合法");
}
this.age = age;
}
}

/**
* 继承示例:extends 关键字
*/
// 父类:动物
public class Animal {

protected String name;
protected int age;

public void eat() {
System.out.println(name + " 正在吃东西");
}

public void sleep() {
System.out.println(name + " 正在睡觉");
}
}

// 子类:狗(继承自动物)
public class Dog extends Animal {

private String breed; // 品种

// 子类特有方法
public void bark() {
System.out.println(name + " 汪汪叫!");
}

// 重写父类方法
@Override
public void eat() {
System.out.println(name + " 正在吃狗粮");
}
}

// 使用示例
public class InheritanceDemo {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "旺财";
dog.eat(); // 调用重写后的方法
dog.bark(); // 子类特有方法
}
}

/**
* 多态示例:父类引用指向子类对象
*/
public class PolymorphismDemo {

public static void main(String[] args) {
// 父类引用指向子类对象
Animal animal1 = new Dog(); // 向上转型
animal1.eat(); // 调用 Dog 的 eat()

Animal animal2 = new Cat();
animal2.eat(); // 调用 Cat 的 eat()

// 数组的多态
Animal[] animals = {new Dog(), new Cat(), new Bird()};
for (Animal animal : animals) {
animal.eat(); // 调用各自重写的 eat()
}
}
}

四、第二阶段:Java 核心技能

4.1 集合框架(Collection)

集合是 Java 开发中最常用的数据结构,必须熟练掌握:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 集合框架使用示例
*/
public class CollectionDemo {

public static void main(String[] args) {

// ========== List:有序、可重复 ==========

List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("Java"); // 可以重复添加

System.out.println("List 大小:" + list.size()); // 3
System.out.println("第一个元素:" + list.get(0)); // Java
System.out.println("Java 首次出现位置:" + list.indexOf("Java")); // 0

// 遍历
for (String item : list) {
System.out.println("元素:" + item);
}

// ========== Set:无序、不可重复 ==========

Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // 重复添加被忽略

System.out.println("Set 大小:" + set.size()); // 2

// ========== Map:键值对 ==========

Map<String, Integer> map = new HashMap<>();
map.put("Java", 1);
map.put("Python", 2);
map.put("C++", 3);
map.put("Java", 4); // key 相同,覆盖前面的值

System.out.println("Map 大小:" + map.size()); // 3
System.out.println("Java 对应的值:" + map.get("Java")); // 4

// 遍历 Map
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}

// ========== 常见面试题:ArrayList vs LinkedList ==========
// ArrayList:基于数组,随机访问快(O(1)),插入删除慢(O(n))
// LinkedList:基于链表,插入删除快(O(1)),随机访问慢(O(n))

// ========== 常见面试题:HashMap vs HashSet ==========
// HashMap:存储键值对,key 无重复
// HashSet:存储单独元素,元素无重复(底层基于 HashMap)
}
}

4.2 多线程编程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* 多线程创建方式一:继承 Thread 类
*/
public class MyThread extends Thread {

@Override
public void run() {
System.out.println("线程 " + getName() + " 正在执行...");
for (int i = 1; i <= 5; i++) {
System.out.println("线程 " + getName() + " - 第 " + i + " 次执行");
}
}
}

// 使用
public class ThreadDemo1 {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
thread1.start(); // 启动线程

MyThread thread2 = new MyThread();
thread2.start();
}
}

/**
* 多线程创建方式二:实现 Runnable 接口(更常用)
*/
public class MyRunnable implements Runnable {

private String taskName;

public MyRunnable(String taskName) {
this.taskName = taskName;
}

@Override
public void run() {
System.out.println("任务 " + taskName + " 开始执行");
try {
Thread.sleep(1000); // 模拟任务执行
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("任务 " + taskName + " 执行完成");
}
}

// 使用
public class ThreadDemo2 {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable("下载文件"));
Thread thread2 = new Thread(new MyRunnable("处理数据"));

thread1.start();
thread2.start();
}
}

/**
* 多线程创建方式三:Lambda 表达式(最简洁)
*/
public class ThreadDemo3 {
public static void main(String[] args) {
// 使用 Lambda 创建 Runnable
Runnable task1 = () -> {
System.out.println("任务 A 执行中...");
};

Runnable task2 = () -> System.out.println("任务 B 执行中...");

new Thread(task1).start();
new Thread(task2).start();
}
}

/**
* 线程同步:synchronized 关键字
*/
public class SynchronizedDemo {

private int count = 0;

// synchronized 保证线程安全
public synchronized void increment() {
count++;
System.out.println("计数器:" + count);
}

public static void main(String[] args) {
SynchronizedDemo demo = new SynchronizedDemo();

// 创建 10 个线程同时执行 increment
for (int i = 0; i < 10; i++) {
new Thread(() -> demo.increment()).start();
}
}
}

4.3 I/O 流与文件处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 文件读写示例
*/
public class IOdemo {

/**
* 字节流读写文件
*/
public static void byteStreamDemo() throws IOException {
// 写文件
String content = "Hello, Java I/O! 🧡";
FileOutputStream fos = new FileOutputStream("test.txt");
fos.write(content.getBytes());
fos.close();

// 读文件
FileInputStream fis = new FileInputStream("test.txt");
byte[] buffer = new byte[1024];
int len = fis.read(buffer);
String result = new String(buffer, 0, len);
System.out.println("读取内容:" + result);
fis.close();
}

/**
* 字符流读写文件
*/
public static void charStreamDemo() throws IOException {
// 写文件
FileWriter fw = new FileWriter("test.txt");
fw.write("使用字符流写入内容");
fw.close();

// 读文件
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}

/**
* 使用 try-with-resources 自动关闭流(推荐)
*/
public static void tryWithResourcesDemo() throws IOException {
// 自动关闭资源,无需 finally
try (FileWriter fw = new FileWriter("test.txt");
BufferedWriter bw = new BufferedWriter(fw)) {
bw.write("第一行内容");
bw.newLine();
bw.write("第二行内容");
}

try (FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr)) {
br.lines().forEach(System.out::println);
}
}
}

4.4 JVM 核心原理

理解 JVM 是成为高级 Java 开发者的必经之路:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* JVM 内存区域示例:帮助理解内存分配
*/
public class JVMMemoryDemo {

// 方法区:存储类信息、常量、静态变量
private static int staticVar = 100;
private static final String CONSTANT = "常量"; // 常量存储在常量池

public static void main(String[] args) {
// 栈帧:每个方法调用都会创建一个栈帧
methodA();
}

public static void methodA() {
// 局部变量表:存储方法参数和局部变量
int localVar = 10;
String str = "局部变量"; // 引用类型,引用存在栈,实际对象在堆

// 堆:所有对象都在堆上分配
Person person = new Person("张三", 25);
methodB(person);
}

public static void methodB(Person p) {
// 操作数栈:方法执行时的临时数据存储
int result = p.getAge() + 10;
System.out.println(result);
}
}

/**
* 对象在堆中的内存分配
*/
public class Person {
private String name; // 引用类型,4字节(reference)
private int age; // 基本类型,4字节

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public int getAge() {
return age;
}
}

五、第三阶段:数据库与持久层

5.1 MySQL 数据库

详见博客文章:MySQL 数据库从入门到精通

5.2 JDBC 数据库连接

详见博客文章:JDBC 零基础入门到实战

5.3 MyBatis 持久层框架

详见博客文章:MyBatis 从入门到精通

5.4 学习建议


六、第四阶段:Web 开发基础

6.1 HTML/CSS/JavaScript 基础

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!-- 简单的 HTML 页面 -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>我的第一个网页</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<h1>🎉 欢迎学习 Web 开发</h1>
<p>这是一个简单的 HTML 页面示例</p>

<button onclick="sayHello()">点击我</button>
</div>

<script>
function sayHello() {
alert("Hello, JavaScript! 🚀");
}
</script>
</body>
</html>

6.2 HTTP 协议基础

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* HTTP 请求方法示例
*/
public class HTTPMethods {

// GET 请求:获取资源
public void httpGetDemo() {
// URL 参数拼接在地址栏
String url = "https://api.example.com/users?id=1";
// 特点:无请求体,参数有长度限制
}

// POST 请求:提交数据
public void httpPostDemo() {
// 请求参数放在请求体中
// 适合发送大量数据
}

// PUT 请求:更新资源
public void httpPutDemo() {
// 幂等操作,多次执行结果相同
}

// DELETE 请求:删除资源
public void httpDeleteDemo() {
// 用于删除服务器上的资源
}
}

6.3 Servlet 与 JSP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Servlet 示例:处理 HTTP 请求
*/
@WebServlet("/users")
public class UserServlet extends HttpServlet {

private UserService userService = new UserServiceImpl();

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

// 获取请求参数
String id = request.getParameter("id");

// 业务处理
User user = userService.findById(Long.parseLong(id));

// 设置响应内容
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("{\"name\":\"" + user.getName() + "\"}");
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

// 处理 POST 请求(添加用户)
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String email = request.getParameter("email");

User user = new User();
user.setName(name);
user.setEmail(email);

userService.save(user);
response.getWriter().write("{\"success\":true}");
}
}

七、第五阶段:主流框架进阶

7.1 Spring Boot 快速开发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Spring Boot 启动类
*/
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

/**
* RESTful API 控制器
*/
@RestController
@RequestMapping("/api/users")
public class UserController {

@Autowired
private UserService userService;

/**
* GET /api/users - 获取所有用户
*/
@GetMapping
public List<User> getAllUsers() {
return userService.findAll();
}

/**
* GET /api/users/{id} - 获取指定用户
*/
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userService.findById(id);
}

/**
* POST /api/users - 创建用户
*/
@PostMapping
public Result<User> createUser(@RequestBody @Valid User user) {
User saved = userService.save(user);
return Result.success(saved);
}

/**
* PUT /api/users/{id} - 更新用户
*/
@PutMapping("/{id}")
public Result<Void> updateUser(@PathVariable Long id,
@RequestBody User user) {
user.setId(id);
userService.update(user);
return Result.success();
}

/**
* DELETE /api/users/{id} - 删除用户
*/
@DeleteMapping("/{id}")
public Result<Void> deleteUser(@PathVariable Long id) {
userService.deleteById(id);
return Result.success();
}
}

7.2 Spring Cloud 微服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# application.yml - Spring Boot 配置文件
spring:
application:
name: user-service # 服务名称

datasource:
url: jdbc:mysql://localhost:3306/demo?useSSL=false
username: root
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver

redis:
host: localhost
port: 6379
password: your_password

server:
port: 8080 # 服务端口

# MyBatis 配置
mybatis:
mapper-locations: classpath:/mapper/**/*.xml
type-aliases-package: com.example.entity
configuration:
map-underscore-to-camel-case: true

7.3 Redis 缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Redis 使用示例
*/
@Service
public class RedisCacheService {

@Autowired
private StringRedisTemplate redisTemplate;

private static final String USER_CACHE_PREFIX = "user:";
private static final long CACHE_EXPIRE = 3600; // 1小时过期

/**
* 从缓存获取用户
*/
public User getUserFromCache(Long userId) {
String key = USER_CACHE_PREFIX + userId;
String json = redisTemplate.opsForValue().get(key);
if (json != null) {
return JSON.parseObject(json, User.class);
}
return null;
}

/**
* 设置用户到缓存
*/
public void setUserToCache(Long userId, User user) {
String key = USER_CACHE_PREFIX + userId;
String json = JSON.toJSONString(user);
redisTemplate.opsForValue().set(key, json, CACHE_EXPIRE, TimeUnit.SECONDS);
}

/**
* 删除缓存
*/
public void deleteUserCache(Long userId) {
String key = USER_CACHE_PREFIX + userId;
redisTemplate.delete(key);
}

/**
* 实现缓存击穿:加锁 + 缓存
*/
public User getUserWithLock(Long userId) {
String key = USER_CACHE_PREFIX + userId;

// 先查缓存
String json = redisTemplate.opsForValue().get(key);
if (json != null) {
return JSON.parseObject(json, User.class);
}

// 缓存未命中,加锁查数据库
synchronized (this) {
// 双重检查
json = redisTemplate.opsForValue().get(key);
if (json != null) {
return JSON.parseObject(json, User.class);
}

// 从数据库查询
User user = userDao.selectById(userId);

// 写入缓存
redisTemplate.opsForValue().set(key,
JSON.toJSONString(user), CACHE_EXPIRE, TimeUnit.SECONDS);

return user;
}
}
}

八、第六阶段:微服务与分布式

8.1 Docker 容器化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Dockerfile - 构建 Docker 镜像
FROM openjdk:17-jdk-slim

# 设置工作目录
WORKDIR /app

# 复制 jar 包
COPY target/myapp.jar /app/myapp.jar

# 暴露端口
EXPOSE 8080

# 运行命令
ENTRYPOINT ["java", "-jar", "/app/myapp.jar"]
1
2
3
4
5
6
7
# 常用 Docker 命令
docker build -t myapp:1.0 . # 构建镜像
docker run -p 8080:8080 myapp:1.0 # 运行容器
docker ps # 查看运行中的容器
docker stop myapp # 停止容器
docker rm myapp # 删除容器
docker push myapp:1.0 # 推送镜像到仓库

8.2 分布式事务

8.3 消息队列 RabbitMQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* RabbitMQ 生产者示例
*/
@Service
public class MessageProducer {

@Autowired
private RabbitTemplate rabbitTemplate;

/**
* 发送消息
*/
public void sendMessage(String routingKey, Object message) {
rabbitTemplate.convertAndSend(routingKey, message);
}

/**
* 发送用户创建消息
*/
public void sendUserCreatedMessage(Long userId) {
Map<String, Object> message = new HashMap<>();
message.put("type", "USER_CREATED");
message.put("userId", userId);
message.put("timestamp", System.currentTimeMillis());

rabbitTemplate.convertAndSend("user.exchange", "user.created", message);
}
}

/**
* RabbitMQ 消费者示例
*/
@Component
public class MessageConsumer {

/**
* 监听并处理用户创建消息
*/
@RabbitListener(queues = "user.created.queue")
public void handleUserCreatedMessage(Map<String, Object> message) {
Long userId = Long.valueOf(message.get("userId").toString());
System.out.println("收到用户创建消息:" + userId);

// 执行业务逻辑:发送欢迎邮件、初始化用户数据等
welcomeEmailService.send(userId);
}
}

九、第七阶段:DevOps 与工具链

9.1 Git 版本控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Git 基础命令

# 初始化仓库
git init

# 克隆远程仓库
git clone https://github.com/example/repo.git

# 添加文件到暂存区
git add . # 添加所有文件
git add file.txt # 添加指定文件

# 提交到本地仓库
git commit -m "提交说明"

# 推送到远程仓库
git push origin main

# 拉取远程更新
git pull origin main

# 创建并切换分支
git checkout -b feature/new-feature

# 合并分支
git merge feature/new-feature

# 查看状态
git status
git log --oneline --graph

9.2 Maven 项目构建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!-- pom.xml 示例 -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo-project</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>

<properties>
<java.version>17</java.version>
<mybatis-plus.version>3.5.5</mybatis-plus.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

9.3 Linux 基础命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Linux 常用命令

# 文件操作
ls -la # 查看目录详情
cd /path/to/dir # 切换目录
pwd # 显示当前目录
mkdir newdir # 创建目录
rm -rf filename # 删除文件/目录

# 文本编辑
cat file.txt # 查看文件内容
grep "keyword" file.txt # 搜索关键字
vim file.txt # 编辑文件

# 进程与资源
ps aux | grep java # 查看 Java 进程
top # 查看系统资源
kill -9 pid # 强制终止进程

# 网络操作
curl http://example.com # 发送 HTTP 请求
netstat -tlnp | grep 8080 # 查看端口占用
ping example.com # 测试网络连通

# Docker 操作
docker ps # 查看容器
docker logs -f container # 查看日志
docker exec -it container bash # 进入容器

十、学习方法与资源推荐

10.1 正确的学习方法

10.2 推荐学习资源

资源类型 推荐内容 适用阶段
书籍 《Java 核心技术 卷 I》《深入理解 Java 虚拟机》《Effective Java》 全阶段
在线课程 极客时间、拉勾教育、B 站视频 全阶段
官方文档 Spring、Docker、MyBatis 官方文档 进阶
技术社区 GitHub、掘金、思否、博客园 全阶段
刷题平台 LeetCode、牛客网 算法、面试

10.3 阶段性项目推荐

阶段 项目类型 推荐项目
基础阶段 控制台应用 图书管理系统、学生成绩管理
进阶阶段 Web 小应用 个人博客、在线笔记
框架阶段 完整 Web 系统 电商后台管理系统
微服务阶段 分布式系统 秒杀系统、即时通讯

十一、常见问题 FAQ

Q1:Java 和 Python 应该选哪个?

A: 从就业角度,Java 需求量大、企业级应用广泛;从学习角度,Python 上手快、科学计算强。建议:如果目标是后端开发就业,选择 Java;如果是数据分析/AI 领域,选择 Python。

Q2:学完基础需要多久才能找到工作?

A: 一般来说,系统学习 4-6 个月可以具备初级开发能力,6-9 个月可以尝试找中高级岗位。但具体时间因人而异,关键是多做项目、积累经验

Q3:要不要学 Kotlin?

A: Kotlin 是 JVM 语言,与 Java 完全兼容,Android 开发首选。建议:先学好 Java 打牢基础,有余力再学 Kotlin。

Q4:微服务要不要学?

A: 微服务是企业级开发的主流架构,是中高级开发者的必备技能。建议先学好 Spring Boot 基础,再学习 Spring Cloud 微服务。

Q5:如何准备面试?


十二、总结

12.1 学习路线总结

12.2 心态建议


💡 写给读者的话:Java 学习是一场马拉松,不是短跑。每个人的起点和速度都不同,重要的是保持专注和坚持。不要被眼前的困难吓倒,也不要被别人的进度影响节奏。认准方向,一步一个脚印,你一定能到达目的地!加油!💪


📅 本文首次发布于 2026 年 5 月 24 日