簡單的手工hibernate程序示例
本文講述了簡單的手工hibernate程序示例。分享給大家供大家參考。具體如下:
今天學(xué)習(xí)了下hibernate,寫了個(gè)小的手工程序,總結(jié)下,
首先創(chuàng)建數(shù)據(jù)庫表:
eclipse下,新建工程。
新建數(shù)據(jù)庫表的映射,這里使用手工方式完成:
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; } }
對應(yīng)編寫映射xml文件
IncrementTester.hbm.xml
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
private
實(shí)現(xiàn)具體功能的類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(); } } }
實(shí)現(xiàn)主函數(shù):
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
增加相應(yīng)的庫,運(yùn)行后成功得到結(jié)果:
Hibernate: delete from INCREMENT_TESTR Hibernate: select max(ID) from INCREMENT_TESTR Hibernate: 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: caijie ID of IncrementTester:2 name: gufeng
希望本文所述對大家的JSP程序設(shè)計(jì)有所幫助。
相關(guān)文章
Eclipse XSD 生成枚舉類型的Schema的實(shí)例詳解
這篇文章主要介紹了Eclipse XSD 生成枚舉類型的Schema的實(shí)例詳解的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下2017-09-09JSP數(shù)據(jù)分頁導(dǎo)出下載顯示進(jìn)度條樣式
這篇文章主要介紹了JSP數(shù)據(jù)分頁導(dǎo)出下載顯示進(jìn)度條樣式的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12Eclipse開發(fā)Hibernate應(yīng)用程序
Eclipse開發(fā)Hibernate應(yīng)用程序...2006-10-10Tomcat配置https并訪問http自動(dòng)跳轉(zhuǎn)至https
這篇文章主要介紹了Tomcat配置https并訪問http自動(dòng)跳轉(zhuǎn)至https的相關(guān)資料,需要的朋友可以參考下2017-06-06jsp通過自定義標(biāo)簽庫實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
這篇文章主要介紹了jsp通過自定義標(biāo)簽庫實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法,較為詳細(xì)的分析了jsp自定義標(biāo)簽的定義、功能實(shí)現(xiàn)與使用技巧,并附帶了完整的實(shí)例代碼供讀者下載參考,需要的朋友可以參考下2015-10-10