/** * */ package hibernate.test; import java.util.Date; /** * @author huang * Oct 18, 2013 3:01:08 PM User.java */ public class UserTest { private Integer id; private String name; private String password; private Date createTime; private Date expireTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getExpireTime() { return expireTime; } public void setExpireTime(Date expireTime) { this.expireTime = expireTime; } }
package hibernate.test; import java.util.Date; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Client { public static void main(String[] args) { query(10); //读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); // SchemaExport export = new SchemaExport(cfg); // export.create(true, true); //建立SessionFactory SessionFactory factory = cfg.buildSessionFactory(); //取得session Session session = null; try { session = factory.openSession(); //开启事务 session.beginTransaction(); UserTest user = new UserTest(); user.setName("张三121"); user.setPassword("121123"); user.setCreateTime(new Date()); user.setExpireTime(new Date()); //保存User对象 session.save(user); //提交事务 session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); }finally { if (session != null) { if (session.isOpen()) { //关闭session session.close(); } } } } static void query(int i){ Session s=null; try{ s = HibernateUtil.currentSession(); //from后面是对象,不是表名 String hql="from UserTest as ut where ut.id=:id";//使用命名参数,推荐使用,易读。 Query query=s.createQuery(hql); query.setInteger("id", i); List<UserTest> list=query.list(); for(UserTest ut:list){ System.out.println(ut.getName()); } }finally{ if(s!=null) s.close(); } } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="hibernate.test.UserTest"> <id name="id" column="id" type="java.lang.Integer"> <generator class="sequence"> <param name="sequence">SEQ_SO</param> </generator> </id> <property name="name" /> <property name="password" /> <property name="createTime" /> <property name="expireTime" /> </class> </hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="hibernate.connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="hibernate.connection.url"> jdbc:oracle:thin:@10.101.2.3:1521:develope </property> <property name="hibernate.connection.username">dpcds_newnew</property> <property name="hibernate.connection.password">dpcds_newnew</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <mapping resource="test.hbm.xml" /> </session-factory> </hibernate-configuration>
操作数据库太方便了,记录一下,目前只有单表。
package hibernate.test; import org.hibernate.*; import org.hibernate.cfg.*; import java.io.File; /** * * <p> * Title: WMDS * </p> * <p> * Description: * </p> * <p> * Copyright: Copyright (c) 2005 * </p> * <p> * Company: ECLINK * </p> * * @author * @version 1.0 */ public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory // System.out.println("-----------------------------"); // File f = new File( sessionFactory = new Configuration().configure() .buildSessionFactory(); } catch (Throwable ex) { ex.printStackTrace(); // log.error("Initial SessionFactory creation failed.", ex); throw new ExceptionInInitializerError(ex); } } public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() { Session s = (Session) session.get(); // Open a new Session, if this Thread has none yet if (s == null) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() { Session s = (Session) session.get(); if (s != null) s.close(); session.set(null); } public static void save(Object object) throws Exception { // 开始 获得hibernate环境 sessioin Session session = HibernateUtil.currentSession(); // 开始 获得hibernate环境 transaction 事物 Transaction tx = session.beginTransaction(); try { session.save(object); // 事物提交 tx.commit(); // 关闭环境 HibernateUtil.closeSession(); } catch (Exception ex) { tx.rollback(); HibernateUtil.closeSession(); throw ex; } } public static Object load(Class class0, String id) throws Exception { // 开始 获得hibernate环境 sessioin Session session = HibernateUtil.currentSession(); // 开始 获得hibernate环境 transaction 事物 Transaction tx = session.beginTransaction(); try { Object o = session.load(class0, id); // 事物提交 tx.commit(); // 关闭环境 HibernateUtil.closeSession(); return o; } catch (Exception ex) { tx.rollback(); HibernateUtil.closeSession(); throw ex; } } public static void delete(Object object) throws Exception { // 开始 获得hibernate环境 sessioin Session session = HibernateUtil.currentSession(); // 开始 获得hibernate环境 transaction 事物 Transaction tx = session.beginTransaction(); try { session.delete(object); // 事物提交 tx.commit(); // 关闭环境 HibernateUtil.closeSession(); } catch (Exception ex) { tx.rollback(); HibernateUtil.closeSession(); throw ex; } } public static void update(Object object) throws Exception { // 开始 获得hibernate环境 sessioin Session session = HibernateUtil.currentSession(); // 开始 获得hibernate环境 transaction 事物 Transaction tx = session.beginTransaction(); try { session.update(object); // 事物提交 tx.commit(); // 关闭环境 HibernateUtil.closeSession(); } catch (Exception ex) { tx.rollback(); HibernateUtil.closeSession(); throw ex; } } public static void saveOrUpdate(Object object) throws Exception { // 开始 获得hibernate环境 sessioin Session session = HibernateUtil.currentSession(); // 开始 获得hibernate环境 transaction 事物 Transaction tx = session.beginTransaction(); try { session.saveOrUpdate(object); // 事物提交 tx.commit(); // 关闭环境 HibernateUtil.closeSession(); } catch (Exception ex) { tx.rollback(); HibernateUtil.closeSession(); throw ex; } } /** * 调用例子 * * @param arg * String[] */ public static void main(String[] arg) { try { } catch (Exception e) { e.printStackTrace(); } } }
相关推荐
Hibernate应用及其详解
Hibernate应用例子many-to-many
《Hibernate应用开发完全手册》中的第2章到第4章的所有的源代码,其他的源代码在本人的资源里都有,希望读者仔细的找
hibernate应用开发完全手册(源码)
Struts2、Spring和Hibernate应用实例
Eclipse是目前非常流行的开发平台,开放扩展的架构让很多程序员找到了自己个性化的工作环境,Hibernate也...本文主要介绍如何利用Hibernate Syn插件在Eclipse 3.0.1中快速的开发Hibernate应用程序,提高我们的工作效率
hibernate应用下载,hibernate应用下载hibernate应用下载hibernate应用下载
在文件中有关于hibernate的应用代码和sql语句,用的是sql server,实现了数据的查找和保存功能,与大家分享一下
《Hibernate应用开发完全手册》附有配套光盘,提供了书中所有实例、范例和案例程序的全部源代码。Hibernate的环境搭建及使用Hibernate时必须掌握的基本技术等基础知识;在Hibernate中映射关联关系、事务应用、检索...
Hibernate应用实例开发 Hibernate功能介绍
介绍如何用Eclipse 开发Hibernate 应用程序,简单易懂,图文解说。 利用Eclipse 开发Hibernate 应用程序.rar
Hibernate应用开发完全手册,希望对大家有用。
Hibernate应用 课堂.ppt 网站开发设计必备的基础
MyEclipse开发Struts+Hibernate应用,图文示例
Hibernate概述 Hibernate应用基础 Hibernate实战经典
4.1 Hibernate概述 4.2 Hibernate应用基础 4.3 Hibernate关系映射 4.4 Hibernate高级功能 4.5 Hibernate与Struts 2整合应用
开发Spring.Struts.Hibernate应用