xml中like的几种写法(模糊查询)

方法1:concat

<where>
    <trim  suffixOverrides="," >
        <if test="id != null and id != ''" >
            and id =  #{id}
        </if>
        <if test="name != null and name != ''" >
            and name like concat('%',#{name},'%')
        </if>
    </trim>
</where>
方法2:${}

<if test="examTypeName!=null and examTypeName!=''">
    and exam_type_name like '%${examTypeName}%'
</if>
方法3:#{}

<if test="examTypeName!=null and examTypeName!=''">
    and exam_type_name like '%' || #{examTypeName} || '%'

第二种可以,但是$有sql注入的风险,不推荐使用;

第一种在mysql才有用,Oracel中不可行。

第三种可以用于Oracle.

补充:

   <if test="_databaseId == 'postgre'">'%'||#{vendorName}||'%'</if>
<if test="_databaseId == 'oracle'">'%'||#{vendorName}||'%'</if>
<if test="_databaseId == 'mssql'">'%'+#{vendorName}+'%'</if>
<if test="_databaseId == 'mysql'">concat('%',#{vendorName},'%')</if>

猜你喜欢

转载自blog.csdn.net/wys0127/article/details/131481424