hibernate的增删改查和事务

版权声明:原创文章,未经允许不得转载 https://blog.csdn.net/Destiny_strive/article/details/81432860

1.导入需要的jar(hibernate-release-5.0.7.Final)

2.创建一个实体(生成对应的get,set)

 

3.配置student.hbm.xml

4.配置 hibernate.cfg.xml

5.测试
 获取SessionFactory 
 通过SessionFactory 获取一个Session
 在Session基础上开启一个事务
 通过调用Session的save方法把对象保存到连接的数据库
 提交事务
 关闭Session
 关闭SessionFactory

如果需要添加事务,修改hibernate.cfg.xml配置文件

新建一个utils,确保session一致

编写测试

注意这里不能在手动关闭session.close(),如果加上会报错:

猜你喜欢

转载自blog.csdn.net/Destiny_strive/article/details/81432860