最近在做mybatis的模糊查询,贡献代码片段以表对CSDN的衷心:
需求:
根据用户在页面输入的若干值,进行查询动态拼接,实现动态(模糊)查询的功能
实现:
在mapper文件中配置dao接口实现配置:
<!-- 广告位一览-动态查询广告位 -->
<select id="selectAdvert" parameterType="com.glmedia.advert.pojo.Advertisement" resultType="com.glmedia.advert.pojo.Advertisement" >
select a.*,d1.name as stateName,d2.name as typeName from ta_advertisement a LEFT JOIN ta_dictionary d1 on a.state = d1.id
LEFT JOIN ta_dictionary d2 on a.type = d2.id where a.delete1='1'
<if test="mediaNumber != '' and mediaNumber != null">
and a.media_number like '%${mediaNumber}%'
</if>
<if test="adName != '' and adName != null">
and a.ad_name like '%${adName}%'
</if>
<if test="type != null and type != ''">
and a.type like '%${type}%'
</if>
<if test="state != null and state != ''">
and a.state like '%${state}%'
</if>
ORDER BY a.createtime
</select>
注意:if标签里面为动态条件查询
效果图: