hibernate基础2:封装hibernateUtil,实现CRUD

Posted 来临

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate基础2:封装hibernateUtil,实现CRUD相关的知识,希望对你有一定的参考价值。

1、封装hibernateUtil

package com.project.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static Configuration cfg = null;
    private static StandardServiceRegistry registry = null;
    private static SessionFactory factory = null;
    private static ThreadLocal<Session> sessionLocal = null;
    
    static{
        //1、读取hibernate.cfg.xml配置文件
        cfg = new Configuration().configure();
        //2、创建服务注册器
        registry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
        //3、新建sessionFactory对象
        //sessionFactory是一个重量级对象,在一个应用只需一个即可,是进程级别的对象,可以在集群中使用,操作完后不释放
        factory = cfg.buildSessionFactory(registry);
        //4、创建threadLocal(成员私有,线程安全)
        sessionLocal = new ThreadLocal<>();
    }
    //获取session对象
    public static Session getSession(){
        //session不为空且session未被打开时,session设置为null
        if(sessionLocal.get() != null && !sessionLocal.get().isOpen()){
            sessionLocal.set(null);
        }
        //session为null时,创建session
        if(sessionLocal.get() == null){
            sessionLocal.set(factory.openSession());
        }
        return sessionLocal.get();
    }
    
    //关闭session
    public static void closeSession(){
        if(sessionLocal.get() != null){
            //session调用close不一定清除session对象
            sessionLocal.get().close();
            //所以sessionLocal中session对象设置为null
            sessionLocal.set(null);
        }
    }
}

 

2、实现hibernate新增数据(save)

private static void inset() {
        Session session = null;
        Transaction ts = null;
        try {
            //创建session
            session = HibernateUtil.getSession();
            //获取事务并开启事务
            ts = session.beginTransaction();
            //新增数据
            User u = new User();
            u.setName("李四");
            u.setAge(45);
            //保存
            session.save(u);
            ts.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(ts!=null)ts.rollback();
        }finally {
            HibernateUtil.closeSession();
        }
    }

3、实现hibernate修改数据(save)

    private static void update() {
        Session session = null;
        Transaction ts = null;
        try {
            //创建session
            session = HibernateUtil.getSession();
            //获取事务并开启事务
            ts = session.beginTransaction();
            //查询数据
            User u = (User) session.get(User.class, 2);
       //修改数据 u.setName(
"小李子"); //保存 session.save(u); ts.commit(); } catch (Exception e) { e.printStackTrace(); if(ts!=null)ts.rollback(); }finally { HibernateUtil.closeSession(); } }

4、实现hibernate删除数据(delete)

private static void delete() {
        Session session = null;
        Transaction ts = null;
        try {
            //创建session
            session = HibernateUtil.getSession();
            //获取事务并开启事务
            ts = session.beginTransaction();
            //查询数据
            User u = (User) session.get(User.class, 2);
            //删除
            session.delete(u);
            ts.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(ts!=null)ts.rollback();
        }finally {
            HibernateUtil.closeSession();
        }
    }

 

以上是关于hibernate基础2:封装hibernateUtil,实现CRUD的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate ORM初始(入门基础)

Mybatis----基础

hibernate4 baseDao的封装

Hibernate基础

Hibernate基础

Hibernate 基础入门