Hibernate数据类型映射关系

本文介绍了一种使用Hibernate框架进行图片BLOB类型数据的存取操作方法。具体包括如何将图片文件转换为BLOB对象并保存到数据库,以及如何从数据库中读取BLOB数据并将其还原为图片文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、基本类型:




二、对象类型:



三、BLOB类型的读写操作:

PictureTest.java】


public class PictureTest {
      private SessionFactory sessionFactory ;
      private Session session ;
      private Transaction transaction ;
     
      @Before
      public void init(){
            //创建配置对象
           Configuration config= new Configuration().configure();
            //创建服务配置对象
           ServiceRegistry serviceRegistry= new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
            //创建会话工厂对象
           sessionFactory =config.buildSessionFactory(serviceRegistry);
            //创建会话对象
            session = sessionFactory .openSession();
            //开启事务
            transaction = session .beginTransaction();
     }
     
      @After
      public void destroy(){
            //提交事务
            transaction .commit();
            //关闭会话
            session .close();
            //关闭会话工厂
            sessionFactory .close();
     }
     
      @Test
      public void testSavePicture() throws Exception{
           String picPath=getWebRootPath()+ "/image/pic1.jpg" ;
            //获得图片文件
           File file= new File(picPath);
            //获得图片文件的输入流
           InputStream inputStream= new FileInputStream(file);
            //创建BLOB对象
           Blob headPicture=Hibernate.getLobCreator( session ).createBlob(inputStream, inputStream.available());
           Picture picture= new Picture();
            //设置学号
           picture.setSno( "20121120162" );
            //设置头像
           picture.setHead(headPicture);
            session .save(picture);
           
     }
     
      @Test
      public void testReadPicture() throws Exception{
           Picture picture= new Picture();
           picture=(Picture) session .get(Picture. class , 1);
           Blob headPicture=picture.getHead();
           InputStream inputStream=headPicture.getBinaryStream();
            byte buffer[]= new byte [inputStream.available()];
           File file= new File(getWebRootPath()+ "/image/pic2.jpg" );
           OutputStream outputStream= new FileOutputStream(file);
           inputStream.read(buffer);
           outputStream.write(buffer);
           inputStream.close();
           outputStream.close();
           
     }
     
      private String getWebRootPath(){
           ClassLoader classLoader = Thread. currentThread()
                 .getContextClassLoader();
         if (classLoader == null ) {
             classLoader = ClassLoader. getSystemClassLoader();
         }
         java.net.URL url = classLoader.getResource( "" );
         String ROOT_CLASS_PATH = url.getPath() + "/" ;
         File rootFile = new File(ROOT_CLASS_PATH);
         String WEB_INFO_DIRECTORY_PATH = rootFile.getParent() + "/" ;
         File webInfoDir = new File(WEB_INFO_DIRECTORY_PATH);
         String SERVLET_CONTEXT_PATH = webInfoDir.getParent() + "/" ;
         //这里 SERVLET_CONTEXT_PATH 就是WebRoot的路径
         String path = SERVLET_CONTEXT_PATH;
         path = path.replaceAll( "%20" , " " );
         return path;
     }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值