⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernate.java

📁 &#61553 网站前台 商品销售排行
💻 JAVA
字号:
package mrgf.other;

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import java.util.List;

public class Hibernate {
    public Hibernate() {
    }

    //
    private static SessionFactory sessionFactory;
    static {
        try {
            Configuration config = new Configuration().configure();
            sessionFactory = config.buildSessionFactory();
        } catch (Exception e) {
            System.out.println("------在初始化hibernate时抛出异常,内容如下:");
            e.printStackTrace();
        }
    }

    private Session session = null;
    private Transaction tx = null;

    //打开session,开启事务
    private void openSession() {
//        System.out.println("------开启session,打开事务:");
        session = sessionFactory.openSession();
        tx = session.beginTransaction();
    }

    //关闭session,关闭事务
    private void closeSession() {
//        System.out.println("------提交事务,关闭session:");
        if (tx != null) {
            tx.commit();
        }
        if (session != null) {
            session.close();
        }
    }

    //检索所有对象
    public List query(String hql) {
        this.openSession();
        List result = null;
        try {
            Query query = session.createQuery(hql);
            result = query.list();
        } catch (Exception e) {
            System.out.println("------在检索对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
        return result;
    }

    //检索一个对象
    public Object queryOne(String hql) {
        this.openSession();
        Object obj = null;
        try {
            obj = (Object) session.createQuery(hql).setMaxResults(1).
                  uniqueResult();
        } catch (Exception e) {
            System.out.println("------在检索对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
        return obj;
    }

    //检索对象通过SQL
    public List queryWithSql(String sql) {
        this.openSession();
        List result = null;
        try {
            Query query = session.createSQLQuery(sql);
            result = query.list();
        } catch (Exception e) {
            System.out.println("------在检索对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
        return result;
    }

    //检索对象部分属性
    public Object[] querySomeAttribute(String sql) {
        this.openSession();
        Object[] result = null;
        try {
            Query query = session.createSQLQuery(sql);
            result = query.getReturnTypes();
        } catch (Exception e) {
            System.out.println("------在检索对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
        return result;
    }


    //删除对象
    public void delete(Object object) {
        this.openSession();
        try {
            session.delete(object);
        } catch (Exception e) {
            System.out.println("------在删除对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
    }

    //删除对象通过SQL
    public void delete(String hql) {
        this.openSession();
        try {
            Query query = session.createQuery(hql);
            List objects = query.list();
            for (int i = 0; i < objects.size(); i++) {
                Object object = (Object) objects.get(i);
                session.delete(object);
            }
        } catch (Exception e) {
            tx.rollback();
            System.out.println("------在删除对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
    }

    //持久化对象
    public boolean save(Object object) {
        this.openSession();
        boolean hasSave = true;
        try {
            session.save(object);
        } catch (Exception e) {
            hasSave = false;
            System.out.println("------在持久化对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
        System.out.println(hasSave);
        return hasSave;
    }

    //更新对象
    public void update(Object object) {
        this.openSession();
        try {
            session.update(object);
        } catch (Exception e) {
            System.out.println("------在更新对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
    }

    //更新对象
    public void saveOrUpdate(Object object) {
        this.openSession();
        try {
            session.saveOrUpdate(object);
        } catch (Exception e) {
            System.out.println("------在更新对象时抛出异常,内容如下:");
            e.printStackTrace();
        }
        this.closeSession();
    }

    //取字段最大值
    public String getMax(String sql) {
        this.openSession();
        String max = null;
        try {
            max = (String) session.createSQLQuery(sql).uniqueResult();
        } catch (Exception e) {
            System.out.println("------在取字段最大值时抛出异常,内容如下:");
            e.printStackTrace();
        } finally {
            this.closeSession();
        }
        return max;
    }

    //统计信息
    public int count(String hql) {
        this.openSession();
        Long c = new Long(0);
        try {
            c = (Long) session.createQuery(hql).uniqueResult();
        } catch (Exception e) {
            System.out.println("------在取字段最大值时抛出异常,内容如下:");
            e.printStackTrace();

        } finally {
            this.closeSession();
        }
        return c.intValue();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -