案例一:#{} 业务层加上%%
mapper.java
@Repository
public interface Demo{
long getCount(@Param("id")String id,@Param("keyword")String keyword);
}
mapper.xml
<select id="getCount" resultType="java.lang.Long">
SELECT COUNT(*),b.nickname
FROM tableA AS a
<if test="keyword != null and keyword.length()>0">
JOIN tableB AS b
ON b.id = a.id
AND b.nickname LIKE #{keyword}
</if>
<where>
<if test="id != null">
a.id = #{id}
</if>
</where>
</select>
案例二:CONCAT('%',#{keyword},'%')
Demo.java
public interface Demo{
List<String> getFriendWxIdByWcId(@Param("wcId") String wcId,@Param("keyword") String keyword,@Param("offset") Integer offset,

本文介绍了在Mybatis中进行模糊查询的两种方法:一种使用#{}和%%,另一种利用CONCAT函数结合条件构建模糊查询。通过示例展示了在业务层和Mapper文件中的具体实现。
最低0.47元/天 解锁文章
3355

被折叠的 条评论
为什么被折叠?



