Java后端学习笔记
作者:赵宇
日期:2026年4月7日
前言
最近重新学习Java基础知识和Spring Boot框架,整理了这份学习笔记。其中特别需要注意的是,Spring Boot 3要求至少Java 17版本,这是一个重要的升级点。
一、Java 基础
1.1 基本数据类型
| 类型 | 大小 | 默认值 | 范围 |
|---|---|---|---|
| byte | 1 字节 | 0 | -128 ~ 127 |
| short | 2 字节 | 0 | -32768 ~ 32767 |
| int | 4 字节 | 0 | -2^31 ~ 2^31-1 |
| long | 8 字节 | 0L | -2^63 ~ 2^63-1 |
| float | 4 字节 | 0.0f | 单精度浮点 |
| double | 8 字节 | 0.0d | 双精度浮点 |
| char | 2 字节 | ‘\u0000’ | 0 ~ 65535 |
| boolean | 1 位 | false | true / false |
1.2 面向对象三大特性
封装:将数据和方法包装在一起,通过访问修饰符控制外部访问。
public class User {
private String name; // 私有字段,外部不可直接访问
public String getName() { // 通过 getter 访问
return name;
}
public void setName(String name) { // 通过 setter 修改
this.name = name;
}
}
继承:子类继承父类的属性和方法,实现代码复用。
public class Animal {
public void eat() {
System.out.println("吃东西");
}
}
public class Dog extends Animal { // Dog 继承 Animal
public void bark() {
System.out.println("汪汪");
}
}
多态:同一方法在不同对象中有不同表现。
Animal a = new Dog(); // 父类引用指向子类对象
a.eat(); // 调用子类重写的方法
1.3 接口与抽象类
| 特性 | 接口 (interface) | 抽象类 (abstract class) |
|---|---|---|
| 实例化 | 不能 | 不能 |
| 构造方法 | 无 | 有 |
| 成员变量 | 只能是常量 (public static final) | 可以有普通成员变量 |
| 方法 | Java 8+ 支持 default/static 方法 | 可以有抽象方法和普通方法 |
| 多继承 | 一个类可实现多个接口 | 一个类只能继承一个抽象类 |
1.4 列表、集合、队列
Collection
├── List(有序可重复)
│ ├── ArrayList — 动态数组,随机访问快
│ └── LinkedList — 双向链表,插入删除快
├── Set(无序不重复)
│ ├── HashSet — 基于哈希表
│ └── TreeSet — 基于红黑树,自动排序
└── Queue(队列)
├── LinkedList — 普通队列
└── PriorityQueue — 优先队列
Map(键值对)
├── HashMap — 最常用,线程不安全,允许 null
├── TreeMap — 按 key 排序
└── ConcurrentHashMap — 线程安全,推荐并发场景使用
1.5 异常处理
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("算术异常: " + e.getMessage());
} catch (Exception e) {
System.out.println("其他异常: " + e.getMessage());
} finally {
System.out.println("始终执行"); // 关闭资源等
}
1.6 泛型
// 泛型类
public class Box<T> {
private T value;
public T getValue() { return value; }
public void setValue(T value) { this.value = value; }
}
// 通配符
List<?> list; // 未知类型
List<? extends Number> // Number 或其子类(上界)
List<? super Integer> // Integer 或其父类(下界)
1.7 Lambda 表达式与函数式接口
// 函数式接口:只有一个抽象方法的接口
@FunctionalInterface
public interface MyFunc {
int operate(int a, int b);
}
// Lambda 写法
MyFunc add = (a, b) -> a + b;
MyFunc mul = (a, b) -> a * b;
// 常用内置函数式接口
Consumer<String> print = s -> System.out.println(s); // 消费型
Supplier<Double> random = () -> Math.random(); // 供给型
Function<String, Integer> len = s -> s.length(); // 函数型
Predicate<Integer> isEven = n -> n % 2 == 0; // 断言型
二、Spring Boot 基础
2.2 创建项目
使用 Spring Initializr (https://start.spring.io) 或 IDE 插件创建。
最小依赖的 pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
启动类:
@SpringBootApplication // 核心注解,包含 @Configuration + @ComponentScan + @EnableAutoConfiguration
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
2.3 常用 Starter 依赖
| Starter | 用途 |
|---|---|
| spring-boot-starter-web | Web 应用 + 内嵌 Tomcat |
| spring-boot-starter-data-jpa | JPA 数据库访问 |
| spring-boot-starter-data-redis | Redis 操作 |
| spring-boot-starter-security | 安全认证与授权 |
| spring-boot-starter-validation | 参数校验 |
| spring-boot-starter-test | 单元测试 |
| spring-boot-starter-actuator | 应用监控 |
2.4 配置文件
application.yml(推荐):
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
my:
app-name: "学习项目"
max-size: 100
读取配置的方式:
// 方式一:@Value
@Value("${my.app-name}")
private String appName;
// 方式二:@ConfigurationProperties(推荐,类型安全)
@Component
@ConfigurationProperties(prefix = "my")
public class MyConfig {
private String appName;
private int maxSize;
// getter / setter
}