日期:2014-05-18 浏览次数:20813 次
public class TeacherTest {
public static void main(String[] args) {
Teacher t = new Teacher();
t.setName("t1");
t.setTitle("aaaa");
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
Teacher t = new Teacher(); //创建Teacher对象的实例,并且下下面初始化属性
t.setName("t1");
t.setTitle("aaaa");
Configuration cfg = new AnnotationConfiguration();//读取以注解方式配置的信息
SessionFactory sf = cfg.configure().buildSessionFactory();//创建session工厂
Session session = sf.openSession();//打开会话,和浏览器会话不一样,这里用于控制事务的
session.beginTransaction();//打开事务
session.save(t);//在该事务中所做的操作
session.getTransaction().commit();//提交事务
session.close();//关闭会话
sf.close();//关闭session工厂,操作结束、