habernate数据库的增删改查
package com.lhp.tes;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.lhp.AbstractTest;
import com.lhp.HibernateSessionFactory;
import com.lhp.Test;
public class test {
public static void main(String args[]){
//查询数据成功
String hql = "from com.lhp.AbstractTest";
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
List<com.lhp.AbstractTest> listTest = query.list();
for (Iterator iterator = listTest.iterator(); iterator.hasNext();) {
com.lhp.AbstractTest test = (com.lhp.AbstractTest) iterator.next();
System.out.println(test.getId() + " " + test.getName() + " " + test.getPwd());
}
HibernateSessionFactory.closeSession();
//删除数据
// Session session = HibernateSessionFactory.getSession();
// Test test = new Test(1,"asdf","sadfsadf");
// Transaction ts = session.beginTransaction();
// session.delete(test);
// ts.commit();
// session.close();
//添加数据成功
// Session session1 = HibernateSessionFactory.getSession();
// Test test1 = new Test(2,"qwe","sqwe");
// Transaction tst = session1.beginTransaction(); //打开事务
// session1.save(test1);
// tst.commit(); //提交事务
// session1.close();
//查询数据库中的一条记录
// Session session = HibernateSessionFactory.getSession();
// Test test = (Test) session.get(Test.class,2);
//
// System.out.println(test.getId() + " " + test.getName() + " " + test.getPwd());
// session.close();
//修改数据成功
// Session session = HibernateSessionFactory.getSession();
//
// com.lhp.Test test = new com.lhp.Test(3,"admin","as456");
//
// Transaction tst = session.beginTransaction(); //打开事务
//
// session.update(test);
//
// tst.commit(); //提交事务
//
// session.close();
//
}
}