最近在看视频学习hibernate,打算写一系列的博客,作为对学习的总结和回顾。
使用hibernate的几个步骤。
1.导入hibernate jar包和mysql jar包。
2.创建hibernate.cfg.xml配置文件。
a.数据库连接配置
b.hibernate属性配置
c.hbm.xml文件关联
3.创建实体类。
4.创建实体类的 hbm.xml对应文件。
5.使用hibernate api来保存对象。
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
News news = new News("好好学习hibernate", new Date());
session.save(news);
transaction.commit();
session.close();
sessionFactory.close();