使用hibernate框架添加数据时,没有错误,但是数据库里没有记录
//这是实体类的配置文件<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEhibernate-mappingPUBLIC"-//H...
//这是实体类的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="cn.com.shxt.po">
<class name="PhotoUp" table="PHOTOUP">
<id name="id">
<column name="ID"></column>
<generator class="sequence">
<param name="sequence">PHOTOUP_SEQ</param>
</generator>
</id>
<property name="fileName" column="FILENAME"></property>
<property name="photo" column="PHOTO" type="binary"></property>
</class>
</hibernate-mapping>
//action代码
public static String upload(File file,String fileName) throws IOException{
String path = ServletActionContext.getServletContext().getRealPath("/upload");
String newName = FileUpload.getFileName(fileName);
File newFile = new File(path,newName);//就是这句话完成的 文件上传功能
FileUtils.copyFile(file, newFile);//struts2帮助文档,这句有什么作用?
return path+"\\"+newName;
}
//serv代码
public void photoUp(String pathAndName) throws IOException{
Session session = HibernateTools.getSession();
String fileName = pathAndName.substring(pathAndName.lastIndexOf("\\")+1);
PhotoUp img = new PhotoUp();
img.setFileName(fileName);
File photo = new File(pathAndName);
FileInputStream input = new FileInputStream(photo);
byte[] photoBinary = new byte[(int) photo.length()];
input.read(photoBinary,0,(int) photo.length());
img.setPhoto(photoBinary);
session.save(img);
session.close();
} 展开
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="cn.com.shxt.po">
<class name="PhotoUp" table="PHOTOUP">
<id name="id">
<column name="ID"></column>
<generator class="sequence">
<param name="sequence">PHOTOUP_SEQ</param>
</generator>
</id>
<property name="fileName" column="FILENAME"></property>
<property name="photo" column="PHOTO" type="binary"></property>
</class>
</hibernate-mapping>
//action代码
public static String upload(File file,String fileName) throws IOException{
String path = ServletActionContext.getServletContext().getRealPath("/upload");
String newName = FileUpload.getFileName(fileName);
File newFile = new File(path,newName);//就是这句话完成的 文件上传功能
FileUtils.copyFile(file, newFile);//struts2帮助文档,这句有什么作用?
return path+"\\"+newName;
}
//serv代码
public void photoUp(String pathAndName) throws IOException{
Session session = HibernateTools.getSession();
String fileName = pathAndName.substring(pathAndName.lastIndexOf("\\")+1);
PhotoUp img = new PhotoUp();
img.setFileName(fileName);
File photo = new File(pathAndName);
FileInputStream input = new FileInputStream(photo);
byte[] photoBinary = new byte[(int) photo.length()];
input.read(photoBinary,0,(int) photo.length());
img.setPhoto(photoBinary);
session.save(img);
session.close();
} 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询