/*应用Hibernate增删改查*/

import org.hibernate.cfg.Configuration;

import org.hibernate.SessionFactory;

import org.hibernate.Session;

import org.hibernate.Transaction;

class 

{

 public static void main(String[] args)

 {

  /*1、读取Hibernate配置文件*/

  Configuration conf=new Configuration().configure();

  /*2、Hibernate操做通过Session来完成,Session由SessionFactory来管理*/

  SessionFactory sf=conf.buildSessionFactory();

  /*3、打开Session*/

  org.hibernate.Session session=sf.openSession();

  /*4、插入数据*/

  Customer cus=new Customer("222","jembai","69063069",4400);

  /*5、启动事务*/

  Transaction tran=session.beginTransaction();

  session.save(cus);

  tran.commit();

  /*查询数据*/

  session.load(cus,"111");

  System.out.println(cus.getCaccount());

  System.out.println(cus.getPassword());

  System.out.println(cus.getCbalance());

  System.out.println(cus.getUsername());

  /*修改数据*/

  session.load(cus,"222");

  cus.setCbalance(cus.getCbalance()+1000);

  Transaction tran=session.beginTransaction();

  session.update(cus);

  tran.commit();

  /*删除数据*/

  session.load(cus,"3333");

  Transaction tran=session.beginTransaction();

  session.delete(cus);

  tran.commit();

  

  session.close();


  /*session的打开与关闭的优化*/
  util.HibernateSessionFactory.getSession();
  util.HibernateSessionFactory.closeSession();

 }

}