当我们需要进行模糊查询时候 一般会遇到一些问题
例如 sql 拼接时候 出现了
select * from student
<if test="sname !=null and sname !=''">
and sname like '%${sname}%'
</if>
<if test="sage !=null and sage !='' ">
and sage like '%${sage}%'
</if>
第一个and的处理 则很关键。<trim>就为我们提供了这一问题的解决。
下面是StudentMapper.xml 我们来写下sql语句
实现模糊查询的三种方式:
#{} :自动附加 ‘ ’
${}:传什么值就替换什么值
bind:做绑定 类似于System.out.println();中的拼接
<select id="queryStudentWithONGL" parameterType="student"
resultType="student">
select * from student
<trim prefix="where" prefixOverrides="and">
<!-- bind实现模糊查询 -->
<bind name="_queryName" value="'%'+sname+'%'"/>
<if test="sname !=null and sname !=''">
and sname like #{_queryName}
</if>
<if test="sage !=null and sage !='' ">
and sage like '%${sage}%'
<