编写一些简单的hibernate的Dao通用性接口(用泛型)

1.编写接口

package com.testdao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public interface IBaseDao<T> {

	public void save(T obj);

	public void deleteById(T obj);

	public List queryAll(T obj);

	public void update(T obj);

	public T queryTById(T obj, String id);

	public T queryTByMap(T obj, Map<String, String> condition);

	public List otherQuery(String hql);

	public T queryTById(T obj, T id);

}

2.编写继承类

package com.testdao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.util.HibernateUtil;

public class BaseDaoHibernate<T> implements IBaseDao<T> {

	private static Transaction transaction;

	@Override
	public void save(T obj) {
		Session session = HibernateUtil.getSession();
		transaction = session.beginTransaction();
		session.save(obj);
		transaction.commit();
		HibernateUtil.closeSession();
	}

	@Override
	public void deleteById(T obj) {

		Session session = HibernateUtil.getSession();
		transaction = session.beginTransaction();
		session.delete(obj);
		transaction.commit();
		HibernateUtil.closeSession();

	}

	@Override
	public List queryAll(T obj) {
		Session session = HibernateUtil.getSession();
		String objName = obj.getClass().getName();
		Query query = session.createQuery("from " + objName);
		// ParameterizedType parameterizedType = (ParameterizedType)
		// obj.getClass().getGenericSuperclass();
		// List<ParameterizedType> list = query.list();
		// Class.forName(objName+".MyClass").newInstance();
		List<T> list = query.list();
		HibernateUtil.closeSession();
		return list;
	}

	@Override
	public void update(T obj) {
		Session session = HibernateUtil.getSession();
		transaction = session.beginTransaction();
		session.save(obj);
		transaction.commit();
		HibernateUtil.closeSession();
	}

	@Override
	public T queryTById(T obj, String id) {
		Session session = HibernateUtil.getSession();
		String objName = obj.getClass().getName();
		String objName_id = objName.toLowerCase().substring(7) + "_id";
		String hql = "from " + objName + " where " + objName_id + "=" + id;
		System.out.println(hql);
		Query query = session.createQuery(hql);
		List<T> list = query.list();
		T classT = list.get(0);
		HibernateUtil.closeSession();
		return classT;
	}

	@Override
	public T queryTById(T obj, T id) {
		Session session = HibernateUtil.getSession();
		transaction = session.beginTransaction();
		T result = (T)session.get(obj.getClass(), (Serializable) id);
		transaction.commit();
		HibernateUtil.closeSession();
		return result;
	}

	@Override
	public T queryTByMap(T obj, Map<String, String> condition) {
		Session session = HibernateUtil.getSession();
		String objName = obj.getClass().getName();
		Iterator<String> iter = condition.keySet().iterator();
		List<String> key = new ArrayList<String>();
		List<String> value = new ArrayList<String>();
		while (iter.hasNext()) {
			String tempKey = iter.next();
			key.add(tempKey);
			value.add(condition.get(tempKey));
		}
		String hql = "from " + objName + " where ";
		for (int i = 0; i < key.size(); i++) {
			if (key.size() == (i + 1)) {
				hql = hql + key.get(i) + "=" + value.get(i);
			} else {
				hql = hql + key.get(i) + "=" + value.get(i) + " and ";
			}
		}
		System.out.println(hql);
		Query query = session.createQuery(hql);
		List<T> list = query.list();
		T classT = list.get(0);
		HibernateUtil.closeSession();
		return classT;
	}

	@Override
	public List otherQuery(String hql) {
		Session session = HibernateUtil.getSession();
		Query query = session.createQuery(hql);
		List<T> list = query.list();
		HibernateUtil.closeSession();
		return list;
	}
}


3.编写测试类

package com.testdao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.vo.Consumer;
import com.vo.Rider;

public class TestBaseDao {

	private IBaseDao baseDao = new BaseDaoHibernate();

	@Test
	public void saveT() {
		Consumer consumer = new Consumer();
		consumer.setCustomer_account("1514010624");
		consumer.setCustomer_age(18);
		consumer.setCustomer_name("谢魁星");
		consumer.setCustomer_password("123456");
		consumer.setCustomer_phone("18846420960");
		consumer.setCustomer_sex("男");
		consumer.setCustomer_starttime("2018-06-07");
		consumer.setCustomer_state("0");
		baseDao.save(consumer);
	}

	@Test
	public void deleteT() {
		Consumer consumer = new Consumer();
		consumer.setCustomer_id(2);
		baseDao.deleteById(consumer);
	}

	@Test
	public void queryAllT() {
		Consumer consumer = new Consumer();
		List<Consumer> consumerList = baseDao.queryAll(consumer);
		for (Consumer consumer2 : consumerList) {
			System.out.println(consumer2);
		}
	}

	@Test
	public void queryTById() {
		Rider rider = new Rider();
		String rider_id = "1";
		rider = (Rider) baseDao.queryTById(rider, rider_id);
		System.out.println(rider);
	}

	// 将查询的条件放进map里面进行查询
	@Test
	public void queryTByMap() {
		Consumer consumer = new Consumer();
		Map<String, String> condition = new HashMap<String, String>();
		condition.put("customer_id", "1");
		consumer = (Consumer) baseDao.queryTByMap(consumer, condition);
		System.out.println(consumer);
	}

	@Test
	public void otherQuery() {
		String hql = "from Consumer";
		List<Consumer> consumerList = baseDao.otherQuery(hql);
		for (Consumer consumer : consumerList) {
			System.out.println(consumer);
		}
	}
	
	@Test
	public void query() {
		Consumer consumer = new Consumer();
		consumer = (Consumer) baseDao.queryTById(consumer, 1);
		System.out.println(consumer);
	}

}

阅读更多
换一批

没有更多推荐了,返回首页