直接上sql语句的写法:
sql语句的写法:
<select id = "XXX(方法名)" parameterType = "hashmap" resultMap = "BaseResultMap">
select * from table(table写自己的表名称)
where 1 = 1
<if test="state != null and state != ' ' ">
and state = #{state,jdbcType = INTEGER}
</if>
<if test="number != null and number !=' ' ">
and phone like concat ('%',#{number,jdbcType=VARCHAR})
</if>
order by XX(根据某个字段排序) desc
<if test ="beginIndex != null and beginIndex != -1 ">
limit #{beginIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
</if>
</select>其中parameterType = "hashmap" 表示通过map集合来传递参数,resultMap = "BaseResultMap"表示返回map形式的数据,如果不知道怎么通过map封装,可以参考我前两篇文章。
if 里面的state和number字段是封装到map集合中的key值,and 后面的state和phone是数据库的字段,phone表示电话号码
phone like concat ('%',#{number,jdbcType=VARCHAR}) 表示查询后四位和number相等的手机号,
而后面再加一个‘%’,phone like concat ('%',#{number,jdbcType=VARCHAR},'%')表示查询包含number的手机号