Hibernate保存图片 文件 到数据库
By:Roy.LiuLast updated:2013-10-15
为了在数据库中保存文件,需要在数据库中定义二进制指端blob. 有的数据库也有 image 类型也可以。在hibernate 实体类 这一边, 定义 类型为 byte[] 就可以。做一个简单的例子测试
1. 在mysql 中创建表
2. 项目所需要的 maven 依赖
3. model 层编写
4. Hiberante mapping 配置文件 Avatar.hbm.xml
5. Hibernate 配置文件 hibernate.cfg.xml
6. Hibernate 工具类
7. 编写测试代码,并进行测试
由此可见,hibernate 保存文件到数据库,起始并不难,其实就是保存 byte[] , 注意到这点,应该就好处理了。
1. 在mysql 中创建表
Create TABLE `yihaomen`.`avatar` ( `AVATAR_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `IMAGE` BLOB NOT NULL, PRIMARY KEY (`AVATAR_ID`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2. 项目所需要的 maven 依赖
4.0.0 com.yihaomen.common HibernateExample jar 1.0-SNAPSHOT HibernateExample http://maven.apache.org junit junit 3.8.1 test mysql mysql-connector-java 5.1.9 hibernate hibernate3 3.2.3.GA dom4j dom4j 1.6.1 commons-logging commons-logging 1.1.1 commons-collections commons-collections 3.2.1 cglib cglib 2.2
3. model 层编写
public class Avatar implements java.io.Serializable { private Integer avatarId; private byte[] image; public Avatar() { } public Avatar(byte[] image) { this.image = image; } public Integer getAvatarId() { return this.avatarId; } public void setAvatarId(Integer avatarId) { this.avatarId = avatarId; } public byte[] getImage() { return this.image; } public void setImage(byte[] image) { this.image = image; } }
4. Hiberante mapping 配置文件 Avatar.hbm.xml
5. Hibernate 配置文件 hibernate.cfg.xml
false com.mysql.jdbc.Driver password jdbc:mysql://localhost:3306/yihaomen root org.hibernate.dialect.MySQLDialect true
6. Hibernate 工具类
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); } }
7. 编写测试代码,并进行测试
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.hibernate.Session; import com.mkyong.persistence.HibernateUtil; public class App { public static void main( String[] args ) { System.out.println("Hibernate save image into database"); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //save image into database File file = new File("C:\\mavan-hibernate-image-mysql.gif"); byte[] bFile = new byte[(int) file.length()]; try { FileInputStream fileInputStream = new FileInputStream(file); //convert file into array of bytes fileInputStream.read(bFile); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } Avatar avatar = new Avatar(); avatar.setImage(bFile); session.save(avatar); //Get image from database Avatar avatar2 = (Avatar)session.get(Avatar.class, avatar.getAvatarId()); byte[] bAvatar = avatar2.getImage(); try{ FileOutputStream fos = new FileOutputStream("C:\\test.gif"); fos.write(bAvatar); fos.close(); }catch(Exception e){ e.printStackTrace(); } session.getTransaction().commit(); } }
由此可见,hibernate 保存文件到数据库,起始并不难,其实就是保存 byte[] , 注意到这点,应该就好处理了。
From:一号门
COMMENTS