关于实现抽取公共DAO

OA项目

该实例来自传智博客的汤阳光OA项目实战视频
该项目使用的是spring + struts2+ hibernate +mysql+jdk1.6+tomcat6.x

  • 今天做了一个公共的BaseDao 比如对所有的表的 删除 添加 修改等 都用这个DAO的 delete add update 方法(下文源码)

UML类图

先和大家说一下类的关系
实体类图

在设计的过程中考虑到后面重复性的增删改查的代码 所以就抽取出来baseDao 和 BaseDaoImpl 后面的实体就方便了如图
这里写图片描述

这里使用到反射的技术(得到具体的实体类)

// 使用反射技术得到T的真实类型
        ParameterizedType pt = (ParameterizedType) this.getClass()
                .getGenericSuperclass(); // 获取当前new的对象的 泛型的父类 类型
        this.clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个类型参数的真实类型
        System.out.println("clazz ---> " + clazz);
/**
 * 通过ID查询数据
 */
    public List<T> getByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Collections.EMPTY_LIST;
        } else {
            return getSession().createQuery(//
                    "FROM " + clazz.getSimpleName() + " WHERE id IN (:ids)")//hibernate对象查询
                    .setParameterList("ids", ids)//
                    .list();
        }
    }
/**
 * 查询所有
 */
    public List<T> findAll() {
        return getSession().createQuery(//
                "FROM " + clazz.getSimpleName())//
                .list();
    }

以后使用就特别方便 了(以User 为例)

  • Dao
public interface UserDao extends BaseDao<User> {
//entityDao extends BaseDao<传对象进去> 就可以了 
}
  • DaoImpl
  `public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
// 因为BaseDao中使用的是反射技术 , 就这样 user的增删改查功能就实现了
}`

就这样 user的增删改查功能就实现了(其它实体也一样的用)

这里我把 BaseDaoImpl中的代码贴出来


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

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public abstract class BaseDaoImpl<T> implements BaseDao<T> {
    @Resource
    private SessionFactory sessionFactory;
    private Class<T> clazz;

    @SuppressWarnings("unchecked")
    public BaseDaoImpl() {
        // 获得当前new 的对象的泛型的父类类型
        ParameterizedType pt = (ParameterizedType) this.getClass()
                .getGenericSuperclass();
        //  
        this.clazz = (Class<T>) pt.getActualTypeArguments()[0];

    }

    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    public void save(T entity) {
        getSession().save(entity);

    }

    public void delete(Long id) {
        Object object = getById(id);
        if (object != null) {
            getSession().delete(object);

        }

    }

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

    @SuppressWarnings("unchecked")
    public List<T> findAll() {

        return getSession().createQuery("FROM" + clazz.getSimpleName()).list();
    }

    @SuppressWarnings("unchecked")
    public T getById(Long id) {

        return (T) getSession().get(clazz, id);
    }

    @SuppressWarnings("unchecked")
    public List<T> getByIds(Long[] ids) {

        return getSession().createQuery("FROM User WHERE id IN (:ids0)")//
                .setParameterList("ids", ids)//
                .list();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值