Hibernate的工作原理

xiaoxiao2021-02-27  164

a)读取并解析配置文件

b)读取解析映射信息,创建sessionFactory

c)打开session

d)创建事务Transation

e)持久化操作

f)提交事务

g)关闭session

h)关闭sessionFactory

/** * 第一步:加载配置(默认加载src下的application.xml) */ Configuration configure = new Configuration().configure(); /** * 第二步:获取sessionFactory */ SessionFactory sessionFactory = configure.buildSessionFactory(); /** * 第三步:获取session对象 */ Session session = sessionFactory.getCurrentSession(); /** * 第四步:开启事务 */ try { session.beginTransaction(); /** * 第五步:执行操作 */ Student student = new Student("李四", 20, 89.9); // 执行操作 session.save(student); /** * 第六步:提交事务 */ session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); // 回滚 session.getTransaction().rollback(); }finally{ //关闭资源 session.close(); sessionFactory.close(); }
转载请注明原文地址: https://www.6miu.com/read-13642.html

最新回复(0)