无为清净楼资源网 Design By www.qnjia.com
本文讲述了简单的手工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_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程序设计有所帮助。
标签:
hibernate程序
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 《幸福工厂》无限报错解决方法
- 交错战线原始交易所推荐角色一览
- 战锤40K星际战士2战术职业介绍|战术职业技能效果一览
- 战锤40K星际战士2突击职业介绍|突击职业技能效果一览
- [妙音金曲]群星《悲情咖啡屋》(黑胶)2CD[DTS-WAV]
- 阿兰·达瓦卓玛《A-Lan阿兰唯美歌姬》2CD[DTS-WAV]
- 【小提琴】陈立新《思乡曲》2004[FLAC+CUE]
- 《战地》新作明年初大规模测试!EA已内部测试一年
- 《GTAOL》PC版时隔多年更新反作弊!小助手宣布跑路
- EA称AI是其业务核心!能提高开发效率、节约成本
- 卫华.1990-太阳升【BMG】【WAV+CUE】
- 呼吸乐队.1992-THEBREATHING【深飞】【WAV+CUE】
- 李玟.2008-1994-2008豪华典藏精选2CD【SONY】【WAV+CUE】
- 《张学友 再现歌神的光辉岁月 梦想成真 2CD》[WAV/分轨][1.2GB]
- 《海来阿木 高音测试王》[WAV+CUE][500MB]