程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> mybatis oracle BLOB類型字段保存與讀取,mybatisblob

mybatis oracle BLOB類型字段保存與讀取,mybatisblob

編輯:Oracle教程

mybatis oracle BLOB類型字段保存與讀取,mybatisblob


一、BLOB字段
  BLOB是指二進制大對象也就是英文Binary Large Object的所寫,而CLOB是指大字符對象也就是英文Character Large Object的所寫。其中BLOB是用來存儲大量二進制數據的;CLOB用來存儲大量文本數據。BLOB通常用來保存圖片、文件等二進制類型的數據。

二、使用mybatis操作blob

  1、表結構如下:

create table BLOB_FIELD
(
   ID                   VARCHAR2(64 BYTE)    not null,
   TAB_NAME             VARCHAR2(64 BYTE)    not null,
   TAB_PKID_VALUE       VARCHAR2(64 BYTE)    not null,
   CLOB_COL_NAME        VARCHAR2(64 BYTE)    not null,
   CLOB_COL_VALUE       CLOB,
   constraint PK_BLOB_FIELD primary key (ID)
);

  2、實體代碼如下:

 1 package com.test.entity;
 2 
 3 import java.sql.Clob; 
 4 
 5 /**
 6  * 大字段 
 7  */
 8 public class BlobField { 
 9 
10     private String tabName;// 表名
11     private String tabPkidValue;// 主鍵值
12     private String blobColName;// 列名
13     private byte[] blobColValue;// 列值 clob類型
14 
15     public String getTabName() {
16         return tabName;
17     }
18 
19     public void setTabName(String tabName) {
20         this.tabName = tabName;
21     }
22 
23     public String getTabPkidValue() {
24         return tabPkidValue;
25     }
26 
27     public void setTabPkidValue(String tabPkidValue) {
28         this.tabPkidValue = tabPkidValue;
29     }
30 
31     public String getBlobColName() {
32         return blobColName;
33     }
34 
35     public void setBlobColName(String blobColName) {
36         this.blobColName = blobColName;
37     }
38 
39     public byte[] getBlobColValue() {
40         return blobColValue;
41     }
42 
43     public void setBlobColValue(byte[] blobColValue) {
44         this.blobColValue = blobColValue;
45     }
46 
47 }

  3、mybatis sql代碼如下:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.test.dao.BlobFieldDao">
 4 
 5     <sql id="blobFieldColumns">
 6         a.ID AS id,
 7         a.TAB_NAME AS tabName,
 8         a.TAB_PKID_VALUE AS tabPkidValue,
 9         a.BLOB_COL_NAME AS blobColName,
10         a.BLOB_COL_VALUE AS blobColValue
11     </sql>
12 
13     <sql id="blobFieldJoins">
14     </sql>
15 
16     <select id="get" resultType="blobField">
17         SELECT
18         <include refid="blobFieldColumns" />
19         FROM BLOB_FIELD a
20         <include refid="blobFieldJoins" />
21         WHERE a.ID = #{id}
22     </select>
23 
24     <select id="findList" resultType="blobField">
25         SELECT
26         <include refid="blobFieldColumns" />
27         FROM BLOB_FIELD a
28         <include refid="blobFieldJoins" />
29     </select> 
30 
31     <insert id="insert">
32         INSERT INTO BLOB_FIELD(
33         ID ,
34         TAB_NAME ,
35         TAB_PKID_VALUE ,
36         BLOB_COL_NAME ,
37         BLOB_COL_VALUE
38         ) VALUES (
39         #{id},
40         #{tabName},
41         #{tabPkidValue},
42         #{blobColName},
43         #{blobColValue,jdbcType=BLOB}
44         )
45     </insert>
46 
47     <update id="update">
48         UPDATE BLOB_FIELD SET
49         TAB_NAME = #{tabName},
50         TAB_PKID_VALUE = #{tabPkidValue},
51         BLOB_COL_NAME = #{blobColName},
52         BLOB_COL_VALUE = #{blobColValue}
53         WHERE ID = #{id}
54     </update>
55     <delete id="delete">
56         DELETE FROM BLOB_FIELD 
57         WHERE ID = #{id}
58     </delete>
59     
60 </mapper>

  3、controller代碼如下:

  a、保存BLOB字段代碼

 1 /**
 2      * 附件上傳
 3      * 
 4      * @param testId
 5      *            主表Id
 6      * @param request
 7      * @return
 8      * @throws UnsupportedEncodingException
 9      */
10     @RequiresPermissions("exc:exceptioninfo:feedback")
11     @RequestMapping(value = "attachment", method = RequestMethod.POST)
12     @ResponseBody
13     public Map<String, Object> uploadAttachment(@RequestParam(value = "testId", required = true) String testId, 
14 
15 HttpServletRequest request)
16             throws UnsupportedEncodingException {
17         Map<String, Object> result = new HashMap<String, Object>();
18 
19         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
20         // 獲得文件
21         MultipartFile multipartFile = multipartRequest.getFile("Filedata");// 與前端設置的fileDataName屬性值一致
22         String filename = multipartFile.getOriginalFilename();// 文件名稱
23         InputStream is = null;
24         try {
25             //讀取文件流
26             is = multipartFile.getInputStream();
27             byte[] bytes = FileCopyUtils.copyToByteArray(is);
28             BlobField blobField = new BlobField();
29             blobField.setTabName("testL");
30             blobField.setTabPkidValue(testId);
31             blobField.setBlobColName("attachment");
32             blobField.setBlobColValue(bytes);
33             //保存blob字段
34             this.testService.save(blobField, testId, filename);
35             result.put("flag", true);
36             result.put("attachmentId", blobField.getId());
37             result.put("attachmentName", filename);
38         } catch (IOException e) {
39             e.printStackTrace();
40             result.put("flag", false);
41         } finally {
42             IOUtils.closeQuietly(is);
43         }
44         return result;
45     }    

  b、讀取BLOB字段

 1 /**
 2      * 下載附件
 3      * 
 4      * @param attachmentId
 5      * @return
 6      */
 7     @RequiresPermissions("exc:exceptioninfo:view")
 8     @RequestMapping(value = "download", method = RequestMethod.GET)
 9     public void download(@RequestParam(value = "attachmentId", required = true) String attachmentId,
10             @RequestParam(value = "attachmentName", required = true) String attachmentName, HttpServletRequest 
11 
12 request, HttpServletResponse response) {
13         ServletOutputStream out = null;
14         try {
15             response.reset();
16             String userAgent = request.getHeader("User-Agent");
17             byte[] bytes = userAgent.contains("MSIE") ? attachmentName.getBytes() : attachmentName.getBytes("UTF-
18 
19 8"); // fileName.getBytes("UTF-8")處理safari的亂碼問題
20             String fileName = new String(bytes, "ISO-8859-1");
21             // 設置輸出的格式
22             response.setContentType("multipart/form-data");
23             response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(attachmentName, 
24 
25 "UTF-8"));
26             BlobField blobField = this.blobFieldService.get(attachmentId);
27             //獲取blob字段
28             byte[] contents = blobField.getBlobColValue();
29             out = response.getOutputStream();
30             //寫到輸出流
31             out.write(contents);
32             out.flush();
33         } catch (IOException e) {
34             e.printStackTrace();
35         }
36     }                                

  本例子將文件上傳並保存到BLOB類型字段字段,下載的時候讀取BLOB字段,並寫入成輸出流。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved