SSH的Base类实现

我们在做一些项目的时候,常常会有许多的类,需要进行同样的CRUD,这时候我们为了方便,我们可以把这些同样的CRUD给提取出来,用反射泛型来实现。

首先我们先定义一个Base接口

package com.bft.oa.base;

import java.util.List;

public interface IBaseDao<T> {
	//增加
	public void add(T entity);
	//删除
	public void delete(Long id);
	//修改
	public void update(T entity);
	//查询单个对象
	public T getById(Long id);
	//查询所有
	public List<T> selectAll();
	//查询多个对象
	public List<T> getByIds(Long[]ids);
}

     这里面包含了一些简单的CRUD,如果有不同的也可以自己加上去。

    接下来写Base的实现类

package com.bft.oa.base;

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

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

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

	@Autowired
	private SessionFactory sessionFactory;
	
	//建立一个Class模型
	private Class clazz;
	
	//StudentDaoImpl(当前类) extends BaseDaoImpl(父类)<Student>(泛型)
	//构造方法在初始化的时候执行
	public BaseDaoImpl() {
		//获取当前类的父类对象
		ParameterizedType  types=(ParameterizedType) getClass().getGenericSuperclass();
		//获取父类中的泛型集合
		Type[] type = types.getActualTypeArguments();
		//获得第一个泛型
		clazz =(Class) type[0];
	}
	
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	@Override
	public void add(T entity) {
		// TODO Auto-generated method stub
		getSession().save(entity);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		getSession().delete(getSession().get(clazz, id));
	}

	@Override
	public void update(T entity) {
		// TODO Auto-generated method stub
		getSession().update(entity);
	}

	@Override
	public T getById(Long id) {
		// TODO Auto-generated method stub
		return (T) getSession().get(clazz, id);
	}

	@Override
	public List<T> selectAll() {
		// TODO Auto-generated method stub
		String hql=" from "+ clazz.getSimpleName();
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	@Override
	public List<T> getByIds(Long[] ids) {
		// TODO Auto-generated method stub
		String hql=" from "+clazz.getSimpleName()+" t where t.id in (:ids) ";
		Query query = getSession().createQuery(hql);
		query.setParameterList("ids",ids);
		return query.list();
	}

}

接下来我们只需要在Dao层调用就可以了

package com.bft.oa.dao;

import com.bft.oa.base.IBaseDao;
import com.bft.oa.entity.Student;

public interface IStudentDao extends IBaseDao<Student> {

}
package com.bft.oa.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bft.oa.base.BaseDaoImpl;
import com.bft.oa.dao.IStudentDao;
import com.bft.oa.entity.Student;
@Repository
public class StudentDaoImpl extends BaseDaoImpl<Student> implements IStudentDao {


}

这样就OK啦,简单的提取CRUD。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值