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>