mybatis实现模糊查询时%%的处理

背景:需要通过mybatis实现简单的字段like模糊查询,%如何处理

方法一:
完整范例查询语句如下:

<select id="queryRecord" resultType="db.model.Record"
            parameterType="java.lang.String">
        select a.recordId as recordID,a.name as name,
        TRIM(TRAILING '.' from b.name) as zoneName,a.content as content,d.name as envName
        from records as a
        left join zones as b on a.zoneID = b.zoneID
        left join (select distinct(zoneId),env from profile) as c on b.zoneID = c.zoneId
        left join t_enviroment as d on c.env = d.id
        where a.content like concat('%',#{searchText},'%') or
        concat(a.name,'.',TRIM(TRAILING '.' from b.name)) like concat('%',#{searchText},'%')
        order by a.recordId;
</select>

通过concat函数,将%和搜索关键字链接起来:

like concat('%',#{searchText},'%') 

方法二:
参数直接添加%,比如还是上面的查询:

// java代码
searchText = '%' +searchText + '%';
queryRecord(searchText);

查询语句:

<select id="queryRecord" resultType="db.model.Record"
            parameterType="java.lang.String">
        select a.recordId as recordID,a.name as name,
        TRIM(TRAILING '.' from b.name) as zoneName,a.content as content,d.name as envName
        from records as a
        left join zones as b on a.zoneID = b.zoneID
        left join (select distinct(zoneId),env from profile) as c on b.zoneID = c.zoneId
        left join t_enviroment as d on c.env = d.id
        where a.content like #{searchText}  
        or concat(a.name,'.',TRIM(TRAILING '.' from b.name)) like #{searchText}
        order by a.recordId;
</select>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值