首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

spring+hibernate,数据库安插图片

2012-10-17 
spring+hibernate,数据库插入图片主要就三個地方需要配置:一hbm中,property namephoto1 typebinar

spring+hibernate,数据库插入图片

主要就三個地方需要配置:

<一>hbm中,

<property name="photo1" type="binary">    <column name="photo1" not-null="false" /></property>

<二>javabean中,

private byte[] photo1 ;public void setPhoto1(byte[] photo1) {this.photo1 = photo1;}public byte[] getPhoto1() {return photo1;}

?<三>spring的applicationContext.xml中,

<!--声明一个处理句柄 Blob-->  <bean id="lobHandler" lazy-init="true" />
?

這樣再加上把File转成byte[]的方法,就ok了

//傳入的File 轉成byte[]public byte[] inputToByte( File f ){try {FileInputStream fis = new FileInputStream( f );byte[] buffer = new byte[1024];  int read;  ByteArrayOutputStream byteArray = new ByteArrayOutputStream();  while((read = fis.read(buffer))>0)  {  byteArray.write(buffer,0,read);  byteArray.flush();  }  byte[]   bt   =   byteArray.toByteArray();byteArray.close();return bt ;} catch (FileNotFoundException e) {e.printStackTrace();return null;} catch (IOException e) {e.printStackTrace();return null;}}
?

.

热点排行