Java实现列表数据的上移和下移总结

列表页面上移、下移功能

主要思路:一般涉及到排序功能,数据库都会新增一个sort为int型的字段,新增一条数据时,sort取当前表里sort最大值再加1,上移功能,主要是拿当前需要移动数据的id,找到上一条记录,交换sort,再更新数据库这两条数据;下移反之

controller

 public Object changeSort(String sort,String linkFlow){
        if(StringUtil.isNullOrEmpty(linkFlow)){
           return ReusltData201(GlobalConstant.OPERATE_FAIL);
        }
        int i = 0;
        if("up".equals(sort)){
            i = portalLinkBiz.moveUp(linkFlow);

        }else{
            i = portalLinkBiz.moveDown(linkFlow);
        }
        if(i <= 0){
            return ReusltData201("当前位置已在最上或最下,无法上移或下移");
        }else {
            return ReusltData(GlobalConstant.OPERATE_SUCCESSED);
        }
    }

impl 上移

public int moveUp(String linkFlow) {
        SkillFriendshipLinks friendshipLinks = friendshipLinksMapper.selectByPrimaryKey(linkFlow);
        //获取上一条数据(sortNo比它大的)
        SkillFriendshipLinks moveUp = tjLinkMapper.getMoveUp(friendshipLinks.getSortNo());
        if(null == moveUp){
            return  -1;
        }
        //交换两者的sortNo
        int temp = friendshipLinks.getSortNo();
        friendshipLinks.setSortNo(moveUp.getSortNo());
        moveUp.setSortNo(temp);
        int i = tjLinkMapper.updateSortNo(friendshipLinks);
        int j = tjLinkMapper.updateSortNo(moveUp);
        return i+j;
    }

impl 下移

 public int moveDown(String linkFlow) {
        SkillFriendshipLinks friendshipLinks = friendshipLinksMapper.selectByPrimaryKey(linkFlow);
        //获取下一条数据(sortNo比它小的)
        SkillFriendshipLinks moveDown = tjLinkMapper.getMoveDown(friendshipLinks.getSortNo());
        if(null == moveDown){
            return  -1;
        }
        //交换两者的sortNo
        int temp = friendshipLinks.getSortNo();
        friendshipLinks.setSortNo(moveDown.getSortNo());
        moveDown.setSortNo(temp);
        int i = tjLinkMapper.updateSortNo(friendshipLinks);
        int j = tjLinkMapper.updateSortNo(moveDown);
        return i+j;
    }

最后最重要的mybatis

<!-- 获取上条数据 -->
    <select id="getMoveUp" parameterType="java.lang.Integer" resultType="com.pinde.sci.model.mo.SkillFriendshipLinks">
        SELECT * FROM (
        SELECT LINK_FLOW,
               SORT_NO
         FROM SKILL_FRIENDSHIP_LINKS
         WHERE SORT_NO &gt; #{sortNo}
         AND RECORD_STATUS = 'Y'
         AND LINK_TYPE = 'Website'
         ORDER BY SORT_NO ASC
          ) WHERE ROWNUM = 1
    </select>
<!-- 获取下条数据 -->
    <select id="getMoveDown" parameterType="java.lang.Integer" resultType="com.pinde.sci.model.mo.SkillFriendshipLinks">
        SELECT * FROM (
        SELECT LINK_FLOW,
               SORT_NO
         FROM SKILL_FRIENDSHIP_LINKS
         WHERE SORT_NO &lt; #{sortNo}
         AND RECORD_STATUS = 'Y'
         AND LINK_TYPE = 'Website'
         ORDER BY SORT_NO DESC
          ) WHERE ROWNUM = 1
    </select>

数据库更新排序

<!-- 更新排序 -->
    <update id="updateSortNo" parameterType="com.pinde.sci.model.mo.SkillFriendshipLinks">
        UPDATE SKILL_FRIENDSHIP_LINKS
            SET SORT_NO = #{sortNo,jdbcType=DECIMAL}
            WHERE LINK_FLOW = #{linkFlow}
    </update>

你可能感兴趣的:(java)