Mybatis分页如何将limit替换成top语句?

Mybatis分页如何将limit替换成top语句?

问题描述:

源代码

<select id="findAll" parameterType="Map" resultMap="GoodsResult">
    select * from sm_goods 
    <where>
    <if test="name!=null and name!=''">
        and name like "%"#{name}"%"
    </if>
    <if test="type_id!=null and type_id!=''">
        and type_id =#{type_id}
    </if>
    <if test="state!=null">
        and state =#{state}
    </if>
    </where>
    <if test="page!=null and limit!=null">
         limit ${page},${limit}
    </if>
</select>

自己修改过的但没成功的代码

<!-- 查询所有商品 -->
    <select id="findAll" parameterType="Map" resultMap="GoodsResult">
        select * from sm_goods 
        <where>
        <if test="name!=null and name!=''">
            and name like "%"#{name}"%"
        </if>
        <if test="type_id!=null and type_id!=''">
            and type_id =#{type_id}
        </if>
        <if test="state!=null">
            and state =#{state}
        </if>
        </where>
        <if test="page!=null and limit!=null">
            top ${page},${limit}
        </if>
    </select>

现在用的是sqlserver数据库,之前是mysql数据库。所以现在要将limit 换成top

你用的什么数据库,mysql不支持top . mssql才支持。而且是放在select 后面 例如select top 10.。。语法都不对

where后面order by一下,然后后面再加limit,就是你要的数据

<select id="findAll" parameterType="Map" resultMap="GoodsResult">
    select 
            <if test="page!=null and limit!=null">
        top ${page},${limit}
    </if>
            * from sm_goods 
    <where>
    <if test="name!=null and name!=''">
        and name like "%"#{name}"%"
    </if>
    <if test="type_id!=null and type_id!=''">
        and type_id =#{type_id}
    </if>
    <if test="state!=null">
        and state =#{state}
    </if>
    </where>

</select>