现在的项目用的是mybatis3,不是ibatis,大同小异的。没什么。
今天在用like作模糊查询时,遇到小小的问题,在此作个小结。
记得以前ibatis时,可以这样写的:select * from Student where sname like '%$sname$%'
为了安全,也不提倡这种写法了。在网上搜了一大把都说下面的写法:
select * from Student where sname like '%'||#{username}||'%',可是我怎么试都不行,不知是什么原因,由于项目又比较紧,也没多时间去研究这种写法。
后来,在一个博客里看到下面这种写法,居然通过了。
select * from Student where sname like CONCAT(CONCAT('%', #{username}),'%')
用了Concat函数!大家可以去研究一下Concat函数。