锘??xml version="1.0" encoding="utf-8" standalone="yes"?>免费成人av电影,最新国产精品,在线观看视频一区二区http://www.aygfsteel.com/jemeli/open codezh-cnWed, 18 Jun 2025 18:31:05 GMTWed, 18 Jun 2025 18:31:05 GMT60Hibernate瀛樺彇鍥劇墖紺轟緥 http://www.aygfsteel.com/jemeli/archive/2007/02/07/98560.htmljemelijemeliWed, 07 Feb 2007 04:31:00 GMThttp://www.aygfsteel.com/jemeli/archive/2007/02/07/98560.htmlhttp://www.aygfsteel.com/jemeli/comments/98560.htmlhttp://www.aygfsteel.com/jemeli/archive/2007/02/07/98560.html#Feedback0http://www.aygfsteel.com/jemeli/comments/commentRss/98560.htmlhttp://www.aygfsteel.com/jemeli/services/trackbacks/98560.html涓鑸綉绔欏湪澶勭悊鐢ㄦ埛涓婁紶鍥劇墖鏃墮氬父閲囩敤涓ょ絳栫暐錛氫竴鏄洿鎺ユ妸鍥劇墖瀛樺叆鏁版嵁搴撲腑鐨凚lob瀛楁錛涗簩鏄暟鎹簱涓彧瀛樺偍鍥劇墖鐨勫湪鏈嶅姟鍣ㄤ笂鐨勮礬寰勪俊鎭狅紝鍥劇墖瀛樻斁鍦ㄥ垎闂ㄥ埆綾葷殑鏂囦歡涓紝浣跨敤鐨勬椂鍊欎粠鏁版嵁搴撹鍙栬礬寰勪俊鎭埌欏甸潰img鍏冪礌鍗沖彲錛庡湪姝や笉璁ㄨ涓ょ鏂規(guī)鐨勪紭鍔o紝鎴戝彧鏄啓浜嗕釜hibernate鐨勪緥瀛愭潵瀹炵幇絎竴縐嶇瓥鐣ワ紟渚嬪瓙寰堢畝鍗曪紝t_user琛ㄤ富瑕佷袱涓瓧孌碉紝name鍜宲hoto錛屽叾涓璸hoto瀛楁綾誨瀷涓築lob錛庡湪姝や緥涓暟鎹簱鎴戦噰鐢╩ysql錛?oracle鐨凚lob瀛楁姣旇緝鐗規(guī)畩錛屼綘蹇呴』鑷畾涔夌被鍨嬶紝鍏蜂綋鐨勮鑷鎼滅儲錛岃繖鏂歸潰鐨勮祫鏂欏緢澶氾紟

//User.java銆銆

package com.denny_blue.hibernate;

import java.io.Serializable;
import java.sql.Blob;

public class User implements Serializable{
聽private Integer id;
聽private String name;
聽private Blob photo;
聽/**
聽 * @return the id
聽 */
聽public User(){
聽}
聽public Integer getId() {
聽聽return id;
聽}
聽/**
聽 * @param id the id to set
聽 */
聽public void setId(Integer id) {
聽聽this.id = id;
聽}
聽/**
聽 * @return the name
聽 */
聽public String getName() {
聽聽return name;
聽}
聽/**
聽 * @param name the name to set
聽 */
聽public void setName(String name) {
聽聽this.name = name;
聽}
聽/**
聽 * @return the photo
聽 */
聽public Blob getPhoto() {
聽聽return photo;
聽}
聽/**
聽 * @param photo the photo to set
聽 */
聽public void setPhoto(Blob photo) {
聽聽this.photo = photo;
聽}

}

綾籙ser鏈夛紦涓睘鎬э紝id,name,photo錛岀浉搴旂殑getter鍜宻etter鏂規(guī)硶浠ュ強涓涓棤鍙傛瀯閫犲嚱鏁幫紟搴旇娉ㄦ剰鐨勬槸photo鐨勭被鍨媕ava.sql.Blob

鐩稿簲鐨剈ser.hbm.xml搴旇濡備笅錛?/p>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
聽"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
聽"<hibernate-mapping
聽package="com.denny_blue.hibernate">

聽<class name="com.denny_blue.hibernate.User"
聽聽聽聽聽聽聽 table="t_user"
聽聽聽聽聽聽聽 dynamic-update="true"
聽聽聽聽聽聽聽 dynamic-insert="true"
聽聽聽聽聽聽聽 batch-size="3">
聽聽<id name="id"
聽聽聽聽聽 column="id"
聽聽聽聽聽 type="java.lang.Integer">
聽聽聽<generator class="native"/>
聽聽</id>
聽聽<property name="name" column="name" type="java.lang.String" lazy="true"/>
聽聽<property name="photo" column="photo" type="java.sql.Blob"/>

聽</class>

</hibernate-mapping>

瀵瑰簲鐨刪ibernate.cfg.xml閰嶇疆鏂囦歡錛屼笉鍐嶅垪鍑猴紝璇峰弬鐓ibernate鏂囨。鑷璁懼畾錛?/p>

錛極錛屽仛浜嗚繖涓姝ワ紝鎴戜滑鍐欎釜嫻嬭瘯綾繪潵榪涜鍗曞厓嫻嬭瘯錛?/p>

package com.denny_blue.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.denny_blue.hibernate.User;

import junit.framework.TestCase;

public class HibernateTest extends TestCase {
聽聽聽聽聽聽聽 private Session session;
聽protected void setUp() throws Exception {
聽聽try{
聽聽聽Configuration config=new Configuration().configure();
聽聽聽SessionFactory sf=config.buildSessionFactory();
聽聽聽session=sf.openSession();
聽聽}catch(HibernateException e){
聽聽聽e.printStackTrace();
聽聽}
聽}

聽protected void tearDown() throws Exception {
聽聽try{
聽聽聽session.close();
聽聽}catch(HibernateException e){
聽聽聽e.printStackTrace();
聽聽}
聽}

聽public void testSave()throws FileNotFoundException,IOException{
聽聽User user=new User();
聽聽user.setName("jordan");
聽聽FileInputStream in=new FileInputStream("C://test.gif");
聽聽Blob photo=Hibernate.createBlob(in);
聽聽user.setPhoto(photo);
聽聽Transaction tx=null;
聽聽try{
聽聽tx=session.beginTransaction();
聽聽session.saveOrUpdate(user);
聽聽tx.commit();
聽聽}catch(HibernateException e){
聽聽聽if(tx!=null)
聽聽聽聽tx.rollback();
聽聽聽e.printStackTrace();
聽聽}finally{
聽聽聽in.close();
聽聽}
聽}
聽public void testLoad()throws Exception{
聽聽try{
聽聽聽User user=(User)session.load(User.class, new Integer(1));
聽聽聽Blob photo=user.getPhoto();
聽聽聽InputStream in=photo.getBinaryStream();
聽聽聽FileOutputStream out=new FileOutputStream("C://out//test2.gif");
聽聽聽byte [] buf=new byte[1024];
聽聽聽int len;
聽聽聽while((len=in.read(buf))!=-1){
聽聽聽聽out.write(buf, 0, len);
聽聽聽}
聽聽聽in.close();
聽聽聽out.close();
聽聽}catch(HibernateException e){
聽聽聽e.printStackTrace();
聽聽}
聽}

}
鎴戜滑璇誨彇C鐩樼洰褰曚笅鐨則est.gif騫跺瓨鍌ㄥ埌鏁版嵁搴撲腑錛岀劧鍚庡啀鍙栧嚭鏉ュ啓鍏:/out鐩綍錛屾鏃朵綘鍙互鏌ョ湅涓嬫暟鎹〃涓璸hoto鏄劇ず涓篵lob,琛ㄧず宸茬粡鎴愬姛瀛樺叆錛庡肩殑娉ㄦ剰鐨勪唬鐮佺墖孌靛氨鏄細

FileInputStream in=new FileInputStream("C://test.gif");
聽聽Blob photo=Hibernate.createBlob(in);
鎴戜滑榪欓噷鏄粠紓佺洏涓鍙栧浘鐗囷紝瀹為檯搴旂敤涓綘鍙互鍒╃敤涓婁紶緇勪歡寰楀埌鍥劇墖鐨勶紥榪涘埗鏁版嵁嫻侊紝騫跺埄鐢℉ibernate.createBlob鏂規(guī)硶鏉ユ瀯閫犵浉搴旂殑Blob瀵硅薄錛庤屽彇鍥劇墖鍒欎嬌鐢?/p>

InputStream in=photo.getBinaryStream();

榪欏彧鏄釜綆鍗曠殑嫻嬭瘯綾伙紝濡傛灉鎴戞兂浠庢暟鎹簱涓彇鍑哄浘鐗囧茍鐜板疄鍦ㄩ〉闈笂璇ュ浣曞仛鍛紵鍏跺疄涔熷緢綆鍗曪紝鎴戜滑鍏堣鍐欎竴涓猻ervlet錛屽湪瀹冪殑service鏂規(guī)硶涓彇鍑哄浘鐗囷紝騫訛紓鐢伙紓鍒版寚瀹氶〉闈笂錛?/p>

package com.easyjf.asp.action;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.denny)blue.hibernate.User;


public class Test extends HttpServlet {

聽/**
聽 * Destruction of the servlet. <br>
聽 */
聽private Session session;
聽public void destroy() {
聽聽try{
聽聽聽session.close();
聽聽}catch(HibernateException e){
聽聽聽e.printStackTrace();
聽聽}
聽}

聽/**
聽 * Initialization of the servlet. <br>
聽 *
聽 * @throws ServletException if an error occure
聽 */
聽public void init() throws ServletException {
聽聽try{
聽聽聽Configuration config=new Configuration().configure();
聽聽聽SessionFactory sf=config.buildSessionFactory();
聽聽聽session=sf.openSession();
聽聽}catch(HibernateException e){
聽聽聽e.printStackTrace();
聽聽}
聽}
聽聽聽 public void doGet(HttpServletRequest request,HttpServletResponse response)
聽聽聽 {
聽聽聽 聽try{
聽聽聽User user=(User)session.load(User.class, new Integer(1));
聽聽聽Blob photo=user.getPhoto();
聽聽聽InputStream in=photo.getBinaryStream();
聽聽聽OutputStream out=response.getOutputStream();
聽聽聽byte [] buf=new byte[1024];
聽聽聽int len;
聽聽聽while((len=in.read(buf))!=-1){
聽聽聽聽out.write(buf, 0, len);
聽聽聽}
聽聽聽in.close();
聽聽聽out.close();
聽聽}catch(Exception e){
聽聽聽e.printStackTrace();
聽聽}
聽聽聽 }

}

閫氳繃response.getOutputStream鍙栧緱杈撳嚭嫻侊紝鍏朵粬灝變笌涓婃浠g爜涓鑷達紟servlet鍐欏ソ浜嗭紝鎬庝箞鍦ㄩ〉闈㈣皟鐢ㄥ憿錛熼偅灝辨洿綆鍗曞暒錛岀洿鎺ュ湪欏甸潰鐨刬mg鏍囩鐨剆rc灞炴т笂璋冪敤璇ervlet鍗沖彲錛屽錛?/p>

<img id="test" src="/servlet/Test"/>

綆鍗曠殑渚嬪瓙錛屽笇鏈涘鍒濆鑰呮湁甯姪錛?/p>

闄勮:濡傛灉涓嶅笇鏈涘湪servlet(鎴栬卆ction涔嬬被)涓繘琛屼繚瀛樻搷浣?甯屾湜鍦―AO榪涜姝ゆ搷浣?閭d箞InputStream鐨勫叧闂棶棰樺彲浠ラ氳繃hibernate鐨刬nterceptor鏈哄埗瑙e喅



jemeli 2007-02-07 12:31 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 漠河县| 永城市| 兴化市| 岫岩| 盐源县| 松滋市| 五家渠市| 商丘市| 苍溪县| 中宁县| 拜泉县| 德兴市| 沂水县| 孟州市| 南昌县| 兰西县| 寻乌县| 清水河县| 塔河县| 偏关县| 工布江达县| 平安县| 申扎县| 陆河县| 林西县| 嘉峪关市| 枣庄市| 晋中市| 昭平县| 九台市| 泸溪县| 石家庄市| 洪江市| 昭苏县| 桓台县| 唐河县| 汉源县| 哈巴河县| 阜平县| 阜新| 藁城市|