1、上傳文件通過mybatis存儲到數(shù)據(jù)庫的BLOB格式中HTML?頁面需要設(shè)置一個文件框[html]?viewplaincopy1.
??上傳操作方法:[java]?viewplaincopy1.????public?int?doUpload()?throws?Exception?{??2.??3.????????Map
2、>?param?=?new?HashMap();??4.????????ServletFileUpload?servletFileUpload?=?new?ServletFileUpload();??5.????????FileItemIterator?itr?=?servletFileUpload.getItemIterator(mSGA.getCurrentRequest());??6.????????while?(itr.hasNext())?{??7.????????????FileItemS
3、tream?fi?=?itr.next();??8.????????????if?("file".equals(fi.getFieldName()))?{??9.????????????????InputStream?is?=?fi.openStream();??10.????????????????byte[]?b?=?FileCopyUtils.copyToByteArray(is);????//FileCopyUtils???為spring下的一個工具類。??11.????????????????if?(b?==?null?
4、
5、
6、?b.length?==?0)?{?return?0;?}??12.????????????????param.put("FILE",?b);//此處所用的參數(shù)類型為?byte[]??13.????????????}??14.????????}??15.??16.//////////各種參數(shù)放入param你懂得。。。。??17.??18.//dao?為個人封裝的工具類。。。??19.??20.????????param.put("ID",?dao.getSequence("SEQ_NAME"));??1.??2.???????
7、?return?dao.insert("mapper_id",?param);??3.????}??mapper文件對應(yīng)的sql設(shè)置[html]?viewplaincopy1.??2.????INSERT?INTO?DEMO??3.????(ID,FILE)??4.????VALUES??5.????(#{ID},?#{FILE,jdbcType=BLOB})?--此處需要加上jdbcType=BLOB??6.??通過MyBat
8、is實現(xiàn)圖片數(shù)據(jù)的存儲與讀取開發(fā)環(huán)境:Mybatis:3.0.5MySQL:5.x?1.數(shù)據(jù)庫Scheme----Tablestructurefortable`user_graphic_t`--DROPTABLEIFEXISTS`user_graphic_t`;/*!40101SET@saved_cs_client=@@character_set_client*/;/*!40101SETcharacter_set_client=utf8*/;CREATETABLE`user_graphic_t`(`id`int(11)NOTNUL
9、LAUTO_INCREMENT,`graphic_data`blob,PRIMARYKEY(`id`))ENGINE=InnoDBAUTO_INCREMENT=360DEFAULTCHARSET=utf8;/*!40101SETcharacter_set_client=@saved_cs_client*/;2.Mapper文件10、column="graphic_data"property="graphicData"jdbcType="BLOB"/>id,graphic_data