手记

Hibernate泛型DAO编写

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;
    }
}
0人推荐
随时随地看视频
慕课网APP