【发布时间】:2021-05-29 11:33:15
【问题描述】:
我在数据库中插入 blob 类型照片时遇到问题。我写了代码,我试图用 Postman 测试它,但我发现没有任何反应,也就是说,照片没有插入到数据库中。 我指定我没有收到任何错误。 这是代码:
Product.java
package Model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import Model.Product;
@Entity
@Table(name="product")
public class Product {
@Id
private String prodcode;
private String name;
@Lob
private byte[] photo;
public Product() {
}
public Product(String prodcode, String name, byte[] photo) {
this.prodcode = prodcode;
this.name = name;
this.photo = photo;
}
public String getProdcode() {
return prodcode;
}
public void setProdcode(String prodcode) {
this.prodcode = prodcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
}
Product_DAO_Imp
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.example.Model.*;
@Repository
public class Product_DAO_Imp implements Product_DAO{
@Autowired
private SessionFactory sessionFactory;
@Override
public boolean updatePhoto(byte[] photo, String prodcode) {
boolean status = false;
Session currentSession= sessionFactory.getCurrentSession();
try
{
Query<Product> query = currentSession.createQuery("update Product set photo = :photo where prodcode=:prodcode ");
query.setParameter("photo", photo);
query.setParameter("prodcode", prodcode);
status=true;
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
}
Product_Service_Imp
package com.example.Service;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.example.DAO.Product_DAO;
import com.example.Model.Product;
@Service
@Transactional
public class Product_Service_Imp implements Product_Service {
@Autowired
private Product_DAO productdao;
@Override
public boolean store(MultipartFile file, String prodcode) throws IOException {
boolean status = false;
try {
Product product=new Product();
byte[] photo=file.getBytes();
System.out.println(prodcode);
System.out.println(photo);
productdao.updatePhoto(photo, prodcode);
status=true;
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
}
Controller.java
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.example.Model.Product;
import com.example.Service.Product_Service;
@RestController
@CrossOrigin(origins="http://localhost:4200")
@RequestMapping(value="/api")
public class Controller {
@Autowired
private Product_Service productservice;
@PostMapping("/save-photoproduct/{prodcode}")
public boolean handleFileUpload(@PathVariable String prodcode,@RequestParam("name") String name,@RequestParam("file") MultipartFile file) throws IOException {
return productservice.store(file,prodcode);
}
}
【问题讨论】:
标签: spring-boot hibernate