ibatis 操作大对象 blob

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

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

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

package com.jinzy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.SQLException;

import org.springframework.util.FileCopyUtils;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class BlobClassTest {

	static SqlMapClient sqlMapper = null;
	/**
	 * @param args
	 * @throws SQLException
	 * @throws IOException
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Reader reader = Resources
				.getResourceAsReader("com/jinzy/maps/sqlMapConfigDB2.xml");
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
		reader.close();

		InsertEntity();
		
		BlobClass blobOut = (BlobClass) sqlMapper.queryForObject(
				"selectBlobByID", "idtxt4");

		byte[] ins = blobOut.getFile();

		File fileDir = getFileFromBytes(ins, "d:\\/4.txt");
	}
    // 2.读取出来放到不同的目录下
	public static File getFileFromBytes(byte[] b, String outputFile) {
		BufferedOutputStream stream = null;
		File file = null;
		try {
			file = new File(outputFile);
			FileOutputStream fstream = new FileOutputStream(file);
			stream = new BufferedOutputStream(fstream);
			stream.write(b);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return file;
	}
    // 1.将本地文件,存入数据库
	public static void InsertEntity() throws IOException, SQLException {
		BlobClass blob = new BlobClass();
		blob.setId("idtxt4");
		blob.setName("name4");

		File file = new File("c:\\/4.txt");

		InputStream in = new BufferedInputStream(new FileInputStream(file));

		byte[] b = FileCopyUtils.copyToByteArray(in);
		blob.setFile(b);
		sqlMapper.insert("insert", blob);
	}
}

package com.jinzy;

public class BlobClass {
	private String id = null;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public byte[] getFile() {
		return file;
	}
	public void setFile(byte[] file) {
		this.file = file;
	}
	private String name = null;
	private byte[] file = null;
	
}

<?xml version="1.0" encoding="UTF-8" ?>   
    <!DOCTYPE sqlMapConfig         
        PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"         
        "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">   
    <sqlMapConfig>   

      <transactionManager type="JDBC" commitRequired="false">   
        <dataSource type="SIMPLE">
          <property name="JDBC.Driver" value="com.ibm.db2.jcc.DB2Driver"/>   
          <property name="JDBC.ConnectionURL" value="jdbc:db2://localhost:50000/SampleDB2"/>   
          <property name="JDBC.Username" value="db2admin"/>   
          <property name="JDBC.Password" value="db2admin"/>   
        </dataSource>   
      </transactionManager>   
      <sqlMap resource="com/jinzy/maps/User.xml"/>   
      
    </sqlMapConfig>   


<typeAlias alias="blob" type="com.jinzy.BlobClass"/>  
  		
<select id="selectBlobByID" parameterClass= "String" resultClass="blob">   
	select * from "jzy"."blobTest" where id = #id#  
</select> 

<insert id="insert" parameterClass="blob">   
	insert into "jzy"."blobTest"(id,  name,  file) 
	values                      (#id#,#name#,#file#)
</insert>  






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值