优化java(控制器和服务)代码以更快地工作

时间:2017-01-01 16:35:02

标签: java spring hibernate spring-mvc

我使用spring mvc创建在线商店。所以关键是当你创建新产品时,你需要为新产品上传图片。我正在使用Cloudinary服务,因此当您上传图像服务器抓取它时,将其发送到Cloudinary获取响应,从响应中获取图像URL并将该URL写入基础。所有的好处就像5秒一样,无论如何都要优化这段代码,这样它的工作速度会更快,因为这个项目是一个获得工作的测试项目,所以我希望它工作得很好)这里是我写的代码 控制器,它获取带有填充信息和图像的产品,如MultipartFile:

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String createProduct(MultipartFile image, Model model, @ModelAttribute(name = "product") @Valid ProductDto product, BindingResult bindingResult) throws IOException {
   productValidator.validate(product,bindingResult);
    if (bindingResult.hasErrors()){
        model.addAttribute("categories", categoryService.findAll());
        return "admin/create";
    } else {
        productService.saveProductDto(product,image);
        return "redirect:/";
    }
}

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createProduct(Model model) {
    model.addAttribute("product", new ProductDto());
    model.addAttribute("categories", categoryService.findAll());
    return "admin/create";
}

之后转到产品服务:

    @Override
public Product saveProductDto(ProductDto productDto, MultipartFile image) throws IOException {
    List<Image> list = transformImageToList(image);
    Product product = new Product();
    product.setCategory(categoryService.findById(productDto.getCategoryId()));
    product.setDescription(productDto.getDescription());
    product.setTitle(productDto.getTitle());
    product.setPrice(productDto.getPrice());
    product.setImages(list);
productDao.create(product);
    return product;
}

@Override
public Product saveProduct(Product product, Long categoryId, MultipartFile file) throws IOException {
    Product deprecatedProduct = productDao.findById(product.getId());
    if (file.getSize() != 0) {
        List<Image> list = transformImageToList(file);
        product.setImages(list);
    } else {
        product.setImages(deprecatedProduct.getImages());
    }
    product.setComments(deprecatedProduct.getComments());
    product.setCategory(categoryService.findById(categoryId));
    productDao.update(product);
    return product;
}

@Override
public List<Image> transformImageToList(MultipartFile image) throws IOException {
    Image dbImage = new Image();
    List<Image> list = new ArrayList<>();
    if (image.getSize() == 0) {
        dbImage.setUrl("http://placehold.it/300x300");
        list.add(dbImage);
        return list;
    } else {
         dbImage.setUrl(imageService.uploadFile(image));
         list.add(dbImage);
        return list;
    }

此ProductService使用imageService imageService:

    Map<String, String> configuration() {
    Map<String, String> clouMap = new HashMap<>();
    clouMap.put("cloud_name", "dehsjhkzx");
    clouMap.put("api_key", "925657455192194");
    clouMap.put("api_secret", "hLAMwN8M-PtICNCtfr32hNIOFIE");
    return clouMap;
}

public String uploadFile(MultipartFile file) throws IOException {
    File convertedImage = new File(file.getOriginalFilename());
    file.transferTo(convertedImage);
    Cloudinary cloudinary = new Cloudinary(configuration());
    Map uploadResult = cloudinary.uploader().upload(convertedImage, new HashMap());
    convertedImage.delete();
    return (String) uploadResult.get("secure_url");
}

然后代码完成所以我在控制器中获取MultipartFile,然后我将其转换为File然后发送到服务器,顺便说一下,我将使ImageService将图像大小调整为300x300,以便将来它会更慢。有没有办法重构这段代码,以便它能更快地运行

图片实体:

    @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
private String url;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

产品实体:

    @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;

@Column(columnDefinition = "TEXT")
private String description;

private Double price;

@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name = "product_image", joinColumns = { @JoinColumn(name = "product_id") }, inverseJoinColumns = { @JoinColumn(name = "image_id") })
private List<Image> images;

@ManyToOne
@JoinColumn(name = "category_id")
private Category category;

@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name = "product_comment)

0 个答案:

没有答案