准备先把以前写的持久层及表示层框架写完再写loonframework-game包(实际上是想自己业余建站用,用现成的框架太无聊,重复发明轮子的最大意义就在于解闷……),在2005年时写过一个开头,由于自己没有整理文档,现在拿起来就觉得代码很乱,又懒于写文档,于是把一些心得类的东西整理一下,用以备忘。
在此持久层框架中,我将持久化过程分为两个松耦合模块,第一模块封装jdbc操作,隐藏Connection及相关事务,处理driver差异后执行标准CRUD并释放资源,于第二模块进行实体映射操作。
但和Spring JdbcTemplate等jdbc封装略有不同的是,我除了直接将结果集转为List和实体返回外,还引入了一个类似CachedRowSet但非继承CachedRowSet或ResultSet的结果集cache实体(没有提供诸如CachedRowSet的execute功能,原因大家都知道……PS:05年我用Hibernate还比较少,现在看来和Hierbante的ScrollableResults接口超级类似,颇感java技术殊途同归|||)。
但在此类get数据时,由于我将所有ResultSet数据无差别以object方式存储,当object为二进制对象时,为实现blob和clob接口就需要进行数据转换,将二进制对象转为blob或clob实现,为此完成代码如下。
比较hibernate的blobimpl而言(hibernate的blobimpl只有getBinaryStream()实现,因为别的对它也没用……),实现了更多的函数以供调用。
BlobImpl.java
package org.loon.framework.dao.lob;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;

import org.loon.framework.Loon;
import org.loon.framework.helper.FileHelper;


/** *//**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:二进制大对象Blob实现,用于转化二进制对象为blob实例,只提供get部分方法(虽然象征性的写了
* set实现,但没有对数据库进行操作,只是摆设……).
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company: LoonFramework
* </p>
* <p>
* License: http://www.apache.org/licenses/LICENSE-2.0
* </p>
*
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1
*/

public class BlobImpl implements Blob ...{


private byte[] _bytes = new byte[0];

private int _length = 0;


/** *//**
* 构造函数,以byte[]构建blob
*
* @param bytes
*/

public BlobImpl(byte[] bytes) ...{
init(bytes);
}


/** *//**
* 构造函数,以InputStream构建blob
*
* @param bytes
*/

public BlobImpl(InputStream input) ...{
init(FileHelper.read(input));
}


/** *//**
* 构造函数,以blob重新构建blob
*
* @param bytes
*/

public BlobImpl(Blob blob) ...{
init(blobToBytes(blob));
}


/** *//**
* 初始化byte[]
*
* @param b
*/

private void init(byte[] bytes) ...{
_bytes = bytes;
_length = _bytes.length;
}


/** *//**
* 将blob转为byte[]
*
* @param blob
* @return
*/

private byte[] blobToBytes(Blob blob) ...{
BufferedInputStream is = null;

try ...{
is = new BufferedInputStream(blob.getBinaryStream());
byte[] bytes = new byte[(int) blob.length()];
int len = bytes.length;
int offset = 0;
int read = 0;
while (offset < len

&& (read = is.read(bytes, offset, len - offset)) >= 0) ...{
offset += read;
}
return bytes;

} catch (Exception e) ...{
return null;

} finally ...{

try ...{
is.close();
is = null;

} catch (IOException e) ...{
return null;
}

}
}


/** *//**
* 获得blob中数据实际长度
*
* @return
* @throws SQLException
*/

public long length() throws SQLException ...{
return _bytes.length;
}


/** *//**
* 返回指定长度的byte[]
*
* @param pos
* @param len
* @return
* @throws SQLException
*/

public byte[] getBytes(long pos, int len) throws SQLException ...{
if (pos == 0 && len == length())
return _bytes;

try ...{
byte[] newbytes = new byte[len];
System.arraycopy(_bytes, (int) pos, newbytes, 0, len);
return newbytes;

} catch (Exception e) ...{
throw new SQLException("Inoperable scope of this array");
}
}


/** *//**
* 返回InputStream
*
* @return
* @throws SQLException
*/

public InputStream getBinaryStream() throws SQLException ...{
return new ByteArrayInputStream(_bytes);
}


/** *//**
* 获取此byte[]中start的字节位置
*
* @param pattern
* @param start
* @return
* @throws SQLException
*/

public long position(byte[] pattern, long start) throws SQLException ...{
start--;

if (start < 0) ...{
throw new SQLException("start < 0");
}

if (start >= _length) ...{
throw new SQLException("start >= max length");
}

if (pattern == null) ...{
throw new SQLException("pattern == null");
}

if (pattern.length == 0 || _length == 0 || pattern.length > _length) ...{
return -1;
}
int limit = (int) _length - pattern.length;

for (int i = (int) start; i <= limit; i++) ...{
int p;

for (p = 0; p < pattern.length && _bytes[i + p] == pattern[p]; p++) ...{

if (p == pattern.length) ...{
return i + 1;
}
}
}
return -1;
}


/** *//**
* 获取指定的blob中start的字节位置
*
* @param pattern
* @param start
* @return
* @throws SQLException
*/

public long position(Blob pattern, long start) throws SQLException ...{
return position(blobToBytes(pattern), start);
}


/** *//**
* 不支持操作异常抛出
*
*/

void nonsupport() ...{
throw new UnsupportedOperationException("This method is not supported!");
}


/** *//**
* 释放Blob对象资源
*
* @throws SQLException
*/

public void free() throws SQLException ...{
_bytes = new byte[0];
_length = 0;
}


/** *//**
* 返回指定长度部分的InputStream,并返回InputStream
*
* @param pos
* @param len
* @return
* @throws SQLException
*/

![]()