首页 > 代码库 > Hibernate5-课程笔记2

Hibernate5-课程笔记2

单表的增删改查操作:

  (1)定义获取Session和SessionFactory的工具类:

 1 package com.tongji.utils;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.cfg.Configuration;
 6 
 7 public class HbnUtils {
 8     private static SessionFactory sessionFactory;
 9     public static Session getSession() {
10          return getSessionFactory().getCurrentSession();
11     }
12     
13     public static SessionFactory getSessionFactory() {
14         if (sessionFactory == null || sessionFactory.isClosed()) {
15             sessionFactory = new Configuration().configure().buildSessionFactory();
16         }
17         return sessionFactory;
18     }
19 }

  (2) 增加操作:

 1 @Test
 2 public void testSave() {
 3     //1. 获取Session
 4     Session session = HbnUtils.getSession();
 5     try {
 6         //2. 开启事务
 7         session.beginTransaction();
 8         //session.getTransaction().begin();  同样
 9         //3. 操作
10         Student student = new Student("张三", 23, 93.5);
11         session.save(student);
12         //4. 事务提交
13         session.getTransaction().commit();
14     } catch (Exception e) {
15         e.printStackTrace();
16         //5. 事务回滚
17         session.getTransaction().rollback();
18     }
19 }
20 
21 @Test
22 public void testPersist() {
23     //1. 获取Session
24     Session session = HbnUtils.getSession();
25     try {
26         //2. 开启事务
27         session.beginTransaction();
28         //session.getTransaction().begin();  同样
29         //3. 操作
30         Student student = new Student("张三", 23, 93.5);
31         session.persist(student);    //JPA接口的API,效果同save
32         //4. 事务提交
33         session.getTransaction().commit();
34     } catch (Exception e) {
35         e.printStackTrace();
36         //5. 事务回滚
37         session.getTransaction().rollback();
38     }
39 }

  (3)删除操作:

 1 @Test
 2 public void testDelete() {
 3     //1. 获取Session
 4     Session session = HbnUtils.getSession();
 5     try {
 6         //2. 开启事务
 7         session.beginTransaction();
 8         //3. 操作
 9         Student student = new Student();
10         student.setId(6);
11         session.delete(student);  //删除是根据对象的id,进行删除的
12         //4. 事务提交
13         session.getTransaction().commit();
14     } catch (Exception e) {
15         e.printStackTrace();
16         //5. 事务回滚
17         session.getTransaction().rollback();
18     }
19 }

  (4) 更新操作:

 1 @Test
 2 public void testUpdate() {
 3     //1. 获取Session
 4     Session session = HbnUtils.getSession();
 5     try {
 6         //2. 开启事务
 7         session.beginTransaction();
 8         //3. 操作
 9         Student student = new Student("李四", 24, 94.5);
10         student.setId(5);
11         session.update(student);  //修改是根据对象的id,进行修改的
12         //4. 事务提交
13         session.getTransaction().commit();
14     } catch (Exception e) {
15         e.printStackTrace();
16         //5. 事务回滚
17         session.getTransaction().rollback();
18     }
19 }
20 
21 @Test
22 public void testUpdate2() {
23     //1. 获取Session
24     Session session = HbnUtils.getSession();
25     try {
26         //2. 开启事务
27         session.beginTransaction();
28         //3. 操作
29         Student student = new Student("李四", 24, 94.5);
30         student.setId(3);
31         session.saveOrUpdate(student);  //执行save还是uodate的判断依据是,操作对象是否存在id
32         //4. 事务提交
33         session.getTransaction().commit();
34     } catch (Exception e) {
35         e.printStackTrace();
36         //5. 事务回滚
37         session.getTransaction().rollback();
38     }
39 }

  (5) 查询操作:

 1 @Test
 2 public void testGet() {
 3     //1. 获取Session
 4     Session session = HbnUtils.getSession();
 5     try {
 6         //2. 开启事务
 7         session.beginTransaction();
 8         //3. 操作
 9         //若查询的对象不存在,get返回null
10         Student student = session.get(Student.class, 5);
11         System.out.println(student);
12         //4. 事务提交
13         session.getTransaction().commit();
14     } catch (Exception e) {
15         e.printStackTrace();
16         //5. 事务回滚
17         session.getTransaction().rollback();
18     }
19 }
20 
21 @Test
22 public void testLoad() {
23     //1. 获取Session
24     Session session = HbnUtils.getSession();
25     try {
26         //2. 开启事务
27         session.beginTransaction();
28         //3. 操作
29         //若查询的对象不存在,load抛出异常
30         Student student = session.load(Student.class, 5);
31         System.out.println(student);
32         //4. 事务提交
33         session.getTransaction().commit();
34     } catch (Exception e) {
35         e.printStackTrace();
36         //5. 事务回滚
37         session.getTransaction().rollback();
38     }
39 }
40 
41 @Test
42 public void testLoad2() {
43     //1. 获取Session
44     Session session = HbnUtils.getSession();
45     //Session必须在事务环境下运行,无论是增、删、改,还是查询
46     Student student = session.load(Student.class, 5);
47     System.out.println(student);
48 }

  (6) 增删改的底层执行顺序:

 1 @Test
 2 public void testSDU() {
 3     //1. 获取Session
 4     Session session = HbnUtils.getSession();
 5     try {
 6         //2. 开启事务
 7         session.beginTransaction();
 8         //3. 操作
 9         //删除
10         Student student = session.get(Student.class, 2);
11         session.delete(student);
12         session.flush();  //添加一个刷新点
13         //修改
14         Student student2 = session.get(Student.class, 7);
15         student2.setName("王小五");
16         session.update(student2);
17         //添加
18         Student student3 = new Student("赵晓刘", 26, 96.5);
19         session.save(student3);
20         //4. 事务提交
21         session.getTransaction().commit();
22     } catch (Exception e) {
23         e.printStackTrace();
24         //5. 事务回滚
25         session.getTransaction().rollback();
26     }
27 }

    注意:底层执行的顺序是,先执行两次查询操作,再依次执行增、改、删。(忽略代码中的sessio.flush())

       如果加入了sessio.flush(),则刷新点之前的操作先执行完,再执行刷新点之后的操作。

Hibernate5-课程笔记2