最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

Hibernate读写Clob和Blob类型字段

来源:动视网 责编:小OO 时间:2025-10-01 09:38:39
文档

Hibernate读写Clob和Blob类型字段

数据库脚本:create table testcb(id varchar(32) primary key,name varchar(32),photo blob,description text); Hibernate.cfg.xml           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0
推荐度:
导读数据库脚本:create table testcb(id varchar(32) primary key,name varchar(32),photo blob,description text); Hibernate.cfg.xml           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0
数据库脚本:

create table testcb(id varchar(32) primary key,name varchar(32),photo blob,description text);

 Hibernate.cfg.xml

 

          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    rootproperty>

    

        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312&useUnicode=true

    property>

    

        org.hibernate.dialect.MySQLDialect

    property>

    mysqlproperty>

    1234property>

    

        com.mysql.jdbc.Driver

    property>

    

        org.hibernate.dialect.MySQLDialect

    property>

    trueproperty>

    threadproperty>

    

session-factory>

hibernate-configuration>

 

POJO:

 

package Search.Clob_Blob;

import java.sql.Blob;

import java.sql.Clob;

public class TestCB ...{

    private String id; //标识id

    private String name; //学生姓名

    private Blob photo;

    private Clob description;

    public String getId() ...{

        return id;

    }

    public void setId(String id) ...{

        this.id = id;

    }

    public String getName() ...{

        return name;

    }

    public void setName(String name) ...{

        this.name = name;

    }

    public Blob getPhoto() ...{

        return photo;

    }

    public void setPhoto(Blob photo) ...{

        this.photo = photo;

    }

    public Clob getDescription() ...{

        return description;

    }

    public void setDescription(Clob description) ...{

        this.description = description;

    }

    

  

}

 

TestCB.hbm.xml

 

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

   

    

       

         generator>

       id>

       property>

       property>

       property>

      

      class>

hibernate-mapping>

 

准备一个图片放在Clob_Blob包下

 

写Blob和Clob测试代码:

 

package Search.Clob_Blob;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.sql.Blob;

import java.sql.Clob;

import org.hibernate.Hibernate;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class Test ...{

    public static void main(String[] args) ...{

        String filePath=System.getProperty("user.dir")+File.separator+"src/Search/Clob_Blob"+File.separator+"hibernate.cfg.xml";

        File file=new File(filePath);

        SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();

        Session session=sessionFactory.openSession();

        Transaction tx=session.beginTransaction();

        try ...{

            FileInputStream fis=new FileInputStream(imgPath);

            Blob photo=Hibernate.createBlob(fis);

            Clob description=Hibernate.createClob("this is description");

            TestCB testcb=new TestCB();

            testcb.setName("tom1");

            testcb.setPhoto(photo);

            testcb.setDescription(description);

            session.save(testcb);

            tx.commit();

        } catch (FileNotFoundException e) ...{

            e.printStackTrace();

        } catch (IOException e) ...{

            e.printStackTrace();

        }

        

        

    }

}

运行后,客户端中可以看到已经成功保存:

 

运行读取测试代码:

 

package Search.Clob_Blob;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.Reader;

import java.sql.Blob;

import java.sql.Clob;

import java.sql.SQLException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class TestRead ...{

    public static void main(String[] args) ...{

        String filePath=System.getProperty("user.dir")+File.separator+"src/Search/Clob_Blob"+File.separator+"hibernate.cfg.xml";

        File file=new File(filePath);

        SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();

        Session session=sessionFactory.openSession();

        Transaction tx=session.beginTransaction();

        

        //读取clob和blob

        TestCB testcb=(TestCB)session.get(TestCB.class, "1");

        Blob photo=testcb.getPhoto();

        Clob description=testcb.getDescription();

        try ...{

            StringBuffer buffer=new StringBuffer();

            Reader reader=(Reader)description.getCharacterStream();

            BufferedReader breader=new BufferedReader(reader);

            char[] b = new char[60000];//每次获取60K

            //将Clob解析成String

            int i = 0;

            while((i = breader.read(b)) != -1)

            ...{

                buffer.append(b,0,i);

            }

            System.out.println(buffer.toString());

            

            //将Blob还原成文件

            FileOutputStream fos=new FileOutputStream(imgPath);

            InputStream  fis1=(InputStream)photo.getBinaryStream();

            byte[] b1 = new byte[60];//每次获取60K

            int i1 = 0;

            while((i1=fis1.read(b1))!=-1)...{

               fos.write(b1);

            }

    

        } catch (SQLException e) ...{

            e.printStackTrace();

        } catch (IOException e) ...{

            e.printStackTrace();

        }

        tx.commit();

        

        

    }

}

 

结果:

Hibernate: select testcb0_.id as id0_0_, testcb0_.name as name0_0_, testcb0_.photo as photo0_0_, testcb0_.description as descript4_0_0_ from testcb testcb0_ where testcb0_.id=?

this is description

红色部分为解析的Clob,同时在Clob_Blob下可以看到生成的

文档

Hibernate读写Clob和Blob类型字段

数据库脚本:create table testcb(id varchar(32) primary key,name varchar(32),photo blob,description text); Hibernate.cfg.xml           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top