码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-05-19 22:17:11      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:文件   standard   配置文件   cer   读取   rac   操作   tst   vat   

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

标签:文件   standard   配置文件   cer   读取   rac   操作   tst   vat   

原文地址:https://www.cnblogs.com/chai-blogs/p/12919776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!