package com.ustc.hibernate.model;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class PersonTest {
public static void main(String[] args) {
//Person p=new Person();
List<Person> qlist=new ArrayList();
//p.setAge(24);
//p.setId(2);
//p.setName("chris");
Configuration cfg=new Configuration();//Configuration类的作用是对Hibernate进行配置,加载配置文件
SessionFactory sf=cfg.configure().buildSessionFactory();//通过Configuration类的实例cfg来构建SessionFactory
Session session=sf.openSession();//从SessionFactory获取session
session.beginTransaction();//session相当于JDBC里的Connection
String hql="from Person p";
Query q=session.createQuery(hql);
qlist=q.list();
for(Person p:qlist){
System.out.println("ID:"+p.getId());
System.out.println("姓名:"+p.getName());
System.out.println("年龄:"+p.getAge());
}
//session.save(p);
session.getTransaction().commit();
session.close();
sf.close();
}
}
运行结果: