知识大全 Hibernate+Spring搞定Clob、Blob的存取

Posted

篇首语:没有激流就称不上勇进,没有山峰则谈不上攀登。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Hibernate+Spring搞定Clob、Blob的存取相关的知识,希望对你有一定的参考价值。

Hibernate+Spring搞定Clob、Blob的存取  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  摘要 本文通过一个实例讲述如何通过Spring +Hibernate 来快捷操作数据库中的Lob字段     环境 Oracle g Srping Hibernate JUint

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

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

  /**     * @author leizhimin     * @hibernate mapping default lazy= false      * ta 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 Id;                 //机构id        private Long userId;                //用户id        private Date createDate;            //创建日期        private byte[] image;               //图片

  public static final String WORKNOTE_BLANK = ;         //未填写        public static final String WORKNOTE_FULL = ;          //已填写

  /**         * @hibernate id generator class= sequence column= BS          * ta 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          * ta 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=          * ta 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          * ta attribute= field description value= 内容          */        public String getContent()             return content;       

  public void setContent(String content)             ntent = content;       

  /**         * @hibernate property column= state not null= false length=          * ta attribute= field description value= 状态          */        public String getState()             return state;       

  public void setState(String state)             this state = state;       

  /**         * @hibernate property column= Id type= long          * ta attribute= field description value= 机构id          */        public Long getOrgId()             return Id;       

  public void setOrgId(Long Id)             Id = Id;       

  /**         * @hibernate property column= userId type= long          * ta 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          * ta 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          * ta attribute= field description value= 图片          */        public byte[] getImage()             return image;       

  public void setImage(byte[] image)             this image = image;           

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

  <?xml version= encoding= gb ?>

  <!DOCTYPE hibernate mapping PUBLIC        //Hibernate/Hibernate Mapping DTD //EN         mapping dtd >

  <hibernate mapping            default lazy= false     >        <class            name= 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=                 not null= false             >                <meta attribute= field description >天气</meta>            </property>

  <property                name= content                 type= springframework orm hibernate 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=                 not null= false             >                <meta attribute= field description >状态</meta>            </property>

  <property                name= Id                 type= long                 update= true                 insert= true                 column= Id             >                <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= springframework orm hibernate 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( ) not null             workDate timestamp             weather varchar ( char)             content clob             state varchar ( char)             Id number( )             userId number( )             createDate timestamp             image blob             primary key (BS)        );

  ment on table rc_gzrz is            工作日志 ;

  ment on column rc_gzrz BS is            标识 ;

  ment on column rc_gzrz workDate is            工作日期 ;

  ment on column rc_gzrz weather is            天气 ;

  ment on column ntent is            内容 ;

  ment on column rc_gzrz state is            状态 ;

  ment on column Id is            机构id ;

  ment on column rc_gzrz userId is            用户id ;

  ment on column rc_gzrz createDate is            创建日期 ;

  ment on column rc_gzrz image is            图片 ;

  四 创建DAO层

  /**     * Created by IntelliJ IDEA      * User: leizhimin     * Date:      * Time: : :      * To change this template use File | Settings | File Templates      */    public interface WorkNoteDAO extends CommonDAO         /**         * 根据日期查询工作日志         *         * @param workDate 工作日期         * @param userId   用户id         * @param Id    机构id         * @param sp       分页对象         * @return List         */        public List findWorkNoteByDate(Date workDate Long userId Long Id SplitPage sp);

  /**         * 根据状态查询工作日志         *         * @param state     日志状态         * @param userId    用户id         * @param Id     机构id         * @param sp        分页对象         * @return List         */        public List findWorkNoteByState(String state Long userId Long Id SplitPage sp);   

  /**     * Created by IntelliJ IDEA      * User: leizhimin     * Date:      * Time: : :      * To change this template use File | Settings | File Templates      */    public class WorkNoteDAOImpl extends CommonDAOImpl implements WorkNoteDAO        public List findWorkNoteByDate(Date workDate Long userId Long Id SplitPage sp)             return null;       

  public List findWorkNoteByState(String state Long userId Long Id SplitPage sp)             return null;           

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

  /**     * Created by IntelliJ IDEA      * User: leizhimin     * Date:      * Time: : :      * 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:      * Time: : :      * 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:      * Time: : :      * 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( );            workNote setOrgId(Long parseLong( ));            workNote setCreateDate(new Date());            byte[] b= lavasoft getBytes();            workNote setImage(b);            officeService saveWorkNote(workNote);           

  看看测试结果

        cha138/Article/program/Java/ky/201311/27913

相关参考

知识大全 spring+hibernate+jbpm整合成功

  终于搞定了在此感谢chenjin的指点  从日整合失败后这块就一直是我的心病我甚至都跑去了去发了一个帖这还是我第一次用英文问问题呢  最后的配置结果是  hibernatecfgxmljbpmcf

知识大全 Hibernate Annotation中BLOB、CLOB注解写法

HibernateAnnotation中BLOB、CLOB注解写法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一

知识大全 五步搞定Spring整合Strus

五步搞定Spring整合Strus  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  项目需要有St

知识大全 MyEclipse搞定hibernate的web应用

MyEclipse搞定hibernate的web应用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

知识大全 Spring系列第2部分:当Hibernate遇上Spring

Spring系列第2部分:当Hibernate遇上Spring  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来

知识大全 Spring整合HIbernate

Spring整合HIbernate  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Spring整

知识大全 Spring操作Hibernate更方便

Spring操作Hibernate更方便  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  软件系统

知识大全 Struts+Spring+Hibernate快速入门

Struts+Spring+Hibernate快速入门  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 Struts&Spring&Hibernate面试总结

Struts&Spring&Hibernate面试总结  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 当Spring遇到Hibernate的时候

当Spring遇到Hibernate的时候  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  )介绍