DAO层重构
2015-12-24 10:28
316 查看
通用DAO层接口:ICommonDao:
通用Dao实现CommonDaoImpl:
具体的某个实体DAO:
对应的Dao实现:
注意:通用的Dao实现是一个抽象类,而且必须这么做;因为他不能有实例对象
![](http://b.bst.126.net/newpage/images/microblog.png?1)
转发至微博
![](http://b.bst.126.net/newpage/images/microblog.png?1)
转发至微博
package com.lrq.dao.base; import java.io.Serializable; import java.util.Collection; public interface ICommonDao<T> { public void persist(T t); public void delete(Serializable id); public void update(T t); public T get(Class<T> clazz,Serializable id); public Collection<T> listAll(); }
通用Dao实现CommonDaoImpl:
package com.lrq.dao.base.impl; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.Collection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateTemplate; import com.lrq.dao.base.ICommonDao; public abstract class CommonDaoImpl<T> implements ICommonDao<T> { private Class clazz; @Autowired public HibernateTemplate hibernateTemplate; public CommonDaoImpl() { ParameterizedType type = (ParameterizedType) this.getClass() .getGenericSuperclass(); clazz = (Class) type.getActualTypeArguments()[0]; } public void persist(T t) { if (t != null) hibernateTemplate.persist(t); } @SuppressWarnings("unchecked") public void delete(Serializable id) { if (id != null) { Object entity = hibernateTemplate.get(clazz, id); if (entity != null) hibernateTemplate.delete(entity); } } public void update(T t) { if (t != null) { hibernateTemplate.update(t); } } @SuppressWarnings("unchecked") public T get(Class<T> clazz, Serializable id) { return (T) (id == null ? null : hibernateTemplate.get(clazz, id)); } @SuppressWarnings("unchecked") public Collection<T> listAll() { return hibernateTemplate.find("from " + clazz.getSimpleName()); } }
具体的某个实体DAO:
package com.lrq.dao; import java.util.Collection; import com.lrq.dao.base.ICommonDao; import com.lrq.domain.Privilege; public interface IPrivilegeDao extends ICommonDao<Privilege> { public Privilege getPrivilegeByName(String nodeName); public Collection<Privilege> getPrivilegesByIds(String ids); }
对应的Dao实现:
package com.lrq.dao.impl; import java.util.Collection; import java.util.List; import org.springframework.stereotype.Repository; import com.lrq.dao.IPrivilegeDao; import com.lrq.dao.base.impl.CommonDaoImpl; import com.lrq.domain.Privilege; import com.lrq.util.StringUtils; import freemarker.template.utility.StringUtil; @Repository public class PrivilegeDaoImpl extends CommonDaoImpl<Privilege> implements IPrivilegeDao { @SuppressWarnings("unchecked") public Privilege getPrivilegeByName(String nodeName) { // 如果 入的参数都为null,我认为没必要和数据库进行交互,直接返回null; if (StringUtils.isEmpty(nodeName)) return null; List<Privilege> list = hibernateTemplate.find( "from Privilege where name=?", nodeName); return list != null && list.size() > 0 ? list.get(0) : null; } @SuppressWarnings("unchecked") public Collection<Privilege> getPrivilegesByIds(String ids) { if (!StringUtils.isEmpty(ids)) { StringBuffer sb = new StringBuffer(); sb.append("from Privilege where id in("); sb.append(ids); sb.append(" )"); System.out.println(sb.toString()); List<Privilege> list = this.hibernateTemplate.find(sb.toString()); return list; } return null; } }
注意:通用的Dao实现是一个抽象类,而且必须这么做;因为他不能有实例对象
![](http://b.bst.126.net/newpage/images/microblog.png?1)
转发至微博
![](http://b.bst.126.net/newpage/images/microblog.png?1)
转发至微博
相关文章推荐
- Action层重构
- service层重构的解决方案(三种解决…
- mybatis笔记
- Spring定时器的使用
- 数据结构课程设计——学生成绩管理系统
- struts总结完整整理版
- struts2总结:草稿
- Spring的总结
- struts2中默认拦截器栈中的拦截器…
- 动态代理模式--自定义数据源的终极…
- Git reset 回退项目某个节点版本
- kali postgress connection
- Ajax—初识
- 装饰设计模式---自定义数据源
- 读《结网》
- 关于tomcat服务器getParameter对ge…
- dom4j和xpath解析带名称空间的xml…
- 【转载】Linux下的javaee开发环境…
- oracle数据库之sql查询语句笔记
- oracle数据库笔记之数据库基本管理