本文地址:2.基础学习 - 荣慕平 - 博客园 (cnblogs.com)

一、原生代码执行插入一条记录

 1 package com.sdkj.hibernate.junitTest;
 2 
 3 import com.sdkj.hibernate.domain.T2UserEntity;
 4 import org.hibernate.Session;
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.Transaction;
 7 import org.hibernate.boot.MetadataSources;
 8 import org.hibernate.boot.registry.StandardServiceRegistry;
 9 import org.hibernate.cfg.Configuration;
10 import org.junit.jupiter.api.Test;
11 
12 import java.math.BigDecimal;
13 
14 /**
15  * @Author wangshuo
16  * @Date 2022/4/7, 16:10
17  * 原生代码执行插入一条记录
18  */
19 
20 public class TestHibernate_1 {
21 
22     @Test
23     public void test(){
24 
25         //建立数据库连接会话
26         SessionFactory sessionFactory = null;
27         StandardServiceRegistry configuration = new Configuration().configure()
28                 .getStandardServiceRegistryBuilder().build();
29         sessionFactory = new MetadataSources(configuration).buildMetadata().buildSessionFactory();
30 
31         //通过工厂类开启Session对象
32         Session session = sessionFactory.openSession();
33 
34         //开启事务处理
35         Transaction transaction = session.beginTransaction();
36 
37         //执行操作(面向对象)
38         T2UserEntity xinJie_song = new T2UserEntity("XinJie_Song", BigDecimal.valueOf(1.2));
39         session.save(xinJie_song);//不需要sql语句 hibernate全自动封装
40 
41         //提交事务
42         transaction.commit();
43 
44         //关闭session
45         session.close();
46 
47         //关闭工厂
48         sessionFactory.close();
49     }
50 }

二、get() / load()查询 封装方法

 1 package com.sdkj.hibernate.junitTest;
 2 
 3 import com.sdkj.hibernate.domain.T2UserEntity;
 4 import com.sdkj.hibernate.util.CommonUtil;
 5 import org.hibernate.Session;
 6 import org.hibernate.SessionFactory;
 7 import org.hibernate.Transaction;
 8 import org.hibernate.boot.MetadataSources;
 9 import org.hibernate.boot.registry.StandardServiceRegistry;
10 import org.hibernate.cfg.Configuration;
11 import org.junit.jupiter.api.Test;
12 
13 import java.math.BigDecimal;
14 
15 /**
16  * @Author wangshuo
17  * @Date 2022/4/7, 20:02
18  * get() / load()查询 封装方法
19  */
20 public class TestHibernate_2_1 {
21 
22     public SessionFactory getSessionFactory(){
23 
24         return (new MetadataSources(new Configuration().configure()
25                 .getStandardServiceRegistryBuilder().build()).buildMetadata().buildSessionFactory());
26     }
27     public void commit(Transaction transaction, SessionFactory sessionFactory, Session session){
28         transaction.commit();
29         session.close();
30         sessionFactory.close();
31     }
32 
33     @Test
34     public void test(){
35 
36         //建立数据库连接会话
37         SessionFactory sessionFactory = getSessionFactory();
38         //通过工厂类开启Session对象
39         Session session = sessionFactory.openSession();
40         //执行操作(面向对象)
41         T2UserEntity xinJie_song = new T2UserEntity();
42         T2UserEntity load = session.load(xinJie_song.getClass(), 1);//根据id查询
43         System.out.println("id = " + load.getId() +
44                 " name = " + load.getName() + " balance = " + load.getBalance());
45 
46         //善后
47         commit(/*事务管理*/session.beginTransaction(),/*工厂*/sessionFactory,/*会话*/session);
48     }
49 }

三、flush() / refresh() / clear()

 1 package com.sdkj.hibernate.junitTest;
 2 
 3 import com.sdkj.hibernate.domain.T2UserEntity;
 4 import org.hibernate.Session;
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.Transaction;
 7 import org.junit.jupiter.api.Test;
 8 
 9 /**
10  * @Author wangshuo
11  * @Date 2022/4/7, 20:28
12  * flush()将缓存中与数据库不同的数据更新到数据库
13  * refresh()发起查询查数据库中的对象,将数据库中的数据覆盖到缓存
14  * clear()清除缓存
15  */
16 public class TestHibernate_2_2 extends TestHibernate_2_1{
17 
18     @Test
19     public void testFlush(){
20 
21         SessionFactory sessionFactory = getSessionFactory();
22         Session session = sessionFactory.openSession();
23         Transaction transaction = session.beginTransaction();
24 
25         T2UserEntity load = session.load(T2UserEntity.class, 1);
26         load.setName("Flush啊哈哈");
27         session.flush();
28         commit(transaction,sessionFactory,session);
29     }
30 
31     @Test
32     public void testRefresh(){
33 
34         SessionFactory sessionFactory = getSessionFactory();
35         Session session = sessionFactory.openSession();
36         Transaction transaction = session.beginTransaction();
37 
38         T2UserEntity t2UserEntity = session.load(T2UserEntity.class, 1);
39         t2UserEntity.setName("ReFresh啊哈哈");
40         session.refresh(t2UserEntity);
41         commit(transaction,sessionFactory,session);
42     }
43 
44     @Test
45     public void testClear(){
46 
47         SessionFactory sessionFactory = getSessionFactory();
48         Session session = sessionFactory.openSession();
49         Transaction transaction = session.beginTransaction();
50 
51         T2UserEntity t2UserEntity = session.load(T2UserEntity.class, 1);
52         session.clear();//清除缓存后缓存不起作用了,要再走一次数据库了
53         T2UserEntity t2UserEntity1 = session.load(T2UserEntity.class,1);
54         commit(transaction,sessionFactory,session);
55     }
56 }

四、update() / delete() / saveOrUpdate()

 1 package com.sdkj.hibernate.junitTest;
 2 
 3 import com.sdkj.hibernate.domain.T2UserEntity;
 4 import org.hibernate.Session;
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.Transaction;
 7 import org.junit.jupiter.api.Test;
 8 
 9 import java.math.BigDecimal;
10 
11 /**
12  * @Author wangshuo
13  * @Date 2022/4/8, 8:54
14  * update() / delete() / saveOrUpdate() demo
15  */
16 public class TestHibernate_2_3 extends TestHibernate_2_1{
17 
18     @Test
19     public void testUpdate(){
20 
21         SessionFactory sessionFactory = getSessionFactory();
22         Session session = sessionFactory.openSession();
23         Transaction transaction = session.beginTransaction();
24         T2UserEntity t2UserEntity = session.load(T2UserEntity.class,6);
25         t2UserEntity.setBalance(BigDecimal.ONE);
26         t2UserEntity.setName("饿呢嗯");
27         session.update(t2UserEntity);
28         commit(transaction,sessionFactory,session);
29     }
30 
31     @Test
32     public void testDelete(){
33 
34         SessionFactory sessionFactory = getSessionFactory();
35         Session session = sessionFactory.openSession();
36         Transaction transaction = session.beginTransaction();
37         T2UserEntity t2UserEntity = new T2UserEntity();
38         t2UserEntity.setId(6);
39         session.delete(t2UserEntity);
40         commit(transaction,sessionFactory,session);
41     }
42 
43     @Test
44     public void testSaveOrUpdate(){
45 
46         SessionFactory sessionFactory = getSessionFactory();
47         Session session = sessionFactory.openSession();
48         Transaction transaction = session.beginTransaction();
49         T2UserEntity t2UserEntity = new T2UserEntity("name1",BigDecimal.TEN);
50         t2UserEntity.setName("name2");
51         session.saveOrUpdate(t2UserEntity);
52         commit(transaction,sessionFactory,session);
53     }
54 }

五、对象在session中的状态解释

 1 package com.sdkj.hibernate.junitTest;
 2 
 3 import com.sdkj.hibernate.domain.T2UserEntity;
 4 import org.hibernate.Session;
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.Transaction;
 7 import org.junit.jupiter.api.Test;
 8 
 9 import java.math.BigDecimal;
10 
11 /**
12  * @Author wangshuo
13  * @Date 2022/4/8, 8:05
14  * 做一个对象在session中的状态解释(临时状态、游离状态、持久化状态)  加个try catch
15  */
16 public class TestHibernateState extends TestHibernate_2_1{
17 
18     @Test
19     public void testState(){
20 
21         SessionFactory sessionFactory = getSessionFactory();
22         Session session = sessionFactory.openSession();
23         Transaction transaction = session.beginTransaction();
24         //事务控制,保证原子性 hibernate本来就实现了原子性,这里简单写个demo模拟
25         try {
26             T2UserEntity t2UserEntity = session.load(T2UserEntity.class,5);//对象进入缓存 状态为持久化状态
27             System.out.println(t2UserEntity.getId());
28             t2UserEntity.setName("修改后");//此时我修改对象的某个属性
29         }catch (Exception e){
30             e.printStackTrace();
31             //出错回滚
32             session.clear();
33         }finally {
34             commit(transaction,sessionFactory,session);//提交,session.flush()起了作用,会再执行一条update语句
35         }
36     }
37 }