Hibernate数据库写入图片

来源:互联网 发布:淘宝导航 编辑:程序博客网 时间:2024/05/22 02:02

一、实体类文件Students

import java.sql.Blob;import java.util.Date;public class Students {    // 1.公有的类    // 2.提供公有的不带参数的默认的构造方法    // 3.属性私有    // 4.属性setter/getter封装    private int sid;// 学号    private String sname;// 姓名    private String gender;// 性别    private Date birthday;// 出生日期    private String address;// 地址    private Blob picture;//照片 Blob 大数据存储对象}

二、Students.hbm.xml文件

<hibernate-mapping>    <class name="Students" table="STUDENTS">        <id name="sid" type="int">            <column name="SID" />            <generator class="assigned" />        </id>        <property name="sname" type="java.lang.String">            <column name="SNAME" />        </property>        <property name="gender" type="java.lang.String">            <column name="GENDER" />        </property>        <property name="birthday" type="java.util.Date">            <column name="BIRTHDAY" />        </property>        <property name="address" type="java.lang.String">            <column name="ADDRESS" />        </property>        <property name="picture" type="java.sql.Blob">            <column name="PICTURE" />        </property>    </class></hibernate-mapping>

三、hibernate.cfg.xml

<hibernate-configuration>    <session-factory>        <property name="connection.username">root</property>        <property name="connection.password">root</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>        <!-- 方言 -->        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="show_sql">true</property><!-- 输出sql语句到控制台 -->        <property name="format_sql">true</property><!-- 对sql语句排版 -->        <property name="hbm2ddl.auto">update</property><!-- 生成数据库表结构策略 create每次创建新的数据库表 update在原有的表上更新数据 -->        <!-- getCurrent()方法获取session时添加<property name="hibernate.current_session_context_class">thread</property> -->        <mapping resource="Students.hbm.xml"/>    </session-factory></hibernate-configuration>

junit测试类

public class StudentsTest {    private SessionFactory sessionFactory;    private Session session;    private Transaction transaction;    @Before    public void init(){        //创建配置对象 用于读取配置文档hibernate.cfg.xml 目的创建sessionFactory        Configuration config = new Configuration().configure();        //获得服务注册对象        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();        //创建会话工厂对象 创建SessionFactory对象时会加载里面的Students.hbm.xml等文件 目的创建Session对象        sessionFactory = config.buildSessionFactory(serviceRegistry);        //会话对象 Session对象类似jdbc中的connection        session = sessionFactory.openSession();        //开启事务        transaction = session.beginTransaction();    }    @After    public void destory(){        transaction.commit();// 提交事务        session.close();//关闭会话        sessionFactory.close();//关闭会话工厂    }    @Test    public void testSaveStudents(){        Students s = new Students(2, "张三丰", "男", new Date(), "武当");        session.save(s);//保存对象进入数据库    }    @Test    public void testWriteBlob() throws Exception{        Students s = new Students(1, "张三丰", "男", new Date(), "武当");        //先获得照片文件        File f = new File("D:"+File.separator+"ceshi.jpg");        //获得照片文件的输入流        InputStream input = new FileInputStream(f);        //创建一个Blob对象        Blob image = Hibernate.getLobCreator(session).createBlob(input, input.available());        //设置照片属性        s.setPicture(image);        //保存学生        session.save(s);    }    @Test    public void testReadBlob() throws Exception{        Students s = (Students)session.get(Students.class, 1);        //获得Blob对象        Blob image = s.getPicture();        //获得输入流        InputStream input = image.getBinaryStream();        //创建输出流        File f = new File("D:"+File.separator+"sss.jpg");        //获得输出流        OutputStream output = new FileOutputStream(f);        //创建缓冲区        byte[] buff = new byte[input.available()];        input.read(buff);        output.write(buff);        input.close();        output.close();    }}
0 0
原创粉丝点击