继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Hibernate泛型DAO编写

car
关注TA
已关注
手记 83
粉丝 56
获赞 363
package com.saicmaxus.user.repository.dao;

import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;

import javax.naming.InitialContext;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateDao<T> {

    private final SessionFactory sf = getSessionFactory();
    private Class<T> clazz;

    public void ddl() {
        Configuration cfg = new Configuration();
        cfg.configure();
        // SchemaExport export=new SchemaExport(cfg);
        // export.create(true, true);

    }

    public HibernateDao() {

    }

public void closeSessionFactory(){
    sf.close();
}
    protected SessionFactory getSessionFactory() {
        Configuration conf = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(conf.getProperties()).build();
        SessionFactory factory = conf.buildSessionFactory(serviceRegistry);
        return factory;
    }

    public void saveEntity(T t) {
        Session session = sf.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.save(t);
        transaction.commit();
        session.close();
    }

    public void updateEntity(T t) {
        Session session = sf.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.update(t);
        transaction.commit();
        session.close();
    }

    public void saveOrUpdateEntity(T t) {
        Session session = sf.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.saveOrUpdate(t);
        transaction.commit();
        session.close();
    }

    public void deleteEntity(T t) {
        Session session = sf.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        session.delete(t);
        transaction.commit();
        session.close();
    }

    // 批量更新
    public void batchEntityByHQL(String hql, Object... objects) {
        Query q = sf.getCurrentSession().createQuery(hql);
        for (int i = 0; i < objects.length; i++) {
            q.setParameter(i, objects[i]);
        }
        q.executeUpdate();
    }

    public T getEntity(Number id, Class<T> clazz) {
        Session session = sf.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        T t = (T) session.get(clazz, id);
        transaction.commit();
        return t;
    }

    public T loadEntity(Number id, Class<T> clazz) {
        Session session = sf.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        T t = (T) session.load(clazz, id);
        transaction.commit();
        return t;

    }

    public List<T> findEntityByHQL(String hql, Object... objects) {
        Session session = sf.getCurrentSession();
         Transaction transaction = session.beginTransaction();
        Query q = session.createQuery(hql);
        for (int i = 0; i < objects.length; i++) {
            q.setParameter(i, objects[i]);
        }
        List<T> list = q.list();
        transaction.commit();
        return list;
    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP