Mysql ibatis 模糊查询

真实代码摘录 :

<isNotEmpty prepend="AND" property="companyName"> 
					<![CDATA[ companyName like concat('%',#companyName#,'%' )]]>
			 </isNotEmpty> 

 

 

网上文章方案:

 

ibatis中使用like模糊查询
 
无效的方法:
?
1
2
Sql代码 
select  from table1 where name like '%#name#%'

 

两种有效的方法:
1. 使用$代替#。此种方法就是去掉了类型检查,使用字符串连接,不过可能会有sql注入风险。
?
1
2
Sql代码 
select  from table1 where name like '%$name$%'

 

 
2.使用连接符。不过不同的 数据库中方式不同。
?
1
2
3
4
5
6
7
8
9
mysql:
Sql代码 
select  from table1 where name like concat( '%' , # name #, '%'
oracle:
Sql代码 
select  from table1 where name like '%' || # name # || '%' 
  sql server:
Sql代码 
select  from table1 where name like '%' + # name # + '%'

 

  
注意:在实际开发中,往往我们需要将模糊查询的空格去掉。为了防止将去除空格放到业务层去,因此我建议如下写(oracle 中,其他数据库雷同):
?
1
2
Sql代码 
select  from table1 where name like '%' || Trim(# name #) || '%' 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值