您的位置:首页 > 其它

上移、下移功能

2016-01-10 20:42 387 查看
实现数据的上移和下移功能

实现效果如下:



功能实现过程为:上移和下移是交换两条数据的位置序号。我将主要业务放在Service,具体操作放在Dao中。

上移的Service层代码:

public void moveUp(Long id) {
//找出相关的Forum:当前需要移动的板块
Forum forum = getById(id);
//当前需要移动的上一个板块
Forum other = forumDao.createLimitQueryUp(forum.getPosition());
//最上面的不能上移
if(other == null){
return;
}
//交换position的值
int temp = forum.getPosition();
forum.setPosition(other.getPosition());
other.setPosition(temp);
//更新到数据库中
forumDao.update(forum);
forumDao.update(other);
}
上移的Dao层代码:

/**
* 限定查询:板块向上移动.
* @param position
* @return
*/
public Forum createLimitQueryUp(int position) {
Forum forum = (Forum) getSession().createQuery("from Forum f where f.position<? order by f.position desc")//
.setParameter(0, position).setFirstResult(0).setMaxResults(1).uniqueResult();
return forum;
}


下移的Service层代码:

public void moveDown(Long id) {
//找出相关的Forum:当前需要移动的板块
Forum forum = getById(id);
//当前需要移动的下一个板块
Forum other = forumDao.createLimitQueryDown(forum.getPosition());
//最下面的不能下移
if(other == null){
return;
}
//交换position的值
int temp = forum.getPosition();
forum.setPosition(other.getPosition());
other.setPosition(temp);
//更新到数据库中
forumDao.update(forum);
forumDao.update(other);
}


下移的Dao层代码:

/**
* 限定查询:板块向下移动
* @param position
* @return
*/
public Forum createLimitQueryDown(int position) {
Forum forum = (Forum) getSession().createQuery("from Forum f where f.position>? order by f.position asc")//
.setParameter(0, position).setFirstResult(0).setMaxResults(1).uniqueResult();
return forum;
}


这些是实现上下移动的主要代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: