熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate+Spring搞定Clob、Blob的存取

2013-11-23 20:05:04  來源: Java開源技術 

  摘要本文通過一個實例講述如何通過Spring+Hibernate來快捷操作數據庫中的Lob字段
    環境OraclegSrpingHibernateJUint

  說明由於時間緊迫沒有詳細寫出思路運行一下例子就明白了

  一創建實體並添加Xdoclet的Hibernate標簽

  /**
     * @author leizhimin
     * @hibernatemapping defaultlazy=false
     * ta attribute=classdescription value=工作日志
     * @hibernateclass 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 = ;         //未填寫
        public static final String WORKNOTE_FULL = ;          //已填寫

  /**
         * @hibernateid generatorclass=sequence column=BS
         * ta attribute=fielddescription value=標識
         * @hibernategeneratorparam name=sequence value=SEQ_GW
         */
        public Long getId() {
            return id;
        }

  public void setId(Long id) {
            thisid = id;
        }

  /**
         * @hibernateproperty column=workDate notnull=false type=timestamp
         * ta attribute=fielddescription value=工作日期
         */

  public Date getWorkDate() {
            return workDate;
        }

  public void setWorkDate(Date workDate) {
            thisworkDate = workDate;
        }

  /**
         * @hibernateproperty column=weather notnull=false length=
         * ta attribute=fielddescription value=天氣
         */
        public String getWeather() {
            return weather;
        }

  public void setWeather(String weather) {
            thisweather = weather;
        }

  /**
         * @hibernateproperty column=content notnull=false type=text
         * ta attribute=fielddescription value=內容
         */
        public String getContent() {
            return content;
        }

  public void setContent(String content) {
            ntent = content;
        }

  /**
         * @hibernateproperty column=state notnull=false length=
         * ta attribute=fielddescription value=狀態
         */
        public String getState() {
            return state;
        }

  public void setState(String state) {
            thisstate = state;
        }

  /**
         * @hibernateproperty column=orgId type=long
         * ta attribute=fielddescription value=機構id
         */
        public Long getOrgId() {
            return orgId;
        }

  public void setOrgId(Long orgId) {
            Id = orgId;
        }

  /**
         * @hibernateproperty column=userId type=long
         * ta attribute=fielddescription value=用戶id
         */
        public Long getUserId() {
            return userId;
        }

  public void setUserId(Long userId) {
            thisuserId = userId;
        }

  /**
         * @hibernateproperty column=createDate notnull=false type=timestamp
         * ta attribute=fielddescription value=創建日期
         */
        public Date getCreateDate() {
            return createDate;
        }

  public void setCreateDate(Date createDate) {
            thiscreateDate = createDate;
        }

  /**
         * @hibernateproperty column=image type=blob notnull=false
         * ta attribute=fielddescription value=圖片
         */
        public byte[] getImage() {
            return image;
        }

  public void setImage(byte[] image) {
            thisimage = image;
        }
    }


    二通過XDoclet生成Mapping並修正lob映射的類型為Spring提供的類型

  <?xml version= encoding=gb?>

  <!DOCTYPE hibernatemapping PUBLIC
        //Hibernate/Hibernate Mapping DTD //EN
        mappingdtd>

  <hibernatemapping
            defaultlazy=false
    >
        <class
            name=comtopsoftoaroutinedomainofficeentityWorkNote
            table=rc_gzrz
        >
            <meta attribute=classdescription>工作日志</meta>

  <id
                name=id
                column=BS
                type=javalangLong
            >
                <meta attribute=fielddescription>標識</meta>
                <generator class=sequence>
                    <param name=sequence>SEQ_GW</param>
                  <!
                      To add non XDoclet generator parameters create a file named
                      hibernategeneratorparamsWorkNotexml
                      containing the additional parameters and place it in your merge dir
                  >
                </generator>
            </id>

  <property
                name=workDate
                type=timestamp
                update=true
                insert=true
                column=workDate
                notnull=false
            >
                <meta attribute=fielddescription>工作日期</meta>
            </property>

  <property
                name=weather
                type=javalangString
                update=true
                insert=true
                column=weather
                length=
                notnull=false
            >
                <meta attribute=fielddescription>天氣</meta>
            </property>

  <property
                name=content
                type=orgspringframeworkormhibernatesupportClobStringType
                update=true
                insert=true
                column=content
                notnull=false
            >
                <meta attribute=fielddescription>內容</meta>
            </property>

  <property
                name=state
                type=javalangString
                update=true
                insert=true
                column=state
                length=
                notnull=false
            >
                <meta attribute=fielddescription>狀態</meta>
            </property>

  <property
                name=orgId
                type=long
                update=true
                insert=true
                column=orgId
            >
                <meta attribute=fielddescription>機構id</meta>
            </property>

  <property
                name=userId
                type=long
                update=true
                insert=true
                column=userId
            >
                <meta attribute=fielddescription>用戶id</meta>
            </property>

  <property
                name=createDate
                type=timestamp
                update=true
                insert=true
                column=createDate
                notnull=false
            >
                <meta attribute=fielddescription>創建日期</meta>
            </property>

  <property
                name=image
                type=orgspringframeworkormhibernatesupportBlobByteArrayType
                update=true
                insert=true
                column=image
                notnull=false
            >
                <meta attribute=fielddescription>圖片</meta>
            </property>

  <!
                To add non XDoclet property mappings create a file named
                    hibernatepropertiesWorkNotexml
                containing the additional properties and place it in your merge dir
            >

  </class>

  </hibernatemapping>

  三通過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)
            orgId number()
            userId number()
            createDate timestamp
            image blob
            primary key (BS)
        );

  comment on table rc_gzrz is
            工作日志;

  comment on column rc_gzrzBS is
            標識;

  comment on column rc_gzrzworkDate is
            工作日期;

  comment on column rc_gzrzweather is
            天氣;

  comment on column ntent is
            內容;

  comment on column rc_gzrzstate is
            狀態;

  comment on column Id is
            機構id;

  comment on column rc_gzrzuserId is
            用戶id;

  comment on column rc_gzrzcreateDate is
            創建日期;

  comment on column rc_gzrzimage 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 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:
     * 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 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:
     * 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) {
            thisworkNoteDAO = workNoteDAO;
        }

  public void saveWorkNote(WorkNote workNote) {
            thisworkNoteDAOsaveObject(workNote);
        }

  public void updateWorkNote(WorkNote workNote) {
            thisworkNoteDAOupdateObject(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) ContextHelpergetContext()getBean(officeServiceProxy);
            WorkNote workNote=new WorkNote();
            workNotesetContent();
            workNotesetOrgId(LongparseLong());
            workNotesetCreateDate(new Date());
            byte[] b=lavasoftgetBytes();
            workNotesetImage(b);
            officeServicesaveWorkNote(workNote);
        }
    }

  看看測試結果



 

 
 
 


From:http://tw.wingwit.com/Article/program/Java/ky/201311/27913.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.