Browse Source

初始化项目

master
hewh 4 months ago
commit
fd43e5266c
  1. 22
      .vscode/tasks.json
  2. 68
      README.md
  3. 9
      clean.bat
  4. 61
      pom.xml
  5. 18
      run.bat
  6. 13
      src/main/java/com/example/App.java
  7. 17
      src/test/java/com/example/AppTest.java
  8. BIN
      target/classes/com/example/App.class
  9. BIN
      target/test-classes/com/example/AppTest.class
  10. 341
      项目说明(命令行版本).md

22
.vscode/tasks.json

@ -0,0 +1,22 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "编译Java项目",
"type": "shell",
"command": "javac",
"args": [
"-cp",
"src/main/java",
"-d",
"target/classes",
"src/main/java/com/example/App.java"
],
"group": "build",
"isBackground": false,
"problemMatcher": [
"$javac"
]
}
]
}

68
README.md

@ -0,0 +1,68 @@
# Java Maven Hello World 项目
这是一个简单的Java Maven项目,用于演示如何创建和运行一个基本的Java应用程序。
## 项目结构
```
PKM/
├── pom.xml # Maven配置文件
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/
│ │ └── example/
│ │ └── App.java # 主应用程序类
│ └── test/
│ └── java/
│ └── com/
│ └── example/
│ └── AppTest.java # 测试类
└── README.md # 项目说明文件
```
## 如何运行
### 前提条件
- 安装Java 17或更高版本
- 安装Maven 3.6或更高版本
### 编译项目
```bash
mvn compile
```
### 运行应用程序
```bash
mvn exec:java
```
### 运行测试
```bash
mvn test
```
### 打包项目
```bash
mvn package
```
### 清理项目
```bash
mvn clean
```
## 输出结果
运行程序后,控制台将显示:
```
Hello World!
欢迎使用Java Maven项目!
```
## 技术栈
- Java 17
- Maven 3.x
- JUnit 5 (用于测试)
## 许可证
此项目仅用于学习和演示目的。

9
clean.bat

@ -0,0 +1,9 @@
@echo off
echo 正在清理编译文件...
if exist target (
rmdir /s /q target
echo 清理完成!
) else (
echo 没有需要清理的文件。
)
pause

61
pom.xml

@ -0,0 +1,61 @@
<?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>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-world</name>
<description>A simple Hello World Java Maven project</description>
<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>
<!-- JUnit 5 for testing -->
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- Maven Exec Plugin for running the main class -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>com.example.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

18
run.bat

@ -0,0 +1,18 @@
@echo off
echo 正在编译Java项目...
javac -cp src\main\java -d target\classes src\main\java\com\example\App.java
if %errorlevel% neq 0 (
echo 编译失败!
pause
exit /b 1
)
echo 编译成功!
echo.
echo 正在运行应用程序...
echo ========================
java -cp target\classes com.example.App
echo ========================
echo.
echo 程序运行完成!
pause

13
src/main/java/com/example/App.java

@ -0,0 +1,13 @@
package com.example;
/**
* Hello World Application
*
* 这是一个简单的Java应用程序用于输出"Hello World"到控制台
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("欢迎使用Java Maven项目!");
}
}

17
src/test/java/com/example/AppTest.java

@ -0,0 +1,17 @@
package com.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* App类的单元测试
*/
public class AppTest {
@Test
public void testApp() {
// 这是一个简单的测试用例
// 在实际项目中,你可以在这里添加更多的测试逻辑
assertTrue(true);
}
}

BIN
target/classes/com/example/App.class

Binary file not shown.

BIN
target/test-classes/com/example/AppTest.class

Binary file not shown.

341
项目说明(命令行版本).md

@ -0,0 +1,341 @@
# 个人知识管理系统 - 命令行版本 (CLI) 设计说明书
## 1. 设计目标
- 实现基础笔记管理功能(增删改查)
- 支持标签系统管理
- 提供本地数据持久化(JSON格式)
- 实现基本搜索功能
- 应用工厂模式实现导出功能
- 为后续Web版本奠定基础架构
## 2. 系统架构
```mermaid
graph TD
A[命令行接口] --> B[命令解析器]
B --> C[控制器]
C --> D[笔记服务]
C --> E[标签服务]
D --> F[存储模块]
E --> F
D --> G[搜索模块]
D --> H[导出模块]
```
## 3. 核心模块设计
### 3.1 笔记实体类 (Note.java)
```java
public class Note {
private String id; // UUID
private String title; // 笔记标题
private String content; // 笔记内容
private List<String> tags; // 标签列表
private LocalDateTime createdAt; // 创建时间
private LocalDateTime updatedAt; // 更新时间
// 构造方法、getter和setter
}
```
### 3.2 存储模块 (StorageService.java)
```java
public interface StorageService {
void saveNote(Note note);
void deleteNote(String id);
Note findNoteById(String id);
List<Note> findAllNotes();
void saveAll(List<Note> notes);
}
```
### 3.3 JSON存储实现 (JsonStorageService.java)
```java
public class JsonStorageService implements StorageService {
private static final String FILE_PATH = "notes.json";
private final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule());
@Override
public void saveNote(Note note) {
List<Note> notes = findAllNotes();
// 更新或添加笔记
notes.removeIf(n -> n.getId().equals(note.getId()));
notes.add(note);
saveAll(notes);
}
@Override
public void saveAll(List<Note> notes) {
try {
objectMapper.writerWithDefaultPrettyPrinter()
.writeValue(new File(FILE_PATH), notes);
} catch (IOException e) {
System.err.println("保存数据失败: " + e.getMessage());
}
}
// 其他方法实现...
}
```
### 3.4 导出模块 (使用工厂模式)
```java
// 导出器接口
public interface Exporter {
void export(Note note, String filePath);
void exportAll(List<Note> notes, String filePath);
}
// 工厂类
public class ExporterFactory {
public static Exporter createExporter(ExportFormat format) {
return switch (format) {
case TXT -> new TxtExporter();
case JSON -> new JsonExporter();
default -> throw new IllegalArgumentException("不支持的导出格式");
};
}
}
// TXT导出实现
public class TxtExporter implements Exporter {
@Override
public void export(Note note, String filePath) {
try (PrintWriter writer = new PrintWriter(filePath)) {
writer.println("标题: " + note.getTitle());
writer.println("创建时间: " + note.getCreatedAt());
writer.println("更新时间: " + note.getUpdatedAt());
writer.println("标签: " + String.join(", ", note.getTags()));
writer.println("\n内容:\n" + note.getContent());
} catch (FileNotFoundException e) {
System.err.println("导出失败: " + e.getMessage());
}
}
// exportAll方法实现...
}
```
### 3.5 搜索模块 (SearchService.java)
```java
public class SearchService {
public List<Note> searchByKeyword(List<Note> notes, String keyword) {
return notes.stream()
.filter(note ->
note.getTitle().contains(keyword) ||
note.getContent().contains(keyword))
.collect(Collectors.toList());
}
public List<Note> searchByTag(List<Note> notes, String tag) {
return notes.stream()
.filter(note -> note.getTags().contains(tag))
.collect(Collectors.toList());
}
}
```
## 4. 命令行接口设计
### 4.1 命令结构
```
pkm [命令] [选项] [参数]
```
### 4.2 支持命令列表
| 命令 | 参数 | 描述 | 示例 |
|--------------|-----------------------|--------------------------|------------------------------------|
| new | [标题] [内容] | 创建新笔记 | `pkm new "Java笔记" "面向对象..."` |
| list | [--tag TAG] | 列出所有笔记 | `pkm list --tag java` |
| view | [笔记ID] | 查看笔记详情 | `pkm view 123e4567` |
| edit | [笔记ID] [新内容] | 编辑笔记内容 | `pkm edit 123e4567 "更新内容"` |
| delete | [笔记ID] | 删除笔记 | `pkm delete 123e4567` |
| tag | [笔记ID] [标签] | 添加标签 | `pkm tag 123e4567 programming` |
| untag | [笔记ID] [标签] | 移除标签 | `pkm untag 123e4567 old` |
| search | [关键词] | 搜索笔记 | `pkm search "设计模式"` |
| export | [笔记ID] [格式] [路径]| 导出笔记 | `pkm export 123e4567 txt output.txt` |
| export-all | [格式] [路径] | 导出所有笔记 | `pkm export-all json backup.json` |
| help | | 显示帮助信息 | `pkm help` |
### 4.3 交互模式
当不带参数运行程序时,进入交互式命令行界面:
```
> 欢迎使用个人知识管理系统 (CLI版)
> 输入 help 查看可用命令
pkm> list
[1] Java笔记 (2023-10-01) [编程, 学习]
[2] 设计模式笔记 (2023-10-05) [编程, 架构]
pkm> view 1
标题: Java笔记
标签: 编程, 学习
创建时间: 2023-10-01T14:30:00
更新时间: 2023-10-02T10:15:00
内容:
面向对象编程的三大特性:封装、继承、多态...
pkm>
```
## 5. 数据存储格式
采用JSON格式存储数据,示例结构:
```json
[
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"title": "Java笔记",
"content": "面向对象编程的三大特性...",
"tags": ["编程", "学习"],
"createdAt": "2023-10-01T14:30:00",
"updatedAt": "2023-10-02T10:15:00"
},
{
"id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"title": "设计模式笔记",
"content": "单例模式确保一个类只有一个实例...",
"tags": ["编程", "架构"],
"createdAt": "2023-10-05T09:45:00",
"updatedAt": "2023-10-05T09:45:00"
}
]
```
## 6. 设计模式应用
### 6.1 工厂模式 - 导出器创建
```java
public class ExporterFactory {
public static Exporter createExporter(ExportFormat format) {
switch (format) {
case TXT:
return new TxtExporter();
case JSON:
return new JsonExporter();
case PDF:
// 未来扩展PDF导出
return new PdfExporter();
default:
throw new IllegalArgumentException("不支持的导出格式: " + format);
}
}
}
// 使用示例
Exporter exporter = ExporterFactory.createExporter(ExportFormat.TXT);
exporter.export(note, "output.txt");
```
### 6.2 策略模式 - 搜索算法(未来扩展)
```java
public interface SearchStrategy {
List<Note> search(List<Note> notes, String query);
}
public class KeywordSearch implements SearchStrategy {
@Override
public List<Note> search(List<Note> notes, String query) {
// 关键词搜索实现
}
}
public class TagSearch implements SearchStrategy {
@Override
public List<Note> search(List<Note> notes, String query) {
// 标签搜索实现
}
}
// 使用示例
SearchStrategy strategy = new KeywordSearch();
List<Note> results = strategy.search(allNotes, "设计模式");
```
## 7. 使用示例
### 7.1 创建新笔记
```
pkm new "设计模式学习" "单例模式:确保一个类只有一个实例..."
```
### 7.2 添加标签
```
pkm tag 550e8400-e29b programming
pkm tag 550e8400-e29b design-patterns
```
### 7.3 搜索笔记
```
pkm search "单例"
```
### 7.4 导出笔记
```
pkm export 550e8400-e29b txt singleton_note.txt
```
## 8. 项目结构
```
pkm-cli/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── cli/
│ │ │ │ ├── App.java // 程序入口
│ │ │ │ ├── CommandParser.java // 命令解析
│ │ │ │ ├── controller/
│ │ │ │ │ ├── NoteController.java
│ │ │ │ │ └── TagController.java
│ │ │ │ ├── service/
│ │ │ │ │ ├── NoteService.java
│ │ │ │ │ ├── TagService.java
│ │ │ │ │ ├── storage/
│ │ │ │ │ │ ├── StorageService.java
│ │ │ │ │ │ └── JsonStorageService.java
│ │ │ │ │ ├── search/
│ │ │ │ │ │ └── SearchService.java
│ │ │ │ │ └── export/
│ │ │ │ │ ├── Exporter.java
│ │ │ │ │ ├── ExporterFactory.java
│ │ │ │ │ ├── TxtExporter.java
│ │ │ │ │ └── JsonExporter.java
│ │ │ │ └── model/
│ │ │ │ ├── Note.java
│ │ │ │ └── ExportFormat.java
│ ├── resources/
├── pom.xml // Maven配置文件
└── notes.json // 数据存储文件
```
## 9. 开发路线图
### 版本1.0 - 基础功能 (第1-2周)
- [x] 笔记增删改查
- [x] 内存存储
- [x] 基本命令行界面
### 版本1.1 - 持久化存储 (第3周)
- [x] JSON文件存储
- [x] 数据加载/保存
- [x] 错误恢复机制
### 版本1.2 - 增强功能 (第4周)
- [x] 标签系统
- [x] 基本搜索功能
- [x] 导出功能(TXT/JSON)
### 版本1.3 - 设计模式应用 (第5-6周)
- [x] 工厂模式实现导出器
- [x] 策略模式实现多种搜索算法
- [x] 命令模式重构命令行处理器
### 版本1.4 - 优化与扩展 (第7-8周)
- [ ] 批处理操作
- [ ] 数据导入功能
- [ ] 更丰富的搜索选项
- [ ] 统计功能(笔记数量、标签云)
此设计为后续Web版本奠定了坚实基础,所有核心业务逻辑(NoteService, TagService等)可在Web版本中直接复用,只需替换存储实现和用户界面层。
Loading…
Cancel
Save