增删改查几乎是所有Dao不可与数据库进行交互的不可避免的操作。在java jdk 1.5 以后提供了泛型可以实现大量重用Dao的增伤改查代码。 所以小编觉得有必要做个笔记。
1 泛型crud重用首先向要创建一个通用的泛型接口
package com.wuyihuai.oa.dao.base;
import java.io.Serializable;
import java.util.Collection;
/**
* 基类接口
* 这是注释
* @author wuyihuai
* 邮箱1049153718@qq.com
* 下午7:11:34
*/
public interface CommonDao<T> {
//保存
public void save(T t);
//查询所有
public Collection<T> getAllEntity();
//根据id查询
public T getEntityById(Serializable id);
//更新
public void updateEntity(T t);
//删除
public void deleteEntity(Serializable id);
}
2 创建基于泛型接口的实现类 并且继承HibernateDaoSupport重而使用hibernate框架 以实现基本的增伤改查。
package com.wuyihuai.oa.dao.base.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wuyihuai.oa.dao.base.CommonDao;
/**
* 实现基本
* 这是注释
* @author wuyihuai
* 邮箱1049153718@qq.com
* 下午7:11:06
*/
public class CommonDaoImlp<T> extends HibernateDaoSupport implements CommonDao<T>{
private Class classT;
public CommonDaoImlp(){
/**
* CommonDaoImpl<T> 是一个被参数化的类
* CommonDao<T> 是一个被参数化的接口
* ParameterizedType 是被参数化的类型 也就是返回带泛型的类
* 一个被参数化的类由两个部分组成 例如CommonDaoImlp<T>
* rawType: 代表CommonDaoImlp
* actualTypeArguments :代表T
* this.getClass 仅表 CommonDaoImpl 并不代表CommonDaoImpl<T>
* type.getActualTypeArguments()[0]返回的是一个数组 获取第一个泛行类
*/
//被参数化的类
ParameterizedType type= (ParameterizedType) this.getClass().getGenericSuperclass();
//获取泛型类
this.classT=(Class) type.getActualTypeArguments()[0];
System.err.println(classT.getName());
}
@Override
public void save(T t) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(t);
}
@Override
public Collection<T> getAllEntity() {
// TODO Auto-generated method stub
return this.getHibernateTemplate().find("from"+classT.getName());
}
@Override
public T getEntityById(Serializable id) {
// TODO Auto-generated method stub
return (T) this.getHibernateTemplate().get(classT, id);
}
@Override
public void updateEntity(T t) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(t);
this.getHibernateTemplate().flush();
}
@Override
public void deleteEntity(Serializable id) {
// TODO Auto-generated method stub
T t=(T) this.getHibernateTemplate().get(classT, id);
this.getHibernateTemplate().delete(t);
this.getHibernateTemplate().flush();
}
}
3 到此curd的重用便实现了,接下来便是调用使用该类 在使用该泛型时为实现MVC首先要 创建一个实体类的接口继承CommonDao<T>
package com.wuyihuai.oa.dao;
import java.io.Serializable;
import com.wuyihuai.oa.dao.base.CommonDao;
import com.wuyihuai.oa.domain.Person;
public interface PersonDao<T> extends CommonDao<T> {
/**
* 在此增加 除增删改查之外的操作。
* 本例中 只例举增删查改的操作
* 故该接口内不在添加新的操作
*/
}
4 接下来是创建PersonDao的实现类PersonDaoImpl 让其继承 CommonDaoImlp<T>和实现PersonDao<T>
便可以了
package com.wuyihuai.oa.dao.impl;
import java.io.Serializable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wuyihuai.oa.dao.PersonDao;
import com.wuyihuai.oa.dao.PostDao;
import com.wuyihuai.oa.dao.base.impl.CommonDaoImlp;
import com.wuyihuai.oa.domain.Person;
public class PersonDaoImpl extends CommonDaoImlp<Person> implements PersonDao<Person>{
}
这要便实现了泛型从用所有的Dao由原先每个类都要写一遍增删改查 ,变成就只写一遍提高了代码的重用性。
1 泛型crud重用首先向要创建一个通用的泛型接口
package com.wuyihuai.oa.dao.base;
import java.io.Serializable;
import java.util.Collection;
/**
* 基类接口
* 这是注释
* @author wuyihuai
* 邮箱1049153718@qq.com
* 下午7:11:34
*/
public interface CommonDao<T> {
//保存
public void save(T t);
//查询所有
public Collection<T> getAllEntity();
//根据id查询
public T getEntityById(Serializable id);
//更新
public void updateEntity(T t);
//删除
public void deleteEntity(Serializable id);
}
2 创建基于泛型接口的实现类 并且继承HibernateDaoSupport重而使用hibernate框架 以实现基本的增伤改查。
package com.wuyihuai.oa.dao.base.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wuyihuai.oa.dao.base.CommonDao;
/**
* 实现基本
* 这是注释
* @author wuyihuai
* 邮箱1049153718@qq.com
* 下午7:11:06
*/
public class CommonDaoImlp<T> extends HibernateDaoSupport implements CommonDao<T>{
private Class classT;
public CommonDaoImlp(){
/**
* CommonDaoImpl<T> 是一个被参数化的类
* CommonDao<T> 是一个被参数化的接口
* ParameterizedType 是被参数化的类型 也就是返回带泛型的类
* 一个被参数化的类由两个部分组成 例如CommonDaoImlp<T>
* rawType: 代表CommonDaoImlp
* actualTypeArguments :代表T
* this.getClass 仅表 CommonDaoImpl 并不代表CommonDaoImpl<T>
* type.getActualTypeArguments()[0]返回的是一个数组 获取第一个泛行类
*/
//被参数化的类
ParameterizedType type= (ParameterizedType) this.getClass().getGenericSuperclass();
//获取泛型类
this.classT=(Class) type.getActualTypeArguments()[0];
System.err.println(classT.getName());
}
@Override
public void save(T t) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(t);
}
@Override
public Collection<T> getAllEntity() {
// TODO Auto-generated method stub
return this.getHibernateTemplate().find("from"+classT.getName());
}
@Override
public T getEntityById(Serializable id) {
// TODO Auto-generated method stub
return (T) this.getHibernateTemplate().get(classT, id);
}
@Override
public void updateEntity(T t) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(t);
this.getHibernateTemplate().flush();
}
@Override
public void deleteEntity(Serializable id) {
// TODO Auto-generated method stub
T t=(T) this.getHibernateTemplate().get(classT, id);
this.getHibernateTemplate().delete(t);
this.getHibernateTemplate().flush();
}
}
3 到此curd的重用便实现了,接下来便是调用使用该类 在使用该泛型时为实现MVC首先要 创建一个实体类的接口继承CommonDao<T>
package com.wuyihuai.oa.dao;
import java.io.Serializable;
import com.wuyihuai.oa.dao.base.CommonDao;
import com.wuyihuai.oa.domain.Person;
public interface PersonDao<T> extends CommonDao<T> {
/**
* 在此增加 除增删改查之外的操作。
* 本例中 只例举增删查改的操作
* 故该接口内不在添加新的操作
*/
}
4 接下来是创建PersonDao的实现类PersonDaoImpl 让其继承 CommonDaoImlp<T>和实现PersonDao<T>
便可以了
package com.wuyihuai.oa.dao.impl;
import java.io.Serializable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.wuyihuai.oa.dao.PersonDao;
import com.wuyihuai.oa.dao.PostDao;
import com.wuyihuai.oa.dao.base.impl.CommonDaoImlp;
import com.wuyihuai.oa.domain.Person;
public class PersonDaoImpl extends CommonDaoImlp<Person> implements PersonDao<Person>{
}
这要便实现了泛型从用所有的Dao由原先每个类都要写一遍增删改查 ,变成就只写一遍提高了代码的重用性。