Hibernate中对于数据的(增删改查)操作的一个例子的代码

来源:互联网 发布:win10 cmd java 编辑:程序博客网 时间:2024/05/20 13:18
package com.xiami.examples;import java.util.List;import org.hibernate.Criteria;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.criterion.Criterion;import org.hibernate.criterion.Restrictions;public class HibernateTest {public static void main(String args[]){//Configuration config = new Configuration();//config.configure();//SessionFactory sessionFactory = config.buildSessionFactory();//Session session = sessionFactory.getCurrentSession();//Transaction tx = session.beginTransaction();//session对象的sava()方法保持一条记录//Guestbook gb = new Guestbook();//gb.setName("Kalision");//gb.setPhone("13854135552");//gb.setContent("我是Kalision.");//gb.setTitle("大家好,");//gb.setEmail("zhouchuanshun@foxmail.com");//gb.setCreatedTime((new Date()).toString());//session.save(gb);//tx.commit();//sessionFactory.close();//session对象的get()方法得到一条记录//Guestbook gb = (Guestbook) session.get(Guestbook.class, new Integer(3));//tx.commit();//System.out.println(gb.getName());//session对象的load()方法得到一条记录//Guestbook gb = (Guestbook) session.load(Guestbook.class,new Integer(3));//tx.commit();//System.out.println(gb.getName());//session对象的update()方法更新一条记录//Configuration config = new Configuration().configure();//SessionFactory sessionFactory = config.buildSessionFactory();//Session session = sessionFactory.getCurrentSession();//Transaction tx = session.beginTransaction();//Guestbook gb = (Guestbook) session.get(Guestbook.class, new Integer(3));//tx.commit();////gb.setName("xiami");//session = sessionFactory.getCurrentSession();//tx = session.beginTransaction();//session.update(gb);//tx.commit();////更新以后,在得到更新后的名字//session = sessionFactory.getCurrentSession();//tx = session.beginTransaction();//gb = (Guestbook) session.get(Guestbook.class, new Integer(3));//tx.commit();//System.out.println(gb.getName());//session对象的delete()方法删除一条记录//Configuration config = new Configuration().configure();//SessionFactory sessionFactory = config.buildSessionFactory();//Session session = sessionFactory.getCurrentSession();//Transaction tx = session.beginTransaction();//Guestbook gb = (Guestbook) session.get(Guestbook.class, new Integer(5));//session.delete(gb);//tx.commit();//query接口  使用Query类型的对象可以方便进行查询数据库的数据//Configuration config = new Configuration().configure();//SessionFactory sessionFactory = config.buildSessionFactory();//Session session = sessionFactory.getCurrentSession();//Transaction tx = session.beginTransaction();//Query query = session.createQuery("from Guestbook");//List list = query.list();//tx.commit();////int l = list.size();//System.out.println("当前数据库中的Guestbook表中一共有"+ l + "条记录数。");//Guestbook gb = new Guestbook();//for(int i = 0;i<l;i++){////gb = (Guestbook) list.get(i);//System.out.println(gb.getName());//}//Criteriia接口与Query接口类似,他允许创建并执行面向对象方式的查询。Configuration config = new Configuration().configure();SessionFactory sessionFactory = config.buildSessionFactory();Session session = sessionFactory.getCurrentSession();Transaction tx = session.beginTransaction();Criteria crit = session.createCriteria(Guestbook.class);Criterion criterion1 = Restrictions.like("name", "ka%");Criterion criterion2 = Restrictions.between("id", new Integer(1), new Integer(20));crit.add(criterion1);crit.add(criterion2);List list =crit.list();tx.commit();int l = list.size();System.out.println("当前数据库中的Guestbook表中一共有" + l + "条name字段为ka字符开始且id值为1-20之间的记录");Guestbook gb = new Guestbook();for(int i =0;i<l;i++){gb = (Guestbook) list.get(i);System.out.println(gb.getName());}}}




注:

本代码为个人学习时所写,实现了hibernate对数据库数据的增删改查操作

涉及到了SessionFactory接口,Session接口,Transaction接口,Query接口,Criteria接口等一些方法的用法。

及Session接口中的get() load() update() delete() 方法的用法

和Session接口中的 createQuery(),createCriteria() 两个方法的用法。


此代码封装了多个功能模块的方法。

请参考部分运行理解。