Mybatis的XxxMapper.xml中模糊查詢條件如何拼接

這裏舉一MySQL中模糊查詢例子:

select count(*) from `movie` where gids like concat('%',1,'%');

上面指令用於查詢movie表中gids字段帶有1的行數

在Mybatis中如何拼接這種模糊查詢字符串呢?

首先看下mapper接口中定義的方法:

    /**
     * 根據模糊條件查詢電影數量
     * @param gids 模糊搜索條件
     * @return
     */
    long getCountByGenre(@Param("gids")String gids);

那麼再看下MovieMapper.xml中關於該條查詢語句的模糊查詢:

方式①

    <select id="getCountByGenre" resultType="long">
        <!--使用concat構造模糊查詢條件-->
        select count(*) from `movie` where gids like concat('%',#{gids},'%');
    </select>

方式②

    <select id="getCountByGenre" resultType="long">
        <!--綁定模糊查詢條件-->
        <bind name="vague_gids" value="'%'+gids+'%'"/>
        select count(*) from `movie` where gids like #{vague_gids};
    </select>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章