欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Hibernate单表操作] 对象类型

程序员文章站 2022-07-12 18:09:38
...
一 对象类型

[Hibernate单表操作] 对象类型
            
    
    博客分类: Hibernate 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、测试结果

[Hibernate单表操作] 对象类型
            
    
    博客分类: Hibernate Hibernate对象类型 
 
 
三 将图片从数据库读出
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、测试结果

 


[Hibernate单表操作] 对象类型
            
    
    博客分类: Hibernate Hibernate对象类型 
 
  • [Hibernate单表操作] 对象类型
            
    
    博客分类: Hibernate Hibernate对象类型 
  • 大小: 153.6 KB
  • [Hibernate单表操作] 对象类型
            
    
    博客分类: Hibernate Hibernate对象类型 
  • 大小: 29.5 KB
  • [Hibernate单表操作] 对象类型
            
    
    博客分类: Hibernate Hibernate对象类型 
  • 大小: 5.2 KB