论坛首页 Java版 DAO

利用JDK5的泛型增强Dao基类

浏览 13744 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2005-06-17
泛型是语言层面的东西,就是保证了型别安全,避免了转型操作。同样一个接口你可以用Object自己转型,也可以用泛型事先就约定好,那你认为泛型的意义在哪里?
   
0 请登录后投票
最后更新时间:2006-04-26
private Class<T> cls;
同问,什么时候给cls赋值呢?
   
0 请登录后投票
最后更新时间:2006-04-26
[code:1]
public abstract class BaseDAOHibernate<E> extends HibernateDaoSupport implements DAO<E> , CommDAO {

    protected final Log log = LogFactory.getLog(getClass());

    /** The class that this instance provides services for */
    private Class supportsClass;
   
    public BaseDAOHibernate() {
this.supportsClass = GenericsUtils.getGeneric(getClass());
if (this.supportsClass == null) {
if (logger.isWarnEnabled()) {
logger.warn("Could not determine the generics type - you will need to set manually");
}
}
}

public void create(E value) {
        Assert.notNull(value);
        getHibernateTemplate().save(value);
}

public void delete(E value) {
        Assert.notNull(value);
        E po = readPO(value);
getHibernateTemplate().delete(po);
}

public List<E> findAll() {
return (List<E>) getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria c = session.createCriteria(supportsClass);
c.addOrder(Order.desc("id"));
return c.list();
}
});
}

public List<E> findId(Collection<Serializable> ids) {
        Assert.notNull(ids, "Collection of IDs cannot be null");
        Assert.notEmpty(ids, "There must be some values in the Collection list");

        return (List) getHibernateTemplate().execute(getFindByIdCallback(ids));
}
}
[/code:1]

这里能取得范型传进来的类,this.supportsClass = GenericsUtils.getGeneric(getClass());
所有的返回类型和传入的类型都用范型实现。。子类只要继承。。

[code:1]
public class StudentManagerHibernate extends BaseManager<Student> implements
StudentManager {

}
[/code:1]
这样就行了,什么都有了,什么都不用作 findAll返回的是Student,其他方法传入的也是Student。。

至于GenericsUtils.getGeneric

[code:1]
    public static Class getGeneric(Class clazz) {
    return getGeneric(clazz, 1);
    }
   

    public static Class getGeneric(Class clazz, int index) {
        Type genType = clazz.getGenericSuperclass();

        if (genType instanceof ParameterizedType) {
            Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

            if ((params != null) && (params.length >= index)) {
                return (Class) params[index-1];
            }
        }
        return null;
    }
[/code:1]

这样就能在DAO初始化的时候取得范型传入的类型了。


具体看法看acegi友情提供的,Service支持类,有很多有用的东西。。还有很多用spring出神入化的方法。
   
0 请登录后投票
最后更新时间:2006-04-28
很想使用 范型……
但是总顾虑着,现在好多地方都是用java1.4。
代码拿过去不能用怎么办。
   
0 请登录后投票
最后更新时间:2006-04-28
这个不用担心,可以用retroweaver,把1.5的class,重新包装成1.4兼容的class。直接在jdk1.4上门run
   
0 请登录后投票
最后更新时间:2006-04-29
差沙 写道
[code:1]

    public BaseDAOHibernate() {
this.supportsClass = GenericsUtils.getGeneric(getClass());
if (this.supportsClass == null) {
if (logger.isWarnEnabled()) {
logger.warn("Could not determine the generics type - you will need to set manually");
}
}
}
[/code:1]



这样也许会好些
[code:1]
public BaseHibernateDaoSupport() {
this.supportsClass = GenericsUtils.getGeneric(getClass());
if (this.supportsClass == null) {
throw new RuntimeException(" concreate class must provide entity type ! ");
}
}
[/code:1]


[code:1]
public BaseHibernateDaoSupport() {
this.supportsClass = GenericsUtils.getGeneric(getClass());
if (this.supportsClass == null) {
setSupportClass();
}
}

/**
* concreate class can overide this method to provide support class
*
*/
protected void setSupportClass() {
}
[/code:1]

还有你的那个 GenericsUtils 好像不是很健壮吧
[code:1]
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

if ((params != null) && (params.length >= index)) {
                return (Class) params[index-1];
            }
[/code:1]

这两句都比较危险
   
0 请登录后投票
最后更新时间:2006-04-29
acegi里面原封不动copy过来的,没有仔细看。。

你是说会classcast?我不太了解不会会产生casr异常,但是用到现在还没有什么问题。
   
0 请登录后投票
最后更新时间:2006-04-30
这一句
[code:1]
Type genType = clazz.getGenericSuperclass(); [/code:1]
如果子类没使用泛型,  得到的是子类的类型而不是参数类型,  那么下一句
[code:1]if (genType instanceof ParameterizedType) { [/code:1] 就不会执行

如果子类使用泛型,  且类型参数是一个数组, 那么这一句
[code:1]return (Class) params[index-1];[/code:1]
是 classcast exception,  因为 params 里面的类型是 GenericArrayType.

我只试了以上两种情况,  不排除还会有其他问题
   
0 请登录后投票
最后更新时间:2006-05-13
在jdk中会有好多编译警告:类型安全,类型XX的表达式需要进行未经检查的转换以符合XX<X>,比如
比如 在类BaseHibernateDao中函数
public List<T> find(String hsql) {

return getHibernateTemplate().find(hsql);
}就会报告:
类型List的表达式需要进行未经检查的转换以符合List<T>
很多类似的编译警告都有
   
0 请登录后投票
最后更新时间:2006-05-25
可以用@SuppressWarnings("unchecked") 干掉警告。

这几天想出来的 给 IBatis用的GenericDAO。

打算另外用Freemarker来生成Sql.xml和Pojo。
Interface
[code:1]import java.util.List;

public interface GenericDAO<T, PK> {

List<T> selectAll();

List<T> selectAtRange(PK id, int begin, int end);

void create(T obj);

T get(PK id);

void update(T obj);

void delete(PK id);

}[/code:1]
   
0 请登录后投票
论坛首页 Java版 DAO

跳转论坛:
JavaEye推荐