`

Hibernate+Spring彻底搞定Clob、Blob的存取

阅读更多
Hibernate+Spring彻底搞定Clob、Blob的存取


摘要:本文通过一个实例讲述如何通过Spring2+Hibernate3来快捷操作数据库中的Lob字段。
环境:Oracle10g、Srping2、Hibernate3、JUint4

说明:由于时间紧迫,没有详细写出思路。运行一下例子就明白了。


一、创建实体并添加Xdoclet的Hibernate标签

/**
 * @author leizhimin
 * @hibernate.mapping default-lazy="false"
 * @hibernate.meta attribute="class-description" value="工作日志"
 * @hibernate.class table="rc_gzrz"
 */
public class WorkNote {
    private Long id;                    //标识
    private Date workDate;             //日期
    private String weather;             //天气
    private String content;             //日志内容(Clob)
    private String state;               //日志状态
    private Long orgId;                 //机构id
    private Long userId;                //用户id
    private Date createDate;            //创建日期
    private byte[] image;               //图片
 
    public static final String WORKNOTE_BLANK = "00";         //未填写
    public static final String WORKNOTE_FULL = "11";          //已填写
 
    /**
     * @hibernate.id generator-class="sequence" column="BS"
     * @hibernate.meta attribute="field-description" value="标识"
     * @hibernate.generator-param name="sequence" value="SEQ_GW"
     */
    public Long getId() {
        return id;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
 
    /**
     * @hibernate.property column="workDate" not-null="false" type="timestamp"
     * @hibernate.meta attribute="field-description" value="工作日期"
     */
 
    public Date getWorkDate() {
        return workDate;
    }
 
    public void setWorkDate(Date workDate) {
        this.workDate = workDate;
    }
 
    /**
     * @hibernate.property column="weather" not-null="false" length="24"
     * @hibernate.meta attribute="field-description" value="天气"
     */
    public String getWeather() {
        return weather;
    }
 
    public void setWeather(String weather) {
        this.weather = weather;
    }
 
    /**
     * @hibernate.property column="content" not-null="false" type="text"
     * @hibernate.meta attribute="field-description" value="内容"
     */
    public String getContent() {
        return content;
    }
 
    public void setContent(String content) {
        this.content = content;
    }
 
    /**
     * @hibernate.property column="state" not-null="false" length="2"
     * @hibernate.meta attribute="field-description" value="状态"
     */
    public String getState() {
        return state;
    }
 
    public void setState(String state) {
        this.state = state;
    }
 
    /**
     * @hibernate.property column="orgId" type="long"
     * @hibernate.meta attribute="field-description" value="机构id"
     */
    public Long getOrgId() {
        return orgId;
    }
 
    public void setOrgId(Long orgId) {
        this.orgId = orgId;
    }
 
    /**
     * @hibernate.property column="userId" type="long"
     * @hibernate.meta attribute="field-description" value="用户id"
     */
    public Long getUserId() {
        return userId;
    }
 
    public void setUserId(Long userId) {
        this.userId = userId;
    }
 
    /**
     * @hibernate.property column="createDate" not-null="false" type="timestamp"
     * @hibernate.meta attribute="field-description" value="创建日期"
     */
    public Date getCreateDate() {
        return createDate;
    }
 
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
 
    /**
     * @hibernate.property column="image" type="blob" not-null="false"
     * @hibernate.meta attribute="field-description" value="图片"
     */
    public byte[] getImage() {
        return image;
    }
 
    public void setImage(byte[] image) {
        this.image = image;
    }
}


二、通过XDoclet生成Mapping,并修正lob映射的类型为Spring提供的类型

<?xml version="1.0" encoding="gb2312"?>
 
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "[url]http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd[/url]">
 
<hibernate-mapping
        default-lazy="false"
>
    <class
        name="com.topsoft.oa.routine.domain.office.entity.WorkNote"
        table="rc_gzrz"
    >
        <meta attribute="class-description">工作日志</meta>
 
        <id
            name="id"
            column="BS"
            type="java.lang.Long"
        >
            <meta attribute="field-description">标识</meta>
            <generator class="sequence">
                <param name="sequence">SEQ_GW</param>
              <!--  
                  To add non XDoclet generator parameters, create a file named 
                  hibernate-generator-params-WorkNote.xml 
                  containing the additional parameters and place it in your merge dir. 
              --> 
            </generator>
        </id>
 
        <property
            name="workDate"
            type="timestamp"
            update="true"
            insert="true"
            column="workDate"
            not-null="false"
        >
            <meta attribute="field-description">工作日期</meta>
        </property>
 
        <property
            name="weather"
            type="java.lang.String"
            update="true"
            insert="true"
            column="weather"
            length="24"
            not-null="false"
        >
            <meta attribute="field-description">天气</meta>
        </property>
 
        <property
            name="content"
            type="org.springframework.orm.hibernate3.support.ClobStringType"
            update="true"
            insert="true"
            column="content"
            not-null="false"
        >
            <meta attribute="field-description">内容</meta>
        </property>
 
        <property
            name="state"
            type="java.lang.String"
            update="true"
            insert="true"
            column="state"
            length="2"
            not-null="false"
        >
            <meta attribute="field-description">状态</meta>
        </property>
 
        <property
            name="orgId"
            type="long"
            update="true"
            insert="true"
            column="orgId"
        >
            <meta attribute="field-description">机构id</meta>
        </property>
 
        <property
            name="userId"
            type="long"
            update="true"
            insert="true"
            column="userId"
        >
            <meta attribute="field-description">用户id</meta>
        </property>
 
        <property
            name="createDate"
            type="timestamp"
            update="true"
            insert="true"
            column="createDate"
            not-null="false"
        >
            <meta attribute="field-description">创建日期</meta>
        </property>
 
        <property
            name="image"
            type="org.springframework.orm.hibernate3.support.BlobByteArrayType"
            update="true"
            insert="true"
            column="image"
            not-null="false"
        >
            <meta attribute="field-description">图片</meta>
        </property>
 
        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-WorkNote.xml
            containing the additional properties and place it in your merge dir.
        -->
 
    </class>
 
</hibernate-mapping>




三、通过Mapping 用XDoclet生成数据库(Oracle)脚本,并建表

   drop table rc_gzrz cascade constraints;
 
 
    create table rc_gzrz (
        BS number(19,0) not null,
        workDate timestamp,
        weather varchar2(24 char),
        content clob,
        state varchar2(2 char),
        orgId number(19,0),
        userId number(19,0),
        createDate timestamp,
        image blob,
        primary key (BS)
    );
 
    comment on table rc_gzrz is
        '工作日志';
 
    comment on column rc_gzrz.BS is
        '标识';
 
    comment on column rc_gzrz.workDate is
        '工作日期';
 
    comment on column rc_gzrz.weather is
        '天气';
 
    comment on column rc_gzrz.content is
        '内容';
 
    comment on column rc_gzrz.state is
        '状态';
 
    comment on column rc_gzrz.orgId is
        '机构id';
 
    comment on column rc_gzrz.userId is
        '用户id';
 
    comment on column rc_gzrz.createDate is
        '创建日期';
 
    comment on column rc_gzrz.image is
        '图片';




四、创建DAO层


/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-11-16
 * Time: 10:55:50
 * To change this template use File | Settings | File Templates.
 */
public interface WorkNoteDAO extends CommonDAO {
    /**
     * 根据日期查询工作日志
     *
     * @param workDate 工作日期
     * @param userId   用户id
     * @param orgId    机构id
     * @param sp       分页对象
     * @return List
     */
    public List findWorkNoteByDate(Date workDate, Long userId, Long orgId, SplitPage sp);
 
    /**
     * 根据状态查询工作日志
     *
     * @param state     日志状态
     * @param userId    用户id
     * @param orgId     机构id
     * @param sp        分页对象
     * @return List
     */
    public List findWorkNoteByState(String state, Long userId, Long orgId, SplitPage sp);
}
 
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-11-16
 * Time: 10:56:00
 * To change this template use File | Settings | File Templates.
 */
public class WorkNoteDAOImpl extends CommonDAOImpl implements WorkNoteDAO{
    public List findWorkNoteByDate(Date workDate, Long userId, Long orgId, SplitPage sp) {
        return null;
    }
 
    public List findWorkNoteByState(String state, Long userId, Long orgId, SplitPage sp) {
        return null;  
    }
}



五、创建带JTA事务控制的业务service层

/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-11-16
 * Time: 16:43:57
 * To change this template use File | Settings | File Templates.
 */
public interface OfficeService {
 
    public void saveWorkNote(WorkNote workNote);
 
    public void updateWorkNote(WorkNote workNote);
}
 
 
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-11-16
 * Time: 16:45:54
 * To change this template use File | Settings | File Templates.
 */
public class OfficeServiceImpl implements OfficeService{
    private WorkNoteDAO workNoteDAO;
 
    public WorkNoteDAO getWorkNoteDAO() {
        return workNoteDAO;
    }
 
    public void setWorkNoteDAO(WorkNoteDAO workNoteDAO) {
        this.workNoteDAO = workNoteDAO;
    }
 
    public void saveWorkNote(WorkNote workNote) {
        this.workNoteDAO.saveObject(workNote);
    }
 
    public void updateWorkNote(WorkNote workNote) {
        this.workNoteDAO.updateObject(workNote);
    }
}



六、书写单元测试,并运行
/**
 * Created by IntelliJ IDEA.
 * User: leizhimin
 * Date: 2007-11-16
 * Time: 16:49:17
 * To change this template use File | Settings | File Templates.
 */
public class TestOffice extends TestCase {
    public void test_worknote_save(){
        OfficeService officeService = (OfficeService) ContextHelper.getContext().getBean("officeServiceProxy");
        WorkNote workNote=new WorkNote();
        workNote.setContent("[url]http://lavasoft.blog.51cto.com/[/url]");
        workNote.setOrgId(Long.parseLong("999"));
        workNote.setCreateDate(new Date());
        byte[] b="lavasoft".getBytes();
        workNote.setImage(b);
        officeService.saveWorkNote(workNote);
    }
}


看看测试结果:




看到了吧,存进去了,各位周末愉快~~


分享到:
评论

相关推荐

    spring+hbernate对clob、blob字段的处理

    一个完整的工程,主要功能为:spring+hbernate对clob、blob字段的处理 包括:数据脚本、典型的SSH框架处理,以及spring、hibernate数据批量保存等功能源码、所用到的lib包 数据环境为:oracle 10G 开发:Mycelipse5.1...

    spring+hibernate 解决大字段(clob)

    ### Spring与Hibernate处理大字段(CLOB/BLOB) 在企业级应用开发中,经常会遇到需要存储大量文本或二进制数据的情况,这时就需要用到数据库的大字段类型如CLOB(Character Large Object)和BLOB(Binary Large ...

    DELPHI的CLOB和BLOB存取处理.zip_DELPHI的CLOB和BLOB存取处理_hallzgz

    本篇文章将深入探讨DELPHI中如何有效地进行CLOB和BLOB的存取处理。 CLOB主要用来存储大文本数据,如长篇文章、XML文档或JSON字符串,而BLOB则用于存储大量的二进制数据,如图像、音频、视频文件或者任何非文本的...

    Hibernate操作Oarcle中Clob、Blob字段小结

    在Java的持久化框架Hibernate中,处理大数据类型如Oracle数据库中的Clob(Character Large Object)和Blob(Binary Large Object)字段是一项重要的任务。Clob通常用于存储大量的文本数据,而Blob则适用于二进制...

    CLOB、BLOB___CLOB与BLOB的区别

    CLOB、BLOB 与 CLOB 与 BLOB 的区别 CLOB(Character Large OBject)是一种数据库类型,用于存储大型字符对象。它是 SQL 类型,用于存储字符大对象(Character Large Object),将字符大对象存储为数据库表某一行中...

    利用spring的jdbcTemplate处理blob、clob

    spring 中对大数据的处理,包括clob,blob的数据。比之jdbc下简便很多。

    修改clob blob 字段

    修改clob blob 字段 修改clob blob 字段 修改clob blob 字段

    移动ssh项目(struts+spring+hibernate+oracle).zip

    它支持复杂的数据类型,如BLOB、CLOB,以及高级的并发控制和数据安全特性。在SSH项目中,Oracle作为后端数据库,存储和管理应用程序的数据。 这个压缩包中的项目可能包含了以下部分: - src目录:包含项目的源代码...

    spring+hibernate操作oracle的clob字段

    Spring和Hibernate会自动处理CLOB的存取,无需额外的特殊处理。例如,如果你有一个更新方法,可以如下所示: ```java public void updateEntity(Entity entity) { assessRegDao.update(entity); } ``` 其中...

    关于在Hibernate中对于Clob,Blob字段的处理方法

    在Java的持久化框架Hibernate中,处理大数据类型如Clob(Character Large Object)和Blob(Binary Large Object)是一项常见的任务。这些数据类型通常用于存储大量的文本数据(Clob)和二进制数据(Blob),例如长篇...

    Mybatis 处理 CLOB、BLOB 类型数据

    Mybatis 处理 CLOB、BLOB 类型数据 MyBatis 处理 CLOB、BLOB 类型数据是指在使用 MyBatis 框架时,如何正确地处理大字段类型的数据。CLOB(Character Large OBject)和 BLOB(Binary Large OBject)都是大字段类型...

    hibernate保存blob,clob对象

    在Java的持久化框架Hibernate中,Blob和Clob对象是用来处理大数据类型的,它们分别对应数据库中的BLOB(Binary Large Object)和CLOB(Character Large Object)。这篇文章将详细讲解如何在Hibernate中保存这两种...

    Hibernate对BLOB CLOB操作

    在Java的持久化框架Hibernate中,BLOB和CLOB是用来处理大数据对象(Binary Large Object和Character Large Object)的。这两个类型常用于存储图像、视频、大文本等数据,因为它们可以容纳超过数据库标准列大小限制的...

    hibernate存取oracle的clob

    hibernate存取oracle的clob

    Oracle导出Clob,Blob工具

    Oracle数据库在处理大对象(LOB)类型,如Clob(Character Large Object)和Blob(Binary Large Object)时,有时需要专门的工具来进行高效且安全的数据导出。这些字段通常存储大量的文本或二进制数据,比如长篇文档...

    Struts2 Hibernate存取Oracle数据库中的BLOB数据.pdf

    - BLOB(二进制大对象):用于存储大型二进制数据,如影像、图片等,其长度可达4GB,支持随机存取。 - BFILE:虽然用于存储大型二进制数据,但数据实际存储在文件系统中,数据库中仅存储指向文件的指针,Oracle对...

Global site tag (gtag.js) - Google Analytics