一,mybaits3.2.8 操作 读写 blob, ojdbc14.jar驱动(下面的hibernate也是这个驱动)
保存步骤:
1,先保存一个空的 empty_blob() 占位;
2 在 select .... for update 更新 blob 字段. (在项目中,实体我用的 java中的 Object 对应 orcale 中的blob字段)
下面是主要的代码片段:
1.mybatis xml文件 insert保存方法, getForUpdate更新方法
<insert id="insert">
<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
SELECT sys_guid() as id from DUAL
</selectKey>
INSERT INTO tb_app(
id,
appname,
appversion,
appdesc,
appdate,
packagename,
appapplication
) VALUES (
#{id},
#{appname},
#{appversion},
#{appdesc},
#{appdate},
#{packagename},
empty_blob()
)
</insert>
<select id="getForUpdate" resultType="AppEntity">
SELECT
<include refid="tbAppColumns"/>
FROM tb_app a
WHERE a.id = #{id} for update
</select>
2,(1),java 实体对应文件 AppEntity 类 只列出属性
private String appname;//app 名字
private String appversion;//版本号
private String packagename;//包名
private String appdesc;//app描述
private Object appapplication;//app 字节文件 blob对应
private Date appdate;//上传日期
(2),java controller层中保存和获取的方法
@RequiresPermissions("app")
@RequestMapping(value = "save")
@Transactional(readOnly=false)
public String save(HttpServletResponse response,AppEntity appEntity,MultipartFile myfile,RedirectAttributes redirectAttributes){
try {
InputStream input=myfile.getInputStream();
appEntity.setAppdate(new Date());
appService.save(appEntity);
String appid=appEntity.getId();
AppEntity entity=appService.getForUpdate(appid);
BLOB blob= (BLOB) entity.getAppapplication();
OutputStream blobOut=blob.setBinaryStream(0);
if(input!=null){
byte[] b=new byte[1024];
int l=0;
while((l=input.read(b))!=-1){
blobOut.write(b, 0, l);
}
}
blobOut.flush();
blobOut.close();
redirectAttributes.addAttribute("message", "保存成功!");
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:"+Global.getAdminPath()+"/emp/tbApp/list";
}
@RequestMapping(value = "getApp/{appid}")
public void getApp(@PathVariable String appid,HttpServletResponse response){
try {
AppEntity entity=appService.get(appid);
BLOB blob= (BLOB) entity.getAppapplication();
InputStream inout= blob.getBinaryStream();
response.setHeader("Content-Disposition","attachment; filename=push.apk");
OutputStream out=response.getOutputStream();
byte[] bs=new byte[1024];
int s=0;
while((s=inout.read(bs))!=-1){
out.write(bs, 0, s);
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
二,使用的hibernate 版本较低.版本号是:3.5.5 这里只有读取操作,写入功能项目没用到所以没研究
java中的实体类 java.sql.Blob 对应 oracale中的blob字段
以下是主要代码片段:
1,实体类 AppEntity
@Entity
@Table(name="TB_APP")
public class AppEntity {
private String ID;
private String APPNAME;//app 名字
private String APPVERSION;//版本号
private String PACKAGENAME;//包名
private String APPDESC;//app描述
private Blob APPAPPLICATION;//app 字节文件
private Date appdate;
.......省略其他get set方法
@Column(name="APPAPPLICATION",columnDefinition = "BLOB")
@Lob
public Blob getAPPAPPLICATION() {
return APPAPPLICATION;
}
}
2,读取操作
用的hql查询 直接返回 AppEntity 对象;appService.download(appId) 返回AppEntity对象
InputStream is = appService.download(appId).getAPPAPPLICATION().getBinaryStream();
得到 blob的输入流;
java oracle blob 读写操作
最新推荐文章于 2024-07-03 11:49:05 发布