编写一些简单的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);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值