From e1a782fc445e197f369d491cf7e7657b8858fb50 Mon Sep 17 00:00:00 2001 From: ovo <ovo@ovo.com> Date: Thu, 27 Feb 2025 22:00:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20[mongo=E6=B5=8B=E8=AF=95=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=AD=98=E5=82=A8]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ++ .../backend/controller/DemoController.java | 14 ++++ .../com/guwan/backend/mongodb/Category.java | 18 +++++ .../backend/mongodb/CategoryRepository.java | 7 ++ .../backend/mongodb/CategoryService.java | 37 +++++++++ .../backend/mongodb/FileStorageService.java | 4 + .../java/com/guwan/backend/parse/Demo.java | 25 ++++++ .../com/guwan/backend/parse/MethodLister.java | 78 +++++++++++++++++++ .../guwan/backend/parse/MethodLister2.java | 58 ++++++++++++++ .../guwan/backend/parse/ParseHandler1.java | 7 ++ .../guwan/backend/parse/ParseHandler2.java | 7 ++ 11 files changed, 260 insertions(+) create mode 100644 src/main/java/com/guwan/backend/mongodb/Category.java create mode 100644 src/main/java/com/guwan/backend/mongodb/CategoryRepository.java create mode 100644 src/main/java/com/guwan/backend/mongodb/CategoryService.java create mode 100644 src/main/java/com/guwan/backend/parse/Demo.java create mode 100644 src/main/java/com/guwan/backend/parse/MethodLister.java create mode 100644 src/main/java/com/guwan/backend/parse/MethodLister2.java create mode 100644 src/main/java/com/guwan/backend/parse/ParseHandler1.java create mode 100644 src/main/java/com/guwan/backend/parse/ParseHandler2.java diff --git a/pom.xml b/pom.xml index 88e47df..4a5895f 100644 --- a/pom.xml +++ b/pom.xml @@ -370,6 +370,11 @@ </dependency> + <dependency> + <groupId>org.reflections</groupId> + <artifactId>reflections</artifactId> + <version>0.10.2</version> + </dependency> diff --git a/src/main/java/com/guwan/backend/controller/DemoController.java b/src/main/java/com/guwan/backend/controller/DemoController.java index ed1ae5c..24a82fb 100644 --- a/src/main/java/com/guwan/backend/controller/DemoController.java +++ b/src/main/java/com/guwan/backend/controller/DemoController.java @@ -4,6 +4,7 @@ import cn.easyes.core.conditions.LambdaEsQueryWrapper; import com.guwan.backend.common.Result; import com.guwan.backend.elasticsearch.document.ProductDocument; import com.guwan.backend.elasticsearch.mapper.ProductEsMapper; +import com.guwan.backend.mongodb.CategoryService; import com.guwan.backend.mongodb.FileStorageService; import com.guwan.backend.util.MinioUtil; import com.mongodb.client.gridfs.model.GridFSFile; @@ -40,6 +41,7 @@ public class DemoController { private final FileStorageService fileStorageService; + private final CategoryService categoryService; @PostMapping("/uploadFile") public Result<String> uploadFile(String bucketName, MultipartFile file){ return Result.success(minioUtil.getUrl(minioUtil.getFileUrl @@ -113,4 +115,16 @@ public class DemoController { } + @GetMapping("/saveCategories") + public void saveCategories(String fileId) throws IOException { + categoryService.saveCategories(); + } + + + @GetMapping("/getAllCategories") + public void getAllCategories() { + System.out.println("categoryService.getAllCategories() = " + categoryService.getAllCategories()); + } + + } diff --git a/src/main/java/com/guwan/backend/mongodb/Category.java b/src/main/java/com/guwan/backend/mongodb/Category.java new file mode 100644 index 0000000..5809f7f --- /dev/null +++ b/src/main/java/com/guwan/backend/mongodb/Category.java @@ -0,0 +1,18 @@ +package com.guwan.backend.mongodb; + +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.List; + +@Document(collection = "categories") +@Data +public class Category { + + @Id + private String id; + private String name; + private List<Category> children; + +} diff --git a/src/main/java/com/guwan/backend/mongodb/CategoryRepository.java b/src/main/java/com/guwan/backend/mongodb/CategoryRepository.java new file mode 100644 index 0000000..57eb25d --- /dev/null +++ b/src/main/java/com/guwan/backend/mongodb/CategoryRepository.java @@ -0,0 +1,7 @@ +package com.guwan.backend.mongodb; + +import org.springframework.data.mongodb.repository.MongoRepository; + +public interface CategoryRepository extends MongoRepository<Category, String> { + // 可以定义一些自定义查询方法 +} diff --git a/src/main/java/com/guwan/backend/mongodb/CategoryService.java b/src/main/java/com/guwan/backend/mongodb/CategoryService.java new file mode 100644 index 0000000..1143a09 --- /dev/null +++ b/src/main/java/com/guwan/backend/mongodb/CategoryService.java @@ -0,0 +1,37 @@ +package com.guwan.backend.mongodb; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class CategoryService { + + @Autowired + private MongoTemplate mongoTemplate; + + public void saveCategories() { + // 创建子类 B 和 C + Category subCategory1 = new Category(); + subCategory1.setName("B"); + Category subCategory2 = new Category(); + subCategory2.setName("C"); + + // 创建根类 A,包含子类 B 和 C + Category rootCategory = new Category(); + rootCategory.setName("A"); + rootCategory.setChildren(List.of(subCategory1, subCategory2)); + + // 保存根类 A 到 MongoDB + mongoTemplate.save(rootCategory); + + System.out.println("Inserted Category: " + rootCategory); + } + + public List<Category> getAllCategories() { + return mongoTemplate.findAll(Category.class); + } + +} diff --git a/src/main/java/com/guwan/backend/mongodb/FileStorageService.java b/src/main/java/com/guwan/backend/mongodb/FileStorageService.java index 05b419b..7ba978a 100644 --- a/src/main/java/com/guwan/backend/mongodb/FileStorageService.java +++ b/src/main/java/com/guwan/backend/mongodb/FileStorageService.java @@ -64,4 +64,8 @@ public class FileStorageService { // 根据文件 ID 删除文件 gridFsTemplate.delete(new Query(Criteria.where("_id").is(fileId))); } + + + + } \ No newline at end of file diff --git a/src/main/java/com/guwan/backend/parse/Demo.java b/src/main/java/com/guwan/backend/parse/Demo.java new file mode 100644 index 0000000..7df9603 --- /dev/null +++ b/src/main/java/com/guwan/backend/parse/Demo.java @@ -0,0 +1,25 @@ +package com.guwan.backend.parse; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +public class Demo { + public static void main(String[] args) { + List<Integer> list1 = Arrays.asList(1, 2, 3, 4); + List<Integer> list2 = Arrays.asList(1, 2, 3, 4); + + + for (Integer i : list1) { + for (Integer integer : list2) { + if (Objects.equals(i, integer)){ + System.out.println("integer = " + integer); + } + } + } + + list1.stream() + .flatMap(i -> list2.stream().filter(integer -> Objects.equals(i, integer))) + .forEach(integer -> System.out.println("integer = " + integer)); + } +} diff --git a/src/main/java/com/guwan/backend/parse/MethodLister.java b/src/main/java/com/guwan/backend/parse/MethodLister.java new file mode 100644 index 0000000..3904f69 --- /dev/null +++ b/src/main/java/com/guwan/backend/parse/MethodLister.java @@ -0,0 +1,78 @@ +package com.guwan.backend.parse; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; + +public class MethodLister { + + public static void listMethodsByClassPrefix(String packageName, String classPrefix) throws Exception { + // 获取包路径 + String packagePath = packageName.replace('.', '/'); + + // 获取类加载器 + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + // 获取包路径下所有文件 + URL url = classLoader.getResource(packagePath); + if (url == null) { + System.out.println("Package not found: " + packageName); + return; + } + + File directory = new File(url.toURI()); + + System.out.println("directory = " + directory); + if (!directory.exists()) { + System.out.println("Directory not found: " + directory.getAbsolutePath()); + return; + } + + // 获取包下所有类 + List<Class<?>> classes = getClassesFromDirectory(directory, packageName, classPrefix); + + // 输出每个符合条件的类及其方法 + for (Class<?> clazz : classes) { + System.out.println("Class: " + clazz.getName()); + Method[] methods = clazz.getDeclaredMethods(); + for (Method method : methods) { + System.out.println(" Method: " + method.getName()); + } + } + } + + private static List<Class<?>> getClassesFromDirectory(File directory, String packageName, String classPrefix) throws ClassNotFoundException { + List<Class<?>> classes = new ArrayList<>(); + + // 获取目录下的所有文件 + File[] files = directory.listFiles(file -> file.getName().endsWith(".class")); + if (files == null) return classes; + + // 遍历文件 + for (File file : files) { + // 获取类名(去掉 .class 后缀) + String className = file.getName().substring(0, file.getName().length() - 6); + + // 判断类名是否以指定的前缀开始 + if (className.startsWith(classPrefix)) { + // 动态加载类 + Class<?> clazz = Class.forName(packageName + "." + className); + classes.add(clazz); + } + } + + return classes; + } + + public static void main(String[] args) { + try { + // 指定包名和类名前缀 + listMethodsByClassPrefix("com.guwan.backend.parse", "Parse"); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/guwan/backend/parse/MethodLister2.java b/src/main/java/com/guwan/backend/parse/MethodLister2.java new file mode 100644 index 0000000..c5c766d --- /dev/null +++ b/src/main/java/com/guwan/backend/parse/MethodLister2.java @@ -0,0 +1,58 @@ +package com.guwan.backend.parse; + +import org.reflections.Reflections; +import org.reflections.scanners.Scanners; +import org.reflections.util.ConfigurationBuilder; + +import java.lang.reflect.Method; +import java.util.Set; + +public class MethodLister2 { + + public static void listMethodsByClassPrefix(String packageName, String classPrefix) { + try { + // 使用 Reflections 库扫描指定包下的类 + // Reflections reflections = new Reflections(packageName); + + /** + * 要重写过滤条件 默认情况下,Reflections 会排除 Object 类的扫描结果,因为它是所有类的根基类 + */ + + Reflections reflections = new Reflections( + new ConfigurationBuilder() + .addScanners(Scanners.SubTypes.filterResultsBy(s -> true)/*Override the default behavior which exclude Object class*/) + .forPackages(packageName) + ); + + // 获取某类的所有子类 + Set<Class<?>> classes = reflections.getSubTypesOf(Object.class); + + //获取使用某注解的所有类 + // Set<Class<?>> classes = reflections.getTypesAnnotatedWith(SCAN.class); + + + + // System.out.println("classes = " + classes); + + // 过滤以 classPrefix 开头的类 + for (Class<?> clazz : classes) { + if (clazz.getSimpleName().startsWith(classPrefix)) { + System.out.println("Class: " + clazz.getName()); + + // 获取类的所有方法 + Method[] methods = clazz.getDeclaredMethods(); + for (Method method : methods) { + System.out.println(" Method: " + method.getName()); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + // 指定包名和类名前缀 + listMethodsByClassPrefix("com.guwan.backend.parse", "Parse"); + } +} diff --git a/src/main/java/com/guwan/backend/parse/ParseHandler1.java b/src/main/java/com/guwan/backend/parse/ParseHandler1.java new file mode 100644 index 0000000..7782178 --- /dev/null +++ b/src/main/java/com/guwan/backend/parse/ParseHandler1.java @@ -0,0 +1,7 @@ +package com.guwan.backend.parse; + + +public class ParseHandler1 extends Object{ + public void parseData1() {} + public void parseFile2() {} +} \ No newline at end of file diff --git a/src/main/java/com/guwan/backend/parse/ParseHandler2.java b/src/main/java/com/guwan/backend/parse/ParseHandler2.java new file mode 100644 index 0000000..c52ac0d --- /dev/null +++ b/src/main/java/com/guwan/backend/parse/ParseHandler2.java @@ -0,0 +1,7 @@ +package com.guwan.backend.parse; + + +public class ParseHandler2 extends Object{ + public void parseString3() {} + public void processData3() {} +} \ No newline at end of file