我有一个应用程序,用户必须将信息保存到DataBase和图像到文件系统目录,并保存数据库中的图像路径。我的问题是如何从数据库表中获取ID(自动增量)以在数据库中创建路径。例子:car1200.jpeg(car:图像名称,1200:用户的Id)。 我的代码: 控制器:
@RequestMapping(value="/save",method=RequestMethod.POST)
public String add ( @RequestParam("prix") Long prix,
RequestParam("adresse") String ville,
@RequestParam("categorie") String categorie,
@RequestParam("photos") MultipartFile file,
) throws FileNotFoundException
{
String chemin=null;
if (!file.isEmpty())
{
try {
String orgName = file.getOriginalFilename();
// this line to retreive just file name
String
name=orgName.substring(orgName.lastIndexOf("\\")+1,orgName.length());
chemin="e:\\images\\"+name;// here I want to add ID of the user ,I
//don t know how to get since it is auto increment
File file1=new File(chemin);
file.transferTo(file1);
} catch (IOException e) {
e.printStackTrace();
}
}
annonce.setImage(chemin);
annonce.setTitre(prix);
annonce.setCorps(ville);
annonce.setPrix(cetegorie)
annoncedao.save(annonce);
return "SuccessAddAnnonce";
}
Annonce班:
package com.eBenamar.Entities;
import java.io.Serializable;
import java.sql.Blob;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import org.hibernate.annotations.Cascade;
import org.hibernate.validator.constraints.NotEmpty;
/**
* @author user
*
*/
@Entity
public class Annonce implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id @GeneratedValue
Integer id;
@NotEmpty
String titre;
Long prix;
@Column(length=10000000)
byte [] photos;
public byte[] getPhotos() {
return photos;
}
String ville;
String categorie;
@Lob
@Column(length=10000000)
byte [] photos;
public Annonce() {
super();
// TODO Auto-generated constructor stub
}
public Annonce( String ville, Long prix, String categorie,
byte[] photos) {
super();
this.ville= ville;
this.prix = prix;
this.categorie = categorie;
this.photos = photos;
}
public Annonce( String categorie, Long prix, String ville, byte[] photos) {
super();
this.ville= ville;
this.categorie = categorie;
this.prix = prix;
this.photos = photos;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getVille() {
return ville;
}
public void setTVille(String ville) {
this.ville = ville;
}
public String getCategorie() {
return categorie;
}
public void setCategorie(String categorie) {
this.categorie= categorie;
}
public Long getPrix() {
return prix;
}
public void setPrix(Long prix) {
this.prix = prix;
}
public byte[] getPhotos() {
return photos;
}
public void setPhotos(byte[] photos) {
this.photos = photos;
}
}
我想获取数据库中保存的最后一个ID(自动增量)。