Spring 内容,为测试环境用图像填充数据库的便捷方式
Spring Content, convenient way to populate DB with images for test environment
为了存储图像,我正在使用 Spring Content JPA 策略。
我的测试配置文件具有 HSQLDB 内存中实现。
有没有更方便的方法来用图像填充数据库?
现在,我有一个解决方案来创建一个包含图像的文件夹,然后
在启动时手动上传它们。据我了解,要摆脱图像文件夹,我可以将它们上传到 SQLite,然后从中获取数据,但也许有更好的方法?
CarrentalApplication
@SpringBootApplication
@EnableJpaRepositories
@EnableJpaStores
public class CarrentalApplication {
private static final String IMAGE_FOLDER = "classpath:static/img/test-cars/";
private final Map<Integer, String> cars = new HashMap<>() {{
put(100010, "merc_benz");
put(100011, "volvo_s60");
put(100012, "suz_swift");
put(100013, "volks16v");
put(100014, "ford150");
put(100015, "lambo610");
put(100016, "bmvx5");
put(100017, "audis6");
}};
public static void main(String[] args) {
SpringApplication.run(CarrentalApplication.class, args);
}
@Bean
public CommandLineRunner uploadImages(CarService carService, CarRepository carRepository,
CarImageStore imageStore) {
return (args) -> cars.forEach((carId, name) -> {
try {
Car car = carService.get(carId);
File file = ResourceUtils.getFile(IMAGE_FOLDER + name + ".jpg");
FileInputStream input = new FileInputStream(file);
imageStore.setContent(car, input);
carRepository.save(car);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
CarImageStore
@StoreRestResource(path = "data")
@Repository
public interface CarImageStore extends ContentStore<Car, String> {
}
汽车内容字段:
@ContentId
private String contentId;
@ContentLength
private Long contentLength = 0L;
@MimeType
private String mimeType = "text/plain";
另一种方法是将图像移动到类路径中;即进入/src/test/resources
(假设是maven)并从那里加载它们:
imageStore.setContent(car, this.getClass().getResourceAsStream("/" + name + ".jpg"));
为了存储图像,我正在使用 Spring Content JPA 策略。 我的测试配置文件具有 HSQLDB 内存中实现。 有没有更方便的方法来用图像填充数据库? 现在,我有一个解决方案来创建一个包含图像的文件夹,然后 在启动时手动上传它们。据我了解,要摆脱图像文件夹,我可以将它们上传到 SQLite,然后从中获取数据,但也许有更好的方法?
CarrentalApplication
@SpringBootApplication
@EnableJpaRepositories
@EnableJpaStores
public class CarrentalApplication {
private static final String IMAGE_FOLDER = "classpath:static/img/test-cars/";
private final Map<Integer, String> cars = new HashMap<>() {{
put(100010, "merc_benz");
put(100011, "volvo_s60");
put(100012, "suz_swift");
put(100013, "volks16v");
put(100014, "ford150");
put(100015, "lambo610");
put(100016, "bmvx5");
put(100017, "audis6");
}};
public static void main(String[] args) {
SpringApplication.run(CarrentalApplication.class, args);
}
@Bean
public CommandLineRunner uploadImages(CarService carService, CarRepository carRepository,
CarImageStore imageStore) {
return (args) -> cars.forEach((carId, name) -> {
try {
Car car = carService.get(carId);
File file = ResourceUtils.getFile(IMAGE_FOLDER + name + ".jpg");
FileInputStream input = new FileInputStream(file);
imageStore.setContent(car, input);
carRepository.save(car);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
CarImageStore
@StoreRestResource(path = "data")
@Repository
public interface CarImageStore extends ContentStore<Car, String> {
}
汽车内容字段:
@ContentId
private String contentId;
@ContentLength
private Long contentLength = 0L;
@MimeType
private String mimeType = "text/plain";
另一种方法是将图像移动到类路径中;即进入/src/test/resources
(假设是maven)并从那里加载它们:
imageStore.setContent(car, this.getClass().getResourceAsStream("/" + name + ".jpg"));