In MyBatis, you can use dynamic SQL statements to move data up, down and to the top.

1. Move up: Use the ASC or DESC keyword in MySQL's ORDER BY clause to implement sorting, then use the LIMIT clause to limit the size of the query result set, and finally use the OFFSET clause to move the element at the specified position to the beginning of the list.

<update id="moveUp" parameterType="int">
    UPDATE tableName
    SET sort = sort - 1
    WHERE sort > #{
    
    id}
    ORDER BY sort ASC
    LIMIT 1
</update>

Among them, #{id} is the sorting value of the element to be moved up.

2. Move down: Similar to move up, you only need to change ASC in the ORDER BY clause to DESC.

<update id="moveDown" parameterType="int">
    UPDATE tableName
    SET sort = sort + 1
    WHERE sort < #{
    
    id}
    ORDER BY sort DESC
    LIMIT 1
</update>

Among them, #{id} is the sorting value of the element to be moved down.

3. Top: Also use the ASC or DESC keyword in the ORDER BY clause to implement sorting, then use the LIMIT clause to limit the size of the query result set, and set the OFFSET clause to 0.

<update id="moveTop" parameterType="int">
    UPDATE tableName
    SET sort = sort - 1
    WHERE sort < #{
    
    id}
    ORDER BY sort ASC
    LIMIT 1
</update>

Among them, #{id} is the sorting value of the element to be pinned.

4. Bottom: Use the DESC keyword in the ORDER BY clause of MySQL to implement sorting, then use the LIMIT clause to limit the size of the query result set, and finally use the OFFSET clause to move the element at the specified position to the end of the list.

<update id="moveBottom" parameterType="int">
    UPDATE tableName
    SET sort = sort + 1
    WHERE sort > #{
    
    id}
    ORDER BY sort DESC
    LIMIT 1
</update>

Among them, #{id} is the sorting value of the element to be placed at the bottom.

Guess you like

Origin blog.csdn.net/qq_49641620/article/details/133178190