java

位置:IT落伍者 >> java >> 浏览文章

Hibernate+Spring搞定Clob、Blob的存取


发布日期:2020年08月25日
 
Hibernate+Spring搞定Clob、Blob的存取

摘要本文通过一个实例讲述如何通过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);

}

}

看看测试结果

               

上一篇:Struts2 框架使用实例分析

下一篇:实例说明如何集成Spring和Struts