Hibernate的Annotation中实体BLOB CLOB类型的注解

在Hibernate Annotation中,实体BLOB、CLOB类型的注解与普通的实体属性有些不同,具体操作如下:

BLOB类型的属性声明为byte[]或者java.sql.Blob:

@Lob

@Basic(fetch=FetchType.LAZY)

@Column(name="IMGS", columnDefinition="BLOB", nullable=true)

private byte[] imgs;

public byte[] getImgs() {

return this.imgs;

}

public void setImgs(byte[] imgs) {

this.imgs= imgs;

}

//下面的例子就是用的java.sql.Blob

@Lob

@Basic(fetch=FetchType.LAZY)

@Column(name="IMGS", columnDefinition="BLOB", nullable=true)

private Blob imgs;

public Blob getImgs() {

return this.imgs;

}

public void setImgs(Blob imgs) {

this.imgs= imgs;

}

CLOB类型的属性声明为String或java.sql.Clob即可:

@Lob

@Basic(fetch = FetchType.EAGER)

@Column(name="REMARK", columnDefinition="CLOB", nullable=true)

private String remark;

public String getRemark() {

return this.remark;

}

public void setRemark(String remark) {

this.remark = remark;

}

@Lob

@Basic(fetch = FetchType.EAGER)

@Column(name="REMARK", columnDefinition="CLOB", nullable=true)

private Clob remark;

public Clob getRemark() {

return this.remark;

}

public void setRemark(Clob remark) {

this.remark = remark;

}

【注】:在Oracle数据库中byte[]和String存储的是二进制流文件,而Blob和Clob存储的是原文件,如果Clob字

符串中出现中文乱码情况,请检查数据库的编码是否支持中文。

上面是Oracle和DB2数据库的写法,如果是MySQL数据库写法如下:

@Lob

@Basic(fetch = FetchType.EAGER)

@Column(name="REMARK", columnDefinition="TEXT", nullable=true)

private String remark;

public String getRemark() {

return this.remark;

}

public void setRemark(String remark) {

this.remark = remark;

}

--------------------------------------------------------------------------------------------------

也可直接把字段属性直接定义成Blob类型。

例如插入操作:

receiptSample_edit.jsp文件的部分代码片段:

<form method="post" name="theForm" id="theForm" action="" enctype="multipart/form-data">

<td height="20" align="right">请选择图片:</td>

<td align="left">

<input type="file" name="filename" id="filename"
class="content"/>

</td>

</form>

receiptSample_edit.js文件的保存方法部分代码片段:

var imageName = $("#filename").val();

var img = imageName.lastIndexOf(".");

var imgNa = imageName.substring(img);

var imgfrom = imgNa.toLowerCase();

if (imageName != "") {

if (imgfrom != ".gif" && imgfrom != ".jpg" && imgfrom != ".bmp") {

alert("不支持的图片类型" + imgNa);

return;

}

}

$("#theForm").attr("action", "editReceiptSample.do");

$("#theForm").submit();    //注意是submit提交,不是ajax提交的

ReceiptSampleCtrl.java文件中的方法:

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.MultipartHttpServletRequest;

@RequestMapping(value = "editReceiptSample.do")

private String editReceiptSample(HttpServletRequest request, ReceiptSampleVO rsVo) throws Exception {

Map parmMap = getParmMap(request);

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)
request;

MultipartFile file = multipartRequest.getFile("filename");

receiptSampleService.saveEditReceipt(rsVo, getGfmisAllUserId(request), parmMap, file);

return "receiptsystem/receipttypemanage/receiptSample_list";

}

ReceiptSampleServiceImpl.java文件中的方法:

public void saveEditReceipt(ReceiptSampleVO rsVO, String userId, Map map, MultipartFile file) throws IOException{

ReceiptSample rs = receiptSampleDao.findUnique("select rs from ReceiptSample rs where rs.receiptSampleId = ?", new Object[]{Long.parseLong(receiptSampleId)});

if (file.getSize() != 0) {

rs.setImgContent(Hibernate.createBlob(file.getInputStream()));

rs.setImgUploadTime(new Date());

rs.setImgUploadUserId(Long.parseLong(userId));

}

receiptSampleDao.save(rs);

}

例如读取操作:

@Entity

@Table(name = "R_RECEIPT_SAMPLE", schema = "GFMIS_ALL")

public class ReceiptSample implements Serializable{

@Id

@Column(name = "RECEIPT_SAMPLE_ID")

@SequenceGenerator(name = "RECEIPT_SAMPLE_ID_GENERATOR", sequenceName = "GFMIS_ALL.SEQ_RECEIPT_SAMPLE_ID", allocationSize = 1)

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "RECEIPT_SAMPLE_ID_GENERATOR")

private Long receiptSampleId;

@Column(name = "IMG_CONTENT")

private Blob imgContent;

public Blob getImgContent() {

return imgContent;

}

public void setImgContent(Blob imgContent) {

this.imgContent = imgContent;

}

}

ReceiptSampleServiceImpl.java文件中的方法:

@Override

public Blob showImg(Long imageId, HttpServletResponse rep) throws Exception{

Blob imgCount;

imgCount = receiptSampleDao.get(imageId).getImgContent();

OutputStream output = null;

InputStream input = null;

try {

final String CONTENT_TYPE = "plain-text/txt;charset=" + "GB2312";

rep.reset();

rep.setContentType(CONTENT_TYPE);

BusinessException.throwWhenNull(imgCount,"该票据类型默认版本没有票样图片信息");

input = imgCount.getBinaryStream();

//     rep.setHeader("Content-Disposition", "attachment;filename=" + filename + "");

//     bos1.close();

output = rep.getOutputStream();

byte data[] = new byte[4096];

int size = input.read(data);

while (size != -1) {

output.write(data, 0, size);

size = input.read(data);

}

output.flush();

}finally {

try {

if (input != null)

input.close();

if (output != null)

output.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return imgCount;

}

ReceiptSampleCtrl.java文件中的方法:

@RequestMapping(value = "viewImg.do")

public String viewImg(HttpServletRequest request, HttpServletResponse rep) throws Exception {

Map parmMap = getParmMap(request);

String imgName = (String) parmMap.get("imageId");

Blob img = receiptSampleService.showImg(Long.parseLong(imgName), rep);

request.setAttribute("img", img);

return "receiptsystem/receipttypemanage/ReceiptSampleImg_show";

}

ReceiptSampleImg_show.jsp文件中的内容:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page isELIgnored="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<%@include file="/receiptcommon/commonHead.jsp" %>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<meta http-equiv="Expires" CONTENT="0"/>

<meta http-equiv="Pragma" CONTENT="no-cache"/>

</head>

<body>

<table>

<tr>

<td>

<c:choose>

<c:when test="${img != null}">

${img}

</c:when>

<c:otherwise>

没有图片

</c:otherwise>

</c:choose>

</td>

</tr>

</table>

</body>

</html>

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

原文地址:https://www.cnblogs.com/skinchqqhah/p/10351477.html

时间: 2024-08-07 03:53:25

Hibernate的Annotation中实体BLOB CLOB类型的注解的相关文章

Oracle 中LONG RAW BLOB CLOB类型介绍

说明: RAW: 未加工类型,可存储二进制数据或字节符 LONG: 可变长的字符串数据,最长2G,LONG具有VARCHAR2列的特性,可以存储长文本一个表中最多一个LONG列[不建议使用] LONG RAW: 可变长二进制数据,最长2G [不建议使用] CLOB: 字符大对象Clob 用来存储单字节的字符数据:大型文本,例如XML数据. NCLOB: 用来存储多字节的字符数据 BLOB: 用于存储二进制大对象数据:例如数码照片: BFILE: 存储在文件中的二进制数据,这个文件中的数据只能被只

grails中如何存取Clob类型数据

首先说一下CLOB.CLOB是一种数据库中的数据类型,它将字符大对象存储为数据库表某一行中的一个列值,请注意,这里说的是字符大数据,驱动程序使用 SQL locator(CLOB) 实现 Clob 对象,这意味着 CLOB 对象包含一个指向 SQL CLOB 数据的逻辑指针而不是数据本身,也就是说,在表里面,Clob类型的存储的字段,它里面存的是一个指针而不是数据本身,这在统计时非常好用,因为你不需要知道数据是什么,只需要知道有多少个数据就够了. 这里也说明一下CLOB和BLOB的区别 1.CL

Hibernate or JPA Annotation中BLOB、CLOB注解写法

BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的.而CLOB是能够直接存储字符串的. 在hibernate or JPA Annotation中.实体BLOB.CLOB类型的注解与普通的实体属性有些不同,详细操作例如以下: BLOB类型,类型声明为byte[]: private byte[] content; 注解: @Lob @Basic(fetch = FetchType.LAZY) @Column(name = "CONTENT", columnDefiniti

Hibernate操作Clob类型数据

在POJO中字符串大对象可以声明成一个java.lang.String或java.sql.Clob类型. 当程序从数据库中加载Clob类型数据时,仅仅加载了一个Clob类型的数据的逻辑指针.我们需要通过使用Clob.getCaracterStream()方法得到Clob类型的数据输入流之后才能获取大对象数据. 看下面具体代码 package dao; import java.io.BufferedReader; import java.io.IOException; import java.io

Hibernate操作Clob类型数据是怎样弄的

在POJO中字符串大对象可以声明成一个java.lang.String或java.sql.Clob类型. 当程序从数据库中加载Clob类型数据时,仅仅加载了一个Clob类型的数据的逻辑指针.我们需要通过使用Clob.getCaracterStream()方法得到Clob类型的数据输入流之后才能获取大对象数据. 看下面具体代码 package dao; import java.io.BufferedReader; import java.io.IOException; import java.io

采用struts2+hibernate实现保存文件到Oracle数据库&amp;&amp;并附下载代码【导入即可用】;数据库中保存blob类型的二进制文件

原文:采用struts2+hibernate实现保存文件到Oracle数据库&&并附下载代码[导入即可用]:数据库中保存blob类型的二进制文件 源代码下载地址:http://www.zuidaima.com/share/1550463402478592.htm

oracle中Blob和Clob类型的区别

一.oracle中Blob和Clob类型的区别BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图片.文件.音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去.而像文章或者是较长的文字,就用CLOB存储,这样对以后的查询更新存储等操作都提供很大的方便. BLOB全称为二进制大型对象(Binary Large Object).它用于存储数据

Struts+Spring+Hibernate处理Lob(Blob,Clob)

在使用struts+spring+hibernate的开发中,有些时候用户会有数据库存储文件的需求,在数据库中一般会采用Blob字段或Clob字段来存储二进制图片.流媒体或文件.现就将在实际开发中遇到的问题及解决方法告之. 一.问题需求: 1.在持久化类中字段该用什么类型?  2.在Struts中文件对应的数据类型是什么?  3.在Hibernate中字段对应的类型是什么?  4.如何在Spring中处理这些文件? 二.解决方法: 1.在数据库中这些文件最终是以字节的形式存储二进制对象,所以在持

hibernate中实体类注解

一.JPA通用策略生成器 通过annotation来映射hibernate实体的,基于annotation的hibernate主键标识为@Id, 其生成规则由@GeneratedValue设定的.这里的@id和@GeneratedValue都是JPA的标准用法, JPA提供四种标准用法,由@GeneratedValue的源代码可以明显看出. Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface GeneratedValue{ G