<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 從數據庫中插入/選擇 Blob 的 Hiberate 示例 > 原文: [https://howtodoinjava.com/hibernate/hibernate-example-of-insertselect-blob-from-database/](https://howtodoinjava.com/hibernate/hibernate-example-of-insertselect-blob-from-database/) 在先前的 [**Hiberate 教程**](//howtodoinjava.com/hibernate-tutorials/ "hibernate tutorials")中,我們了解了[**一級緩存**](//howtodoinjava.com/hibernate/understanding-hibernate-first-level-cache-with-example/ "first level cache"),[**二級緩存**](//howtodoinjava.com/hibernate/how-hibernate-second-level-cache-works/)和一些[**映射示例**](//howtodoinjava.com/hibernate/hibernate-one-to-one-mapping-using-annotations/)等。這是 [**Hiberate 相關教程**](//howtodoinjava.com/category/frameworks/hibernate/)的完整列表。 在本文中,我將舉一個使用 hibernate 將 BLOB 數據插入數據庫并使用 hibernate 實體從數據庫中獲取數據的示例。 簡而言之,插入和獲取 BLOB 數據(例如圖像)需要兩個步驟:將數據庫列類型定義為“BLOB”,并且在實體中具有“字節數組”類型的字段。 讓我們舉個例子,其中,我將 Windows C 驅動器中的“`test.png`”圖像插入數據庫(MySQL)。 然后,我將再次從數據庫中讀取圖像數據并將其存儲到其他位置。 ## Hiberate 實體 請注意,我已將數據字段聲明為`byte[]`。 ```java @Entity @Table(name = "TBL_IMAGES") public class ImageWrapper implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) private Integer id; @Column(name = "IMAGE_NAME", unique = false, nullable = false, length = 100) private String imageName; @Column(name = "DATA", unique = false, nullable = false, length = 100000) private byte[] data; //Getters and Setters } ``` ## 將 Blob 數據插入數據庫 讓我們看一下代碼: ```java Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); File file = new File("C:\test.png"); byte[] imageData = new byte[(int) file.length()]; try { FileInputStream fileInputStream = new FileInputStream(file); fileInputStream.read(imageData); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } ImageWrapper image = new ImageWrapper(); image.setImageName("test.jpeg"); image.setData(imageData); session.save(image); //Save the data session.getTransaction().commit(); HibernateUtil.shutdown(); ``` 執行完上述代碼后,您可以驗證是否已在數據庫中創建表。 并且創建了一個 BLOB 列來保存圖像數據。 ![Hibernate blob example](https://img.kancloud.cn/2e/be/2ebecc679dfeb06e86fcfa9f19838371_929x276.png "Hibernate blob example") Hibernate blob 示例 ## 從數據庫讀取 Blob 數據 這很簡單,實際上您不需要執行任何其他操作。 上面的實體定義可以正常工作。 ```java Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); ImageWrapper imgNew = (ImageWrapper)session.get(ImageWrapper.class, 1); byte[] bAvatar = imgNew.getData(); try{ FileOutputStream fos = new FileOutputStream("C:\temp\test.png"); fos.write(bAvatar); fos.close(); }catch(Exception e){ e.printStackTrace(); } session.getTransaction().commit(); HibernateUtil.shutdown(); ``` ## Hiberate 配置 作為參考,這是我在此示例中使用的配置: **`hibernate.cfg.xml`** ```java <?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"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hibernate.hbm2ddl.auto">create</property> <mapping class="hibernate.test.dto.ImageWrapper"></mapping> </session-factory> </hibernate-configuration> ``` 以下也是`HibernateUtil.java`的代碼 **`HibernateUtil.java`** ```java public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); @SuppressWarnings("deprecation") private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new AnnotationConfiguration().configure(new File ("D:\Latest Setup\eclipse_juno_workspace\hibernate-test-project\hibernate.cgf.xml")) .buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { getSessionFactory().close(); } } ``` 如果仍然感到麻煩,請下載隨附的源代碼。 [**源碼下載**](https://docs.google.com/file/d/0B7yo2HclmjI4R0dDNzY5OTR6ZG8/edit?usp=sharing "hibernate blob example") **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看