Hibernate读写Clob和Blob类型字段

系统 1566 0

数据库脚本:

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

 Hibernate.cfg.xml

 

<? xml version='1.0' encoding='UTF-8' ?>
<! DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

<!--  Generated by MyEclipse Hibernate Tools.                    -->
< hibernate-configuration >

< session-factory >
    
< property  name ="connection.username" > root </ property >
    
< property  name ="connection.url" >
        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312
&amp; useUnicode=true
    
</ property >
    
< property  name ="dialect" >
        org.hibernate.dialect.MySQLDialect
    
</ property >
    
< property  name ="myeclipse.connection.profile" > mysql </ property >
    
< property  name ="connection.password" > 1234 </ property >
    
< property  name ="connection.driver_class" >
        com.mysql.jdbc.Driver
    
</ property >
    
< property  name ="hibernate.dialect" >
        org.hibernate.dialect.MySQLDialect
    
</ property >
    
< property  name ="hibernate.show_sql" > true </ property >
    
< property  name ="current_session_context_class" > thread </ property >
    
< mapping  resource ="Search/Clob_Blob/TestCB.hbm.xml"   />

</ 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

 

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="Search.fetch"   >
   
    
< class  name ="Search.Clob_Blob.TestCB"  table ="testcb"  lazy ="true" >
       
< id  name ="id"  column ="id"  unsaved-value ="null" >
         
< generator  class ="uuid.hex" ></ generator >
       
</ id >

       
< property  name ="name"  column ="name"  type ="string" ></ property >
       
< property  name ="photo"  column ="photo"  type ="blob" ></ property >
       
< property  name ="description"  column ="description"  type ="clob" ></ property >
      
      
</ class >
</ hibernate-mapping >

 

准备一个图片sample.jpg放在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   ... {
            String imgPath
= System.getProperty( " user.dir " ) + File.separator + " src/Search/Clob_Blob " + File.separator + " sample.jpg " ;
            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();
        }

        
        

    }


}


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

Hibernate读写Clob和Blob类型字段

运行读取测试代码:

 

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
        String imgPath = System.getProperty( " user.dir "

Hibernate读写Clob和Blob类型字段


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论