MyBatis读取MySql中BLOB数据

首先自定义一个handler用来处理blob和string之间的转换

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.sql.Blob;  
  4. import java.sql.CallableStatement;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8.   
  9. import org.apache.commons.logging.Log;  
  10. import org.apache.commons.logging.LogFactory;  
  11. import org.apache.ibatis.type.BaseTypeHandler;  
  12. import org.apache.ibatis.type.JdbcType;  
  13.   
  14. /** 
  15.  * 项目名称: 
  16.  *  
  17.  * @description: 
  18.  *  
  19.  * @author spg 
  20.  *  
  21.  * @create_time:2014年7月24日 下午8:49:02 
  22.  *  
  23.  * @version V1.0.0 
  24.  *  
  25.  */  
  26. public class MyBlobTypeHandler extends BaseTypeHandler<String>  
  27. {  
  28.   
  29.     private static final Log LOGGER = LogFactory  
  30.             .getLog(MyBlobTypeHandler.class);  
  31.     // 指定字符集  
  32.     private static final String DEFAULT_CHARSET = "utf-8";  
  33.   
  34.     @Override  
  35.     public void setNonNullParameter(PreparedStatement ps, int i,  
  36.             String parameter, JdbcType jdbcType) throws SQLException  
  37.     {  
  38.         ByteArrayInputStream bis;  
  39.         try  
  40.         {  
  41.             // 把String转化成byte流  
  42.             bis = new ByteArrayInputStream(parameter.getBytes(DEFAULT_CHARSET));  
  43.         } catch (UnsupportedEncodingException e)  
  44.         {  
  45.             throw new RuntimeException("Blob Encoding Error!");  
  46.         }  
  47.         ps.setBinaryStream(i, bis, parameter.length());  
  48.     }  
  49.   
  50.     @Override  
  51.     public String getNullableResult(ResultSet rs, String columnName)  
  52.             throws SQLException  
  53.     {  
  54.         Blob blob = rs.getBlob(columnName);  
  55.         byte[] returnValue = null;  
  56.         String result = null;  
  57.         if (null != blob)  
  58.         {  
  59.             returnValue = blob.getBytes(1, (int) blob.length());  
  60.         }  
  61.         try  
  62.         {  
  63.             if (null != returnValue)  
  64.             {  
  65.                 // 把byte转化成string  
  66.                 result = new String(returnValue, DEFAULT_CHARSET);  
  67.             }  
  68.         } catch (UnsupportedEncodingException e)  
  69.         {  
  70.             throw new RuntimeException("Blob Encoding Error!");  
  71.         }  
  72.         return result;  
  73.     }  
  74.   
  75.     @Override  
  76.     public String getNullableResult(CallableStatement cs, int columnIndex)  
  77.             throws SQLException  
  78.     {  
  79.         Blob blob = cs.getBlob(columnIndex);  
  80.         byte[] returnValue = null;  
  81.         String result = null;  
  82.         if (null != blob)  
  83.         {  
  84.             returnValue = blob.getBytes(1, (int) blob.length());  
  85.         }  
  86.         try  
  87.         {  
  88.             if (null != returnValue)  
  89.             {  
  90.                 result = new String(returnValue, DEFAULT_CHARSET);  
  91.             }  
  92.         } catch (UnsupportedEncodingException e)  
  93.         {  
  94.             throw new RuntimeException("Blob Encoding Error!");  
  95.         }  
  96.         return result;  
  97.     }  
  98.   
  99.     /** 
  100.      * @Description: 
  101.      *  
  102.      * @param arg0 
  103.      * @param arg1 
  104.      * @return 
  105.      * @throws SQLException 
  106.      *  
  107.      * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.ResultSet, 
  108.      *      int) 
  109.      *  
  110.      */  
  111.   
  112.     @Override  
  113.     public String getNullableResult(ResultSet rs, int columnName)  
  114.             throws SQLException  
  115.     {  
  116.         LOGGER.debug("enter function");  
  117.         String result = null;  
  118.         Blob blob = rs.getBlob(columnName);  
  119.         byte[] returnValue = null;  
  120.         if (null != blob)  
  121.         {  
  122.             returnValue = blob.getBytes(1, (int) blob.length());  
  123.         }  
  124.         try  
  125.         {  
  126.             // 把byte转化成string  
  127.             if (null != returnValue)  
  128.             {  
  129.                 result = new String(returnValue, DEFAULT_CHARSET);  
  130.             }  
  131.         } catch (UnsupportedEncodingException e)  
  132.         {  
  133.             throw new RuntimeException("Blob Encoding Error!");  
  134.         }  
  135.         LOGGER.debug("exit function");  
  136.         return result;  
  137.   
  138.     }  
  139. }  
resultMap中添加handler配置,如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <result property="filed11" column="filed11"  typeHandler="com.eebbk.cms.common.MyBlobTypeHandler" />  

OK!这样查出来的结果就是string类型

同理,如果对与枚举类型也可通过此方式进行转换!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值