1、保存:Session的save方法
@Testpublic void testSave() {Customer c = new Customer();c.setCustName("测试");Session s = HibernateUtils.openSession();Transaction t = s.beginTransaction();s.save(c);t.commit();s.close();}
2、查询:Session的get()方法
@Testpublic void testFindOne() {Session s = HibernateUtils.openSession();Transaction t = s.beginTransaction();Customer c = s.get(Customer.class, 2L);System.out.println(c);t.commit();s.close();}
3、修改:Session的update()方法
@Testpublic void testUpdate() {Session s = HibernateUtils.openSession();Transaction t = s.beginTransaction();Customer c = s.get(Customer.class, 2L);c.setCustAddress("110");s.update(c);t.commit();s.close();}
4、删除:Session的delete()方法
@Testpublic void testDelete() {Session s = HibernateUtils.openSession();Transaction t = s.beginTransaction();Customer c = s.get(Customer.class, 2L);s.delete(c);t.commit();s.close();}
5、查询全部:Session的createSQLQuery()方法
@Testpublic void testFindAll() {Session s = HibernateUtils.openSession();Transaction t = s.beginTransaction();SQLQuery sqlQuery = s.createSQLQuery("select * from cst_customer");List list = sqlQuery.list();for(Object o : list) {System.out.println(o);}t.commit();s.close();}