在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() {
}
public void setImgs(byte[] imgs) {
}
//下面的例子就是用的java.sql.Blob
@Lob
@Basic(fetch=FetchType.LAZY)
@Column(name="IMGS", columnDefinition="
BLOB", nullable=true)
private
Blob
imgs;
public Blob getImgs() {
}
public void setImgs(Blob imgs) {
}
CLOB类型
的属性声明为
String或
java.sql.Clob即可:
@Lob
@Basic(fetch = FetchType.EAGER)
@Column(name="REMARK", columnDefinition="
CLOB", nullable=true)
private
String remark;
public String getRemark() {
}
public void setRemark(String remark) {
}
@Lob
@Basic(fetch = FetchType.EAGER)
@Column(name="REMARK", columnDefinition="
CLOB", nullable=true)
private
Clob
remark;
public Clob getRemark() {
}
public void setRemark(Clob 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() {
}
public void setRemark(String remark) {
}
--------------------------------------------------------------------------------------------------
也可直接把字段属性直接定义成Blob类型。
例如插入操作:
receiptSample_edit.
jsp文件的部分代码片段:
<form method="post" name="theForm" id="theForm" action="" enctype="multipart/form-data">
<input type="file" name="
filename" id="
filename" class="content"/>
</form>
receiptSample_edit.
js文件的保存方法部分代码片段:
var imageName = $("#
filename").val();
var img = imageName.lastIndexOf(".");
var imgNa = imageName.substring(img);
var imgfrom = imgNa.toLowerCase();
if (imageName != "") {
}
$("#theForm").attr("action", "editReceiptSample.do");
$("#theForm").submit();
//注意是submit提交,不是ajax提交的
ReceiptSampleCtrl
.
java
文件中的方法:
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.
MultipartHttpServletRequ est;
@RequestMapping(value = "editReceiptSample.do")
private String editReceiptSample(HttpServletRequest request, ReceiptSampleVO rsVo) throws Exception {
}
ReceiptSampleServiceImpl .
java文件中的方法:
public void saveEditReceipt(ReceiptSampleVO rsVO, String userId, Map map,
MultipartFile file) throws IOException{
}
例如读取操作:
@Entity
@Table(name = "R_RECEIPT_SAMPLE", schema = "GFMIS_ALL")
public class
ReceiptSample implements Serializable{
}
ReceiptSampleServiceImpl .
java文件中的方法:
@Override
public Blob showImg(Long imageId, HttpServletResponse rep) throws Exception{
// rep.setHeader("Content-Disposition", "attachment;filename=" + filename + "");
// bos1.close();
}
ReceiptSampleCtrl.java文件中的方法:
@RequestMapping(value = "viewImg.do")
public String viewImg(HttpServletRequest request, HttpServletResponse rep) throws Exception {
}
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>
</head>
<body>
<table>
</table>
</body>
</html>