[Hibernate单表操作] 对象类型

一 对象类型


 
 
二 将图片写入数据库
1、Students
import java.sql.Blob;
import java.util.Date;
//学生类
public class Students {
        /*
         * JavaBeans的四点原则
         * 1.必须是公有的类
         * 2.提供公有的不带参数的默认的构造方法
         * 3.属性私有
         * 4.属性setter/getter封装
         */
        private int sid; // 学号
        private String sname; // 姓名
        private String gender; // 性别
        private Date birthday; // 出生日期
        private String address; // 地址
        private Blob picture;//照片
        
        public Blob getPicture() {
                return picture;
        }
 
        public void setPicture(Blob picture) {
                this.picture = picture;
        }
 
        public Students() {
        }
 
        public Students(int sid, String sname, String gender, Date birthday,
                        String address) {
                // super();
                this.sid = sid;
                this.sname = sname;
                this.gender = gender;
                this.birthday = birthday;
                this.address = address;
        }
 
        public int getSid() {
                return sid;
        }
 
        public void setSid(int sid) {
                this.sid = sid;
        }
 
        public String getSname() {
                return sname;
        }
 
        public void setSname(String sname) {
                this.sname = sname;
        }
 
        public String getGender() {
                return gender;
        }
 
        public void setGender(String gender) {
                this.gender = gender;
        }
 
        public Date getBirthday() {
                return birthday;
        }
 
        public void setBirthday(Date birthday) {
                this.birthday = birthday;
        }
 
        public String getAddress() {
                return address;
        }
 
        public void setAddress(String address) {
                this.address = address;
        }
 
        @Override
        public String toString() {
                return "Students [sid=" + sid + ", sname=" + sname + ", gender="
                                + gender + ", birthday=" + birthday + ", address=" + address
                                + "]";
        }
 
}
2、hibernate.cfg.xml配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<!-- Generated 2017-9-17 10:33:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="Students" table="STUDENTS">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="native" />
            <!--  <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="timestamp">
            <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>
3、测试方法
        @Test
        public void testWriteBlog() throws Exception{
                Students s= new Students(1,"张三丰","男",new Date(),"武当山");
                //先获得照片文件
                File f = new File("E:"+File.separator+"1.png");
                //获得照片文件的输入流
                InputStream input = new FileInputStream(f);
                //创建一个Blob对象
                Blob image = Hibernate.getLobCreator(session).createBlob(input, input.available());
                //设置照片属性
                s.setPicture(image);
            session.save(s);
        }
4、测试结果


 
 
三 将图片从数据库读出
1、测试方法
        @Test
        public void testReadBlog() throws Exception{
                Students s= (Students)session.get(Students.class, 1);
                //获得Blob对象
                Blob image = s.getPicture();
                //获得照片的输入流
                InputStream input = image.getBinaryStream();
                //创建文件
                File f = new File("E:"+File.separator+"2.png");
                //获得输出流
                OutputStream output = new FileOutputStream(f);
                //创建缓冲区
                byte[] buff = new byte[input.available()];
                input.read(buff);
                output.write(buff);
                input.close();
                output.close();
        }
2、测试结果

 



 
  • 大小: 153.6 KB
  • 大小: 29.5 KB
  • 大小: 5.2 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值