MyBatis动态sql中模糊查询

来源:互联网 发布:程序员高级证书如何考 编辑:程序博客网 时间:2024/05/12 17:41

1、直接拼接%%:

<select id="getActiveDatas" parameterType="java.util.Map" resultType="java.util.Map">

  select * from tab_test where is_deleted = 0
  <if test="frameNo != null and frameNo != '%%'">
  and frame_no like '%${frameNo}%'
  </if>
  </select>

2、定义 一个<bind/>变量:

<select id="getActiveDatas" parameterType="java.util.Map" resultType="java.util.Map">

  <bind name="frameNo" value="'%' + _parameter.frameNo + '%'" />
  select <include refid="textDatas"/> from tab_test where is_deleted = 0
  <if test="frameNo != null and frameNo != '%%'">
  and frame_no like #{frameNo}
  </if>

</select>

3、使用locate()函数:

<select id="getActiveDatas" parameterType="java.util.Map" resultType="java.util.Map">

  select * from tab_test where is_deleted = 0
  <if test="frameNo != null and frameNo != ''">
  and LOCATE(#{frameNo}, frame_no ) > 0
  </if>
 </select>

4、使用instr()函数:

<select id="getActiveDatas" parameterType="java.util.Map" resultType="java.util.Map">

  select * from tab_test where is_deleted = 0
  <if test="frameNo != null and frameNo != ''">
  and INSTR(frame_no , #{frameNo})
  </if>
 </select>