//创建服务注册对象
ServiceRegistry serviceRegistry =
new
StandardServiceRegistryBuilder().configure().build();
//创建会话工厂对象
SessionFactory sessionFactory =
new
MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
//会话对象
Session session = sessionFactory.openSession();
//开启事物
Transaction transaction = session.beginTransaction();
//输入实体类的信息
System.out.println(
"请依次输入:账号 密码"
);
int
name=cin.nextInt();
String pwd=cin.next();
Admin admin=
new
Admin(name,pwd);
System.out.println(name+
" :"
+pwd);
//保存到数据库,持久化操作
session.save(admin);
//提交事务
transaction.commit();
//关闭会话
session.close();
//关闭会话工厂
sessionFactory.close();