mybatis中llike模糊查询中#和$的使用
来源:互联网 发布:java url重写技术 编辑:程序博客网 时间:2024/06/16 18:07
在mybatis中经常要写到like 查询,关于#和$的使用的。总结如下:
name like 表达式 and falg=#{falg}
四种衍生:
1. 表达式: name like "%"#{name}"%"
2. 表达式: name like '%${name}%'
3. 表达式: name like concat(concat('%',#{username}),'%')
4. 表达式:name like '%'||#{name}||'%'
本次示例中共两个条件,一个是name like 表达式, 还有flag相等,这个是使用#{}占位符,没有任何问题,关键问题就是 表达式的书写.下面来研究下表达式的书写:
如果写成'%#{name}%' ,就会报错Parameter index out of range (2> number of parameters, which is 1). 这个错误,就是参数的问题,所以就查阅了一下资料,然后结合自己的实践,得到总结如下:
本次使用 mysql 5.5.27和mybatis3.2.7进行测试
1.表达式: name like "%"#{name}"%"
==> Preparing: select * from bbs_brand WHERE namelike "%"?"%"and falg=? limit 0 , 10
==>Parameters: 莲(String), 1(Integer)
能够查询出来,没有问题,这是使用了占位符来占位,写成SQL就是: name like "%"'莲'"%" 没有问题
2.表达式: name like '%${name}%'
Preparing:select count(0) from (select * from bbs_brand WHERE name like '%莲%' and falg=?) as total
Parameters: 1(Integer)
使用$进行字符串的拼接,直接把传入的值,拼接上去了,没有任何问题
3. 表达式: name like concat(concat('%',#{username}),'%')
==> Preparing: select count(0) from (select *from bbs_brand WHERE name like
concat(concat('%',?),'%') and falg=?) as total
==>Parameters: 莲(String), 1(Integer)
这是使用了cancat进行字符串的连接,同时使用了#进行占位
转换成SQL就是: name like CONCAT(CONCAT('%','莲'),'%')
3. 表达式:name like CONCAT('%','${name}','%')
==> Preparing: select count(0) from (select *from bbs_brand WHERE name like CONCAT('%','莲','%') and falg=?) astotal
==>Parameters: 1(Integer)
对上面的表达式进行了简化,更方便了
4. 表达式:name like '%'||#{name}||'%'
这个不能满足要求,直接把数据库中的所有数据查询出来了,不符合我的要求,在mysql中||代表是or的意思
==> Preparing: select count(0) from (select *from bbs_brand WHERE name like '%'||?||'%' and falg=?) as total
==>Parameters: 莲(String), 1(Integer)
原文链接:http://www.2cto.com/database/201704/633355.html
- 关于mybatis中llike模糊查询中#和$的使用
- mybatis中llike模糊查询中#和$的使用
- Mybatis中使用like和%模糊查询
- MyBatis中模糊查询
- Mybatis中模糊查询
- Mybatis中模糊查询
- mybatis中模糊查询
- Mybatis中使用like模糊查询
- mybatis 中使用模糊查询语句
- Mybatis中模糊查询的各种写法
- Mybatis中模糊查询的各种写法
- Mybatis中模糊查询的各种写法
- Mybatis中模糊查询的各种写法
- Mybatis中模糊查询的各种写法
- Mybatis中模糊查询的各种写法
- Mybatis中模糊查询的各种写法
- Mybatis中模糊查询的各种写法
- Mybatis中模糊查询的写法总结
- 如何下载通过 jitpack 发布的 jar
- centos 7 安装java redis
- html5 video 手机端视频播放全屏显示
- poj 3525 多边形内核,缩进
- 基础dp
- mybatis中llike模糊查询中#和$的使用
- yarn平台的内存分配和设置
- vue相关的开源项目
- 数据库索引的实现原理
- 创建私有Pods
- 简单易学的机器学习算法——Mean Shift聚类算法
- 浅谈三种特殊进程:孤儿进程,僵尸进程和守护进程
- nyoj756 重建二叉树
- win10应用商店无法下载应用