当前位置:K88软件开发文章中心编程全书编程全书01 → 文章内容

利用Hibernate储存大对象到达梦数据库

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2019-1-3 2:25:05

rows="20" cols="80" name="clobTest" ></textarea>
     </td>
     </tr>
    <tr>
     <td>附件:</td>
     <td>
      <input type="file" name="blobTest" size="71">
     </td>
    </tr>
    <tr>
     <td colspan="2" align="center">
      <input type="submit" value=" 确  认 ">
     </td>
    </tr>
    <tr>
     <td colspan="2" align="center"> <font id="info" color="red"><%=message %></font></td>
    </tr>
   </table>
  </form>
 
  处理业务逻辑类:

  public class TestLobAction extends DispatchAction{

  public ActionForward save(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws   Exception {
  try{
     //设置文件标题
     String title = request.getParameter("title");
     TestLob testLob = new TestLob();
     testLob.setTitle(title);
   
     //处理blob字段
     Hashtable files = form.getMultipartRequestHandler().getFileElements();
     FormFile blobTestFile = (FormFile) files.get("blobTest");//得到文件
     String blobName = blobTestFile.getFileName();            //得到文件名
     testLob.setBlobName(blobName);                           //设置文件
     byte[] blobContent=blobTestFile.getFileData();           //得到文件内容
     testLob.setBlobContent(blobContent);                     //设置文件内容
   
     //处理clob字段
     String clobName = title;      //文件名就是标题名
     String clobContent = request.getParameter("clobTest");   //得到文件
     testLob.setClobName(clobName);                           //设置文件名
     testLob.setClobContent(clobContent);                     //设置文件
   
     TestLobService testLobService = TestLobService.getInstance();
     if(testLobService.createTestLob(testLob)==0){
      request.setAttribute("message", "上传失败");
      return list(mapping,form,request,response);
     }
    }catch(Exception e){
     throw new BaseException();
    }
    request.setAttribute("message", "上传成功");
    return list(mapping,form,request,response);
  }
   }
 
  因为clob字段对应的是String类型,所以可以直接将表单中得到的数据设置到对象的属性中去,而blob类型处理的是二进制类型的文件,我们需要将得到的文件以流的形式存入到对象的属性中,这里我使用了struts的FormFile组件来进行文件的上传,将得到的数据流设置到对象的属性中。

  完成以上操作后我们就可以将设置好的对象用以下面的方法保存数据到数据库中:

  首先定义一个Hibernate的Session管理类:HibernateUtil.java,它是使用ThreadLocal类建立的一个Session管理的辅助类。

  package com.dm.lobtest.util;

  import org.hibernate.HibernateException;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;

  public class HibernateUtil {
        private static SessionFactory sessionFactory = null;

        public static final ThreadLocal session = new ThreadLocal();

        public static Session currentSession() throws HibernateException {
                if (sessionFactory == null) {
                        if (getSystemSessionFactory() == false) {
                                throw new HibernateException(
                                                "Exception geting SessionFactory ! ");
                        }
                }
                Session s = (Session) session.get();
                // 如果该线程还不存在,打开一个新的Session
                if (s == null) {
                        s = sessionFactory.openSession();
                        session.set(s);
                }
                return s;
        }

        public static void closeSession() throws HibernateException {
                Session s = (Session) session.get();
                session.set(null);
                if (s != null)
                        s.close();
        }
  }
 
  最后利用Hibernate的运作中心--Session接口的save()方法保存对象并提交相关的Session实例:

  public int createTestLob(TestLob testLob){
   Session session = null;
   try{
      session = HibernateUtil.currentSession();
      Transaction tr = session.beginTransaction();
      session.save(testLob);
      tr.commit();
      return 1;
   }catch(Exception e){
    e.printStackTrace();
    return 0;
   }finally {
            try {
                if (session != null) {
                    HibernateUtil.closeSession();
                }
             }catch (Exception ex) {}
         }
  }

  现在我们已经利用Hibernate完成了多个不同的大数据类型存储到达梦数据库中的全过程。

上一页  [1] [2] 


利用Hibernate储存大对象到达梦数据库