一、背景
有这样一个需求,需要模糊匹配一个List<String>
的所有元素,如果采用LIKE
则需要写很多个语句,这时候其实可以采用正则匹配
二、代码
List<Student> selectStrdentByNameList(@Param("names") List<String> names);
<select id="selectStrdentByNameList" resultType="com.example.bootmptest.domain.Student">
select *
from student
WHERE
<foreach collection="names" item="name" open="name REGEXP CONCAT_WS('|'," close=")" separator=",">
#{name}
</foreach>
</select>
实际运行sql:
学习来自:(47条消息) Mybatis使用正则模糊匹配多个数据_mabatis的sql参数接收正则匹配_颤抖吧腿子的博客-CSDN博客