记录在mybatis中使用LOWER()与LIKE实现模糊查询并且不区分大小写

    <select id="commonSearch" resultType="com.ccc.interfaces.web.vo.DocumentSearchVo">
        select
            di.id,
            di.name,
            di.type,
            di.document_category_id,
            dc.name documentCategoryName,
            di.ascription_id,
            di.attachment_type,
            di.urls,
            di.update_time
            from
            document_info di
        left join
            document_category dc on
            di.document_category_id = dc.id
        <where>
            <if test="searchDto.type != null">
                and di.type = #{searchDto.type}
            </if>
            <if test="searchDto.condition != null and searchDto.condition != ''">
                AND (LOWER(di.name) LIKE CONCAT('%', LOWER(#{searchDto.condition}), '%')
                OR
                LOWER(dc.name) LIKE CONCAT('%', LOWER(#{searchDto.condition}), '%'))
            </if>
        </where>
        ORDER BY di.update_time DESC
    </select>

使用LOWER()函数可实现。

索引优化:

如果使用 LOWER(),建议创建函数索引:

sql
CREATE INDEX idx_di_name_lower ON document_info (LOWER(name));
CREATE INDEX idx_dc_name_lower ON document_category (LOWER(name));
如果使用 ILIKE,可尝试使用 text_pattern_ops 运算符类索引:

sql
CREATE INDEX idx_di_name_ilike ON document_info (name text_pattern_ops);
字符集兼容性:

ILIKE 默认依赖数据库的排序规则(collation),若需特殊字符(如中文)支持,需确保数据库使用 UTF8 编码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值