本指南将带你系统性地学习 Java 实战开发,从环境搭建到完整项目开发,涵盖 Web 开发、后端服务和工具开发等主要应用场景。
目录
- 开发环境搭建
- Java 基础快速回顾
- 构建工具与依赖管理
- 实战项目一:控制台应用开发
- 实战项目二:Spring Boot Web 应用
- 实战项目三:数据库操作应用
- 实战项目四:RESTful API 开发
- 下一步学习建议
开发环境搭建
1. JDK 安装与配置
- 下载 JDK 17 (LTS版本) 从 Oracle官网 或 Eclipse Temurin
- 设置
JAVA_HOME环境变量 - 验证安装:
java -version和javac -version
2. IDE 选择与配置
- IntelliJ IDEA (推荐):功能强大的商业IDE,有社区免费版
- Eclipse:经典的开源Java IDE
- VS Code + Java扩展包:轻量级选择
3. 构建工具配置
# Maven 安装验证
mvn --version
# Gradle 安装验证
gradle --version
Java 基础快速回顾
基础语法示例
// 基本数据类型和变量
public class Basics {
public static void main(String[] args) {
// 基本数据类型
int age = 25;
double salary = 50000.50;
char grade = 'A';
boolean isJavaFun = true;
String name = "Alice";
// 数组
int[] numbers = {1, 2, 3, 4, 5};
String[] fruits = {"Apple", "Banana", "Orange"};
// 控制流
if (age >= 18) {
System.out.println("成年人");
} else {
System.out.println("未成年人");
}
// 循环
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits[i]);
}
// for-each 循环
for (String fruit : fruits) {
System.out.println(fruit);
}
// while 循环
int count = 0;
while (count < 5) {
System.out.println("Count: " + count);
count++;
}
}
}
面向对象编程示例
// 类和对象
public class Person {
// 字段
private String name;
private int age;
// 构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 方法
public void introduce() {
System.out.println("你好,我叫 " + name + ",今年 " + age + " 岁。");
}
// 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) {
this.age = age;
}
}
// 使用类
public class Main {
public static void main(String[] args) {
Person person1 = new Person("张三", 25);
person1.introduce();
Person person2 = new Person("李四", 30);
person2.introduce();
}
}
集合框架示例
import java.util.*;
public class CollectionExample {
public static void main(String[] args) {
// List示例
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
System.out.println("List: " + list);
// Set示例(自动去重)
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(1); // 重复元素不会被添加
System.out.println("Set: " + set);
// Map示例(键值对)
Map<String, Integer> map = new HashMap<>();
map.put("Alice", 25);
map.put("Bob", 30);
map.put("Charlie", 35);
System.out.println("Map: " + map);
System.out.println("Alice的年龄: " + map.get("Alice"));
// 遍历Map
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
构建工具与依赖管理
Maven 项目结构
project/
├── src/
│ ├── main/
│ │ ├── java/ # Java源代码
│ │ └── resources/ # 资源文件
│ └── test/
│ ├── java/ # 测试代码
│ └── resources/ # 测试资源
├── target/ # 编译输出
└── pom.xml # Maven配置文件
基本的 pom.xml 示例
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-java-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 常用测试框架 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
实战项目一:控制台应用开发
学生管理系统
import java.util.*;
class Student {
private String id;
private String name;
private int age;
private String grade;
public Student(String id, String name, int age, String grade) {
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
}
// Getter 方法
public String getId() { return id; }
public String getName() { return name; }
public int getAge() { return age; }
public String getGrade() { return grade; }
@Override
public String toString() {
return "学号: " + id + ", 姓名: " + name + ", 年龄: " + age + ", 年级: " + grade;
}
}
public class StudentManagementSystem {
private static List<Student> students = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\n=== 学生管理系统 ===");
System.out.println("1. 添加学生");
System.out.println("2. 查看所有学生");
System.out.println("3. 查找学生");
System.out.println("4. 退出");
System.out.print("请选择操作: ");
int choice = scanner.nextInt();
scanner.nextLine(); // 消耗换行符
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayAllStudents();
break;
case 3:
findStudent();
break;
case 4:
System.out.println("感谢使用学生管理系统!");
return;
default:
System.out.println("无效选择,请重新输入!");
}
}
}
private static void addStudent() {
System.out.print("请输入学号: ");
String id = scanner.nextLine();
System.out.print("请输入姓名: ");
String name = scanner.nextLine();
System.out.print("请输入年龄: ");
int age = scanner.nextInt();
scanner.nextLine(); // 消耗换行符
System.out.print("请输入年级: ");
String grade = scanner.nextLine();
Student student = new Student(id, name, age, grade);
students.add(student);
System.out.println("学生添加成功!");
}
private static void displayAllStudents() {
if (students.isEmpty()) {
System.out.println("没有学生记录!");
return;
}
System.out.println("\n=== 所有学生 ===");
for (Student student : students) {
System.out.println(student);
}
}
private static void findStudent() {
System.out.print("请输入要查找的学生姓名: ");
String name = scanner.nextLine();
boolean found = false;
for (Student student : students) {
if (student.getName().equalsIgnoreCase(name)) {
System.out.println("找到学生: " + student);
found = true;
}
}
if (!found) {
System.out.println("未找到姓名为 " + name + " 的学生");
}
}
}
实战项目二:Spring Boot Web 应用
1. 创建 Spring Boot 项目
使用 Spring Initializr 或 IDE 创建新项目,选择依赖:
- Spring Web
- Thymeleaf (模板引擎)
- Spring Boot DevTools
2. 基本控制器和页面
// HomeController.java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "欢迎使用Spring Boot!");
return "home";
}
@GetMapping("/greet")
public String greet(@RequestParam(name = "name", required = false, defaultValue = "World") String name, Model model) {
model.addAttribute("name", name);
return "greet";
}
}
3. Thymeleaf 模板
<!-- src/main/resources/templates/home.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Boot 首页</title>
<link rel="stylesheet" th:href="@{/styles.css}">
</head>
<body>
<div class="container">
<h1 th:text="${message}">默认消息</h1>
<a th:href="@{/greet(name='张三')}">向张三问好</a>
</div>
</body>
</html>
<!-- src/main/resources/templates/greet.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>问候页面</title>
</head>
<body>
<div class="container">
<h1>Hello, <span th:text="${name}">World</span>!</h1>
<a th:href="@{/}">返回首页</a>
</div>
</body>
</html>
4. 运行应用
# 使用Maven运行
mvn spring-boot:run
# 或打包后运行
mvn clean package
java -jar target/demo-0.0.1-SNAPSHOT.jar
访问 http://localhost:8080 查看应用
实战项目三:数据库操作应用
1. 添加数据库依赖
在 pom.xml 中添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
2. 创建实体类和Repository
// User.java
package com.example.demo.entity;
import javax.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String email;
// 构造方法
public User() {}
public User(String username, String email) {
this.username = username;
this.email = email;
}
// Getter和Setter
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
// UserRepository.java
package com.example.demo.repository;
import com.example.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByUsernameContaining(String username);
User findByEmail(String email);
}
3. 创建服务层和控制器
// UserService.java
package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers() {
return userRepository.findAll();
}
public User saveUser(User user) {
return userRepository.save(user);
}
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}
// UserController.java
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public String listUsers(Model model) {
model.addAttribute("users", userService.getAllUsers());
return "users/list";
}
@GetMapping("/new")
public String showUserForm(Model model) {
model.addAttribute("user", new User());
return "users/form";
}
@PostMapping
public String saveUser(@ModelAttribute User user) {
userService.saveUser(user);
return "redirect:/users";
}
}
实战项目四:RESTful API 开发
1. 创建REST控制器
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/users")
public class UserApiController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
User user = userService.getUserById(id);
if (user != null) {
return ResponseEntity.ok(user);
} else {
return ResponseEntity.notFound().build();
}
}
@PostMapping
public User createUser(@RequestBody User user) {
return userService.saveUser(user);
}
@PutMapping("/{id}")
public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User userDetails) {
User user = userService.getUserById(id);
if (user != null) {
user.setUsername(userDetails.getUsername());
user.setEmail(userDetails.getEmail());
User updatedUser = userService.saveUser(user);
return ResponseEntity.ok(updatedUser);
} else {
return ResponseEntity.notFound().build();
}
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteUser(@PathVariable Long id) {
User user = userService.getUserById(id);
if (user != null) {
userService.deleteUser(id);
return ResponseEntity.ok().build();
} else {
return ResponseEntity.notFound().build();
}
}
}
2. 测试API
可以使用Postman或curl测试API:
# 获取所有用户
curl http://localhost:8080/api/users
# 创建新用户
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{"username":"john_doe","email":"john@example.com"}'
# 获取特定用户
curl http://localhost:8080/api/users/1
# 更新用户
curl -X PUT http://localhost:8080/api/users/1 \
-H "Content-Type: application/json" \
-d '{"username":"john_updated","email":"john_updated@example.com"}'
# 删除用户
curl -X DELETE http://localhost:8080/api/users/1
下一步学习建议
-
深入学习 Spring 生态系统:
- Spring Security:安全认证和授权
- Spring Data:各种数据库集成
- Spring Cloud:微服务架构
-
掌握数据库技术:
- MySQL/PostgreSQL 等关系型数据库
- MongoDB/Redis 等NoSQL数据库
- 数据库优化和索引
-
学习测试开发:
- JUnit 5 单元测试
- Mockito 模拟测试
- 集成测试和端到端测试
-
了解DevOps相关技术:
- Docker 容器化
- Jenkins/GitLab CI 持续集成
- Kubernetes 容器编排
-
参与开源项目:
- 在GitHub上寻找Java项目贡献
- 学习代码审查和协作开发
-
构建完整的项目:
- 从需求分析到部署上线的完整流程
- 学习系统设计和架构模式
- 性能优化和监控 blog.ziipppmm.com.cn/article/20250907_117115.html www.ziipppmm.com.cn/article/202… tv.ziipppmm.com.cn/article/20250907_448798.html read.ziipppmm.com.cn/article/20250907_249558.html news.ziipppmm.com.cn/article/20250907_185122.html mip.ziipppmm.com.cn/article/20250907_230777.html vog.ziipppmm.com.cn/article/20250907_844515.html vog.ziipppmm.com.cn/article/20250907_029478.html m.ziipppmm.com.cn/article/20250907_286437.html www.ziipppmm.com.cn/article/202… read.ziipppmm.com.cn/article/20250907_075292.html m.ziipppmm.com.cn/article/20250907_462912.html blog.ziipppmm.com.cn/article/20250907_358717.html mip.ziipppmm.com.cn/article/20250907_736932.html news.ziipppmm.com.cn/article/20250907_847275.html news.ziipppmm.com.cn/article/20250907_080361.html www.ziipppmm.com.cn/article/202… m.ziipppmm.com.cn/article/20250907_977361.html blog.ziipppmm.com.cn/article/20250907_392197.html tv.ziipppmm.com.cn/article/20250907_209353.html blog.ziipppmm.com.cn/article/20250907_936355.html tv.ziipppmm.com.cn/article/20250907_886409.html read.ziipppmm.com.cn/article/20250907_188233.html tv.ziipppmm.com.cn/article/20250907_304343.html news.ziipppmm.com.cn/article/20250907_652133.html vog.ziipppmm.com.cn/article/20250907_363336.html m.ziipppmm.com.cn/article/20250907_270787.html blog.ziipppmm.com.cn/article/20250907_973488.html tv.ziipppmm.com.cn/article/20250907_337557.html news.ziipppmm.com.cn/article/20250907_359534.html tv.ziipppmm.com.cn/article/20250907_785488.html read.ziipppmm.com.cn/article/20250907_601754.html blog.ziipppmm.com.cn/article/20250907_191634.html tv.ziipppmm.com.cn/article/20250907_135181.html m.ziipppmm.com.cn/article/20250907_967684.html mip.ziipppmm.com.cn/article/20250907_202887.html www.ziipppmm.com.cn/article/202… vog.ziipppmm.com.cn/article/20250907_593667.html news.ziipppmm.com.cn/article/20250907_509905.html vog.ziipppmm.com.cn/article/20250907_854468.html www.ziipppmm.com.cn/article/202… m.ziipppmm.com.cn/article/20250907_198010.html m.ziipppmm.com.cn/article/20250907_137766.html news.ziipppmm.com.cn/article/20250907_867564.html read.ziipppmm.com.cn/article/20250907_961374.html tv.ziipppmm.com.cn/article/20250907_951673.html mip.ziipppmm.com.cn/article/20250907_725216.html news.ziipppmm.com.cn/article/20250907_504107.html mip.ziipppmm.com.cn/article/20250907_078614.html read.ziipppmm.com.cn/article/20250907_418567.html mip.ziipppmm.com.cn/article/20250907_958054.html mip.ziipppmm.com.cn/article/20250907_177047.html vog.ziipppmm.com.cn/article/20250907_916858.html www.ziipppmm.com.cn/article/202… vog.ziipppmm.com.cn/article/20250907_118799.html vog.ziipppmm.com.cn/article/20250907_857308.html m.ziipppmm.com.cn/article/20250907_182459.html mip.ziipppmm.com.cn/article/20250907_729768.html blog.ziipppmm.com.cn/article/20250907_761611.html read.ziipppmm.com.cn/article/20250907_482605.html blog.ziipppmm.com.cn/article/20250907_712657.html news.ziipppmm.com.cn/article/20250907_359427.html read.ziipppmm.com.cn/article/20250907_630175.html tv.ziipppmm.com.cn/article/20250907_860108.html tv.ziipppmm.com.cn/article/20250907_284567.html mip.ziipppmm.com.cn/article/20250907_863550.html news.ziipppmm.com.cn/article/20250907_819232.html m.ziipppmm.com.cn/article/20250907_843313.html www.ziipppmm.com.cn/article/202… vog.ziipppmm.com.cn/article/20250907_574477.html read.ziipppmm.com.cn/article/20250907_768022.html blog.ziipppmm.com.cn/article/20250907_506819.html mip.ziipppmm.com.cn/article/20250907_891798.html blog.ziipppmm.com.cn/article/20250907_292401.html vog.ziipppmm.com.cn/article/20250907_921887.html www.ziipppmm.com.cn/article/202… news.ziipppmm.com.cn/article/20250907_078055.html mip.ziipppmm.com.cn/article/20250907_547578.html read.ziipppmm.com.cn/article/20250907_290423.html news.ziipppmm.com.cn/article/20250907_946285.html www.ziipppmm.com.cn/article/202… www.ziipppmm.com.cn/article/202… news.ziipppmm.com.cn/article/20250907_136737.html m.ziipppmm.com.cn/article/20250907_946395.html tv.ziipppmm.com.cn/article/20250907_060588.html mip.ziipppmm.com.cn/article/20250907_319658.html tv.ziipppmm.com.cn/article/20250907_154160.html m.ziipppmm.com.cn/article/20250907_041867.html vog.ziipppmm.com.cn/article/20250907_523221.html news.ziipppmm.com.cn/article/20250907_453167.html vog.ziipppmm.com.cn/article/20250907_666545.html blog.ziipppmm.com.cn/article/20250907_957927.html vog.ziipppmm.com.cn/article/20250907_378764.html tv.ziipppmm.com.cn/article/20250907_752109.html read.ziipppmm.com.cn/article/20250907_916812.html vog.ziipppmm.com.cn/article/20250907_092022.html mip.ziipppmm.com.cn/article/20250907_032249.html mip.ziipppmm.com.cn/article/20250907_604981.html blog.ziipppmm.com.cn/article/20250907_913006.html www.ziipppmm.com.cn/article/202… Java 是一个强大的生态系统,通过不断实践和构建真实项目,你将能够掌握企业级应用开发的各项技能。