最近在用mybatis做模糊查询的时候,这个‘%xx%’不知道怎么插入,直接写在sql语句里面没法实现,在网上查了一下主要有两种比较好的方式实现
1.使用<bind>标签
<select>
<bind name="pattern" value="'%'+_parameter+'%'">
select * from table where field like #{pattern}
</select>
注意:_paramter代指传入的参数,如果参数类型是基本数据类型或者它们对于的类类型,就直接写_paramter,如果是包装类型,写为_paramter.getter() getter()代指对应字段的get方法
2.使用concat
sql语句写为 select * from table where field like concat('%','${field}','%')
或 select * from table where field like concat('%',#{field},'%')