读书人

spring+hibernate数据库安插图片

发布时间: 2012-10-17 10:25:46 作者: rapoo

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;}}
?

.

读书人网 >其他数据库

热点推荐