[ibatis] ibatis 操作大对象 blob

batis 操作大对象: 数据库用的是DB2

1.将本地文件,存入数据库,

2.然后读取出来放到不同的目录下

[java]  view plain copy
  1. package com.jinzy;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.io.Reader;  
  11. import java.sql.SQLException;  
  12.   
  13. import org.springframework.util.FileCopyUtils;  
  14.   
  15. import com.ibatis.common.resources.Resources;  
  16. import com.ibatis.sqlmap.client.SqlMapClient;  
  17. import com.ibatis.sqlmap.client.SqlMapClientBuilder;  
  18.   
  19. public class BlobClassTest {  
  20.   
  21.     static SqlMapClient sqlMapper = null;  
  22.     /** 
  23.      * @param args 
  24.      * @throws SQLException 
  25.      * @throws IOException 
  26.      */  
  27.     public static void main(String[] args) throws Exception {  
  28.         // TODO Auto-generated method stub  
  29.         Reader reader = Resources  
  30.                 .getResourceAsReader("com/jinzy/maps/sqlMapConfigDB2.xml");  
  31.         sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);  
  32.         reader.close();  
  33.   
  34.         InsertEntity();  
  35.           
  36.         BlobClass blobOut = (BlobClass) sqlMapper.queryForObject(  
  37.                 "selectBlobByID""idtxt4");  
  38.   
  39.         byte[] ins = blobOut.getFile();  
  40.   
  41.         File fileDir = getFileFromBytes(ins, "d:\\/4.txt");  
  42.     }  
  43.     // 2.读取出来放到不同的目录下  
  44.     public static File getFileFromBytes(byte[] b, String outputFile) {  
  45.         BufferedOutputStream stream = null;  
  46.         File file = null;  
  47.         try {  
  48.             file = new File(outputFile);  
  49.             FileOutputStream fstream = new FileOutputStream(file);  
  50.             stream = new BufferedOutputStream(fstream);  
  51.             stream.write(b);  
  52.         } catch (Exception e) {  
  53.             e.printStackTrace();  
  54.         } finally {  
  55.             if (stream != null) {  
  56.                 try {  
  57.                     stream.close();  
  58.                 } catch (IOException e1) {  
  59.                     e1.printStackTrace();  
  60.                 }  
  61.             }  
  62.         }  
  63.         return file;  
  64.     }  
  65.     // 1.将本地文件,存入数据库  
  66.     public static void InsertEntity() throws IOException, SQLException {  
  67.         BlobClass blob = new BlobClass();  
  68.         blob.setId("idtxt4");  
  69.         blob.setName("name4");  
  70.   
  71.         File file = new File("c:\\/4.txt");  
  72.   
  73.         InputStream in = new BufferedInputStream(new FileInputStream(file));  
  74.   
  75.         byte[] b = FileCopyUtils.copyToByteArray(in);  
  76.         blob.setFile(b);  
  77.         sqlMapper.insert("insert", blob);  
  78.     }  
  79. }  

[java]  view plain copy
  1. package com.jinzy;  
  2.   
  3. public class BlobClass {  
  4.     private String id = null;  
  5.     public String getId() {  
  6.         return id;  
  7.     }  
  8.     public void setId(String id) {  
  9.         this.id = id;  
  10.     }  
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.     public void setName(String name) {  
  15.         this.name = name;  
  16.     }  
  17.     public byte[] getFile() {  
  18.         return file;  
  19.     }  
  20.     public void setFile(byte[] file) {  
  21.         this.file = file;  
  22.     }  
  23.     private String name = null;  
  24.     private byte[] file = null;  
  25.       
  26. }  

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>     
  2.     <!DOCTYPE sqlMapConfig           
  3.         PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"           
  4.         "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">     
  5.     <sqlMapConfig>     
  6.   
  7.       <transactionManager type="JDBC" commitRequired="false">     
  8.         <dataSource type="SIMPLE">  
  9.           <property name="JDBC.Driver" value="com.ibm.db2.jcc.DB2Driver"/>     
  10.           <property name="JDBC.ConnectionURL" value="jdbc:db2://localhost:50000/SampleDB2"/>     
  11.           <property name="JDBC.Username" value="db2admin"/>     
  12.           <property name="JDBC.Password" value="db2admin"/>     
  13.         </dataSource>     
  14.       </transactionManager>     
  15.       <sqlMap resource="com/jinzy/maps/User.xml"/>     
  16.         
  17.     </sqlMapConfig>     


[html]  view plain copy
  1. <typeAlias alias="blob" type="com.jinzy.BlobClass"/>    
  2.           
  3. <select id="selectBlobByID" parameterClass"String" resultClass="blob">     
  4.     select * from "jzy"."blobTest" where id = #id#    
  5. </select>   
  6.   
  7. <insert id="insert" parameterClass="blob">     
  8.     insert into "jzy"."blobTest"(id,  name,  file)   
  9.     values                      (#id#,#name#,#file#)  
  10. </insert>    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值