/* * //创建hibernate配置对象 Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); //注册配置属性信息 ServiceRegistry sr = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); //创建sessionFactory SessionFactory factory = cfg.buildSessionFactory(sr); //创建session Session session = factory.openSession(); //开启事务之后,才能保存 Transaction tx = session.beginTransaction();*/ /** * hibernate5.x版本 */ SessionFactory factory = new Configuration().configure().buildSessionFactory(); Session session = factory.openSession(); Transaction tx = session.beginTransaction(); //创建对象 User user = new User(); user.setUserId(1); user.setUname("shanxinxin"); user.setGender(0); user.setBirthday(new Date(0)); //通过session保存对象 session.save(user); //提交事务 tx.commit(); //关闭事务 session.close(); }