DAO设计问题

最近由于项目限制使用JDK1.4(没有办放使用泛型DAO)所以设计了一个DAO如下


public interface Dao {
public abstract List getList(int first,int max) throws Exception;
public abstract int getTotal()throws Exception;
}

public abstract class AbsDao extends HibernateDaoSupport implements Dao {

/**
*@return List 以集合形式返回符合HQL语句中实体
*@param int max 最大记录数
*@param int first 第一条记录的位置
*/
public List getList(final int first,final int max)throws Exception{

return super.getHibernateTemplate().executeFind(
new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException,SQLException{
try{
return session.createQuery("from "+getTableName()).setFirstResult(first).setMaxResults(max).list();
}
catch(Exception e){
throw new HibernateException(e);
}
}
}
);
}

/**
* @return int count 返回HQL查询的记录数
* @param String HQL 统计的HQL语句
*/
public int getTotal() throws Exception {
return ((Integer)super.getSession().createQuery("select count(*) from "+getTableName()).uniqueResult()).intValue();
}

/**
* @return String TableName
* */
private String getTableName()throws Exception{
return this.getClass().getName().substring(this.getClass().getName().lastIndexOf(".")+1, this.getClass().getName().length()-3);
}
}


///这个是工具生成的修改了继承,使得为分页作准备。
public class UserinfoDAO extends AbsDao {}


//这个是伪泛型的DAO
public class DaoImpl extends AbsDao{
/**
*@return List 以集合形式返回符合HQL语句中实体
*@param String HQL 要执行的HQL语句
*/
public List getList(String HQL)throws Exception{
return super.getHibernateTemplate().find(HQL);
}

/**
*将Object写入数据库
*@param Object o 要保存的对象
*/
public void save(Object o)throws Exception{
super.getHibernateTemplate().save(o);
}

/**
*将List(Object)写入数据库
*@param List list(Object) 要保存的对象数组列表
*/
public void saveAll(List list)throws Exception{
super.getHibernateTemplate().save(list);
}

/**
*将Object更新
*@param Object o 要跟新的对象
*/
public void update(Object o)throws Exception{
super.getHibernateTemplate().update(o);
}


/**
*将Object更新
*@param List list(Object) 要更新的对象数组列表
*/
public void updateAll(List list)throws Exception{
super.getHibernateTemplate().update(list);
}


/**通过SQL语句直接操作数据库
* @param String HQL;
*/
public void excuteSQL(String HQL) throws Exception {
super.getHibernateTemplate().find(HQL);
}

}



服务层

public interface PageService{
public abstract int getPageSize()throws Exception;
public abstract void setPageSize(int pageSize)throws Exception;

public abstract int getTotal() throws Exception;
public abstract int getPageCount()throws Exception;
public abstract List getFirstPage() throws Exception;
public abstract List getLastPage() throws Exception;
public abstract List getPreviousPage(int currentPage)throws Exception;
public abstract List getNextPage(int currentPage)throws Exception;
public abstract List getPointPage(int pointPage)throws Exception;

}

public abstract class AbsPageService implements PageService {
/**
* @return int 数据库中记录总数
* */
public int getTotal() throws Exception {
return getDao().getTotal();
}

/**
*@return int totalpage 返回分页数
*@param int PageSize 页面的记录数
*/
public int getPageCount(int PageSize) throws Exception {
return ((getTotal()+PageSize)-1)/PageSize;
}

/**
*@return List 返回指定页面的记录 注意这个方法提供给接口方法
*@param int PointPage 指定的页面
*@param int PageSize 页面的记录数
*/
public List getPointPage(int PointPage, int PageSize) throws Exception {
if(PointPage>=0&&PointPage<=getPageCount(PageSize)){
return getDao().getList((PointPage-1)*PageSize, PageSize);
}
else{
return null;
}
}

//以下是接口方法
/**
*@return List 返回第一页的记录
**/
public List getFirstPage() throws Exception{
return this.getPointPage(1, PageSize);
}

/**
* @return List 返回最后一页
* */
public List getLastPage() throws Exception{
return this.getPointPage(this.getPageCount(PageSize), PageSize);
}

/**
* @return List 返回前一页的数据
* @param int currentPage 当前页
* */
public List getPreviousPage(int currentPage)throws Exception{
if(currentPage>1&&this.getTotal()>currentPage){
return this.getPointPage(currentPage-1, PageSize);
}
else{
return null;
}
}

/**
* @return List 返回后一页的数据
* @param int currentPage 当前页
* */
public List getNextPage(int currentPage)throws Exception{
if(this.getTotal()>=(currentPage+1)&&(currentPage+1)>0){
return this.getPointPage(currentPage+1, PageSize);
}
else{
return null;
}
}

/**
* @return List 返回当前页的数据
* @param int currentPage 当前页
* */
public List getPointPage(int pointPage)throws Exception{
if(this.getTotal()>=(pointPage)&&(pointPage)>0){
return this.getPointPage(pointPage, PageSize);
}
else{
return null;
}
}

/**
* @return int 总页面数
* */
public int getPageCount() throws Exception {
return this.getPageCount(this.getPageSize());
}
//注入方法
public int getPageSize() {
return PageSize;
}

public void setPageSize(int pageSize) {
PageSize = pageSize;
}

public Dao getDao() {
return dao;
}

public void setDao(Dao dao) {
this.dao = dao;
}

private int PageSize=10;
private Dao dao;
}

public interface UserinfoService {
public abstract void regist(Userinfo ui)throws Exception;
public abstract void delete(Integer userinfoid)throws Exception;
public abstract void update(Userinfo ui)throws Exception;
public abstract Map manager(String pointPage,String operator)throws Exception;
public abstract Userinfo find(Integer userinfoid)throws Exception;

}

public class UserinfoServiceImpl extends AbsPageService implements PageService,UserinfoService{
/**
* 用户的删除方法
* @param Integer userinfoid
* */
public void delete(Integer userinfoid) throws Exception {
((UserinfoDAO)super.getDao()).delete(((UserinfoDAO)super.getDao()).findById(userinfoid));
}

/**
* 用户的查找方法
* @return Userinfo
* @param Integer userinfoid
* */
public Userinfo find(Integer userinfoid) throws Exception {
return ((UserinfoDAO)super.getDao()).findById(userinfoid);
}

/**
* 用户的管理方法
* @param String pointPage 当前页
* @param String operator 操作
* */
public Map manager(String pointPage,String operator) throws Exception {
this.setAph(new PageHold());
this.getAph().setPs(this);
return this.getAph().getUpsPage(pointPage, operator);
}

/**
* 用户的注册方法
* @param Userinfo userinfo
* */
public void regist(Userinfo ui) throws Exception {
((UserinfoDAO)super.getDao()).save(ui);
}

/**
* 用户的更新方法
* @param Userinfo userinfo
* */
public void update(Userinfo ui) throws Exception {
((UserinfoDAO)super.getDao()).merge(ui);
}

public AbsPageHold getAph() {
return aph;
}

public void setAph(AbsPageHold aph) {
this.aph = aph;
}

private AbsPageHold aph;

}



工具类

public abstract class AbsPageHold {
/**
* 获取页面的逻辑
* @return Map map
* @param String pointPage 当前页
* @param String operator 操作符
* */
public Map getUpsPage(String pointPage,String operator)throws Exception{
Map map=null;
try{
if(operator==null){
map=this.getFirstPage();
}
else if(operator.equals("first")){
map=this.getFirstPage();
}
else if(operator.equals("previous")){
map=this.getPreviousPage(pointPage);
}
else if(operator.equals("next")){
map=this.getNextPage(pointPage);
}
else if(operator.equals("jump")){
map=this.getJumpPage(pointPage);
}
else if(operator.equals("last")){
map=this.getLastPage();
}
else{
map=this.getFirstPage();
}
}
catch(Exception e){
System.out.println(e);
}
return map;
}

/**
* 分页所需要的帮助类
* */
public Map getMap()throws Exception{
Map map=new HashMap();
map.put("totalCount",new Integer(this.getPs().getTotal()));
map.put("totalPage", new Integer(this.getPs().getPageCount()));
return map;
}

//模板方法
public abstract Map getFirstPage()throws Exception;
public abstract Map getLastPage()throws Exception;
public abstract Map getNextPage(String pointPage)throws Exception;
public abstract Map getPreviousPage(String pointPage)throws Exception;
public abstract Map getJumpPage(String pointPage)throws Exception;

//注入
public PageService getPs() {
return ps;
}
public void setPs(PageService ps) {
this.ps = ps;
}
private PageService ps;
}

public class PageHold extends AbsPageHold{

public Map getFirstPage() throws Exception {
Map map=super.getMap();
map.put("pointPage", "1");
map.put("list", this.getPs().getFirstPage());
return map;
}

public Map getJumpPage(String pointPage) throws Exception {
Map map=super.getMap();
int i=Integer.parseInt(pointPage);
List list=this.getPs().getPointPage(i);
if(list!=null){
map.put("pointPage", ""+(i));
map.put("list", list);
}
else{
map.put("pointPage", "1");
map.put("list", this.getPs().getFirstPage());
}

return map;
}

public Map getLastPage() throws Exception {
Map map=super.getMap();
map.put("pointPage",((Integer)map.get("totalPage")).toString());
map.put("list",this.getPs().getLastPage());
return map;
}


public Map getNextPage(String pointPage) throws Exception {
Map map=super.getMap();
int i=Integer.parseInt(pointPage);
List list=this.getPs().getNextPage(i);
if(list!=null){
map.put("pointPage", ""+(i+1));
map.put("list", list);
}
else{
map.put("pointPage", ""+this.getPs().getPageCount());
map.put("list", this.getPs().getLastPage());
}

return map;
}

public Map getPreviousPage(String pointPage) throws Exception {
Map map=super.getMap();
int i=Integer.parseInt(pointPage);
List list=this.getPs().getPreviousPage(i);
if(list!=null){
map.put("pointPage", ""+(i-1));
map.put("list", list);
}
else{
map.put("pointPage", "1");
map.put("list",this.getPs().getFirstPage());
}
return map;
}

}



现在问题是。我没有办法写一个通用的DAO接口实现CURD。
用伪泛型DAO无法解决注入问题。还有伪泛型DAO怎么处理SQL语句?放在Service层么?
似乎不怎么合适。
还有就是分页的时候PAGEHOLD(分页)和Userinfo相互交叉。
DTO传输应该放在什么地方?
等诸如此类的问题。请指点一下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值