国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

手工hibernate程序簡單示例

2024-09-05 00:18:24
字體:
來源:轉載
供稿:網友

本文主要介紹了手工hibernate程序簡單示例,小編以實例的形式為大家分析了簡單hibernate程序的實現技巧,感興趣的朋友們來看看吧。

首先創建數據庫表:

復制代碼 代碼如下:
create table increment_testr(id bigint not null, name char(10), primary key(id));

 

eclipse下,新建工程。

新建數據庫表的映射,這里使用手工方式完成:

IncrementTester.java

public class IncrementTester {  private Long id;  private String name;  public IncrementTester(){}  public IncrementTester(String name){    this.name = name;  }  public Long getId(){    return this.id;  }  private void setId(Long id){    this.id = id;  }  public String getName(){    return this.name;  }  public void setName(String name){    this.name = name;  }}

對應編寫映射xml文件

IncrementTester.hbm.xml

PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
private

實現具體功能的類BussinessService

import java.lang.reflect.*;import org.hibernate.*;import org.hibernate.cfg.*;import java.io.*;import java.sql.*;import java.util.*;public class BussinessService {  public static SessionFactory sessionFactory;  static{    try{      Configuration config = new Configuration().configure();      sessionFactory = config.buildSessionFactory();    }catch(Exception e){      e.printStackTrace();    }  }  public void findAllObjects(String className){    Session session = sessionFactory.openSession();    Transaction tx = null;    try{      tx = session.beginTransaction();      List objects = session.createQuery("from "+className).list();      for(Iterator it = objects.iterator();it.hasNext();){        Long id = new Long(0);        IncrementTester xx = (IncrementTester)it.next();        id=xx.getId();        System.out.println("ID of "+className+":"+id+" name: "+xx.getName());      }      tx.commit();    }catch(Exception e){      e.printStackTrace();    }finally{      session.close();    }  }  public void saveObject(Object object){    Session session = sessionFactory.openSession();    Transaction tx = null;    try{      tx = session.beginTransaction();      session.save(object);      tx.commit();    }catch(Exception e){      e.printStackTrace();      if(tx != null){        tx.rollback();      }    }finally{      session.close();    }  }  public void deleteAllObject(String className){    Session session = sessionFactory.openSession();    Transaction tx = null;    try{      tx = session.beginTransaction();      Query query=session.createQuery("delete from "+className);      query.executeUpdate();      tx.commit();    }catch(Exception e){      e.printStackTrace();      if(tx!=null){        tx.rollback();      }    }finally{      session.close();    }  }}

實現主函數:

public class test {  /**   * @param args   */  public static void main(String[] args) {    // TODO Auto-generated method stub    String name="IncrementTester";    BussinessService aa = new BussinessService();    aa.deleteAllObject(name);    Object o1 = null;    try {      o1 = Class.forName(name).newInstance();      ((IncrementTester)o1).setName("caijie");      aa.saveObject(o1);      o1 = Class.forName(name).newInstance();      ((IncrementTester)o1).setName("gufeng");      aa.saveObject(o1);    } catch (Exception e) {      e.printStackTrace();    }    aa.findAllObjects(name);  }}

hibernate配置文件:hibernate.cfg.xml
 
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test
root
root
true

增加相應的庫,運行后成功得到結果:

Hibernate: delete from INCREMENT_TESTRHibernate: select max(ID) from INCREMENT_TESTRHibernate: insert into INCREMENT_TESTR (NAME, ID) values (?, ?)Hibernate: insert into INCREMENT_TESTR (NAME, ID) values (?, ?)Hibernate: select incrementt0_.ID as ID0_, incrementt0_.NAME as NAME0_ from INCREMENT_TESTR incrementt0_ID of IncrementTester:1 name: caijieID of IncrementTester:2 name: gufeng以上就是手工hibernate程序簡單示例,希望本文所述對大家的JSP程序設計有所幫助。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 星座| 平泉县| 南平市| 正镶白旗| 鄂州市| 永新县| 博兴县| 宁远县| 班玛县| 台前县| 新乡市| 台江县| 黔西| 庄浪县| 辰溪县| 清河县| 江达县| 天台县| 柳州市| 开阳县| 鄢陵县| 新营市| 南雄市| 新晃| 隆林| 库尔勒市| 和政县| 屯留县| 扶风县| 新疆| 绥化市| 罗田县| 陆丰市| 云霄县| 通道| 中阳县| 德清县| 贺州市| 江安县| 潼南县| 托克逊县|