fix: [mongo测试文件存储]
This commit is contained in:
parent
7fe4cda97b
commit
e1a782fc44
5
pom.xml
5
pom.xml
|
@ -370,6 +370,11 @@
|
|||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.reflections</groupId>
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.10.2</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.guwan.backend.mongodb;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
public interface CategoryRepository extends MongoRepository<Category, String> {
|
||||
// 可以定义一些自定义查询方法
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -64,4 +64,8 @@ public class FileStorageService {
|
|||
// 根据文件 ID 删除文件
|
||||
gridFsTemplate.delete(new Query(Criteria.where("_id").is(fileId)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.guwan.backend.parse;
|
||||
|
||||
|
||||
public class ParseHandler1 extends Object{
|
||||
public void parseData1() {}
|
||||
public void parseFile2() {}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.guwan.backend.parse;
|
||||
|
||||
|
||||
public class ParseHandler2 extends Object{
|
||||
public void parseString3() {}
|
||||
public void processData3() {}
|
||||
}
|
Loading…
Reference in New Issue