MyBatis做动态模糊查询时,like后面要不要加单引号??
做项目遇到了个奇怪的问题,项目里面要对商品、账户、进行分别的多条件查询,于是我就采用动态多条件分页查询,起初在做账户部分的时候Mybatis是这样写的
<!-- 动态多条件分页查询 -->
<select id="searchPageUseDyc" parameterType="page" resultMap="accountResultMap">
select acc_id,acc_login,acc_name,acc_pass
from account
<where>
<if test="paramsEntity.accId!=null">
and acc_id like #{paramsEntity.accId}
</if>
<if test="paramsEntity.accLogin!=null">
and acc_login like #{paramsEntity.accLogin}
</if>
<if test="paramsEntity.accName!=null">
and acc_name like #{paramsEntity.accName}
</if>
<if test="paramsEntity.accPass!=null">
and acc_pass like #{paramsEntity.accPass}
</if>
</where>
limit #{start},#{rows}
</select>
like 后面直接跟 #{paramsEntity.accName} 不需要添加单引号
然后完成商品查询的时候我一样写了一套
<!-- 动态分页多条件查询(找内容) -->
<select id="searchPageUseDyc" parameterType="page" resultMap="goodsResultMap">
select goods_Id,goods_name,goods_unit,goods_type,goods_color,goods_store,goods_limit,goods_commission,goods_producer,goods_remark,goods_sel_price,goods_buy_price
from goods
<where>
<if test="paramsEntity.goodsId!=null">
and goods_Id like ${paramsEntity.goodsId}
</if>
<if test="paramsEntity.goodsName!=null">
and goods_name like ${paramsEntity.goodsName}
</if>
<if test="paramsEntity.goodsUnit!=null">
and goods_unit like ${paramsEntity.goodsUnit}
</if>
<if test="paramsEntity.goodsType!=null">
and goods_type like ${paramsEntity.goodsType}
</if>
<if test="paramsEntity.goodsColor!=null">
and goods_color like ${paramsEntity.goodsColor}
</if>
<if test="paramsEntity.goodsStore!=null">
and goods_store like ${paramsEntity.goodsStore}
</if>
<if test="paramsEntity.goodsLimit!=null">
and goods_limit like ${paramsEntity.goodsLimit}
</if>
<if test="paramsEntity.goodsCommission!=null">
and goods_commission like ${paramsEntity.goodsCommission}
</if>
<if test="paramsEntity.goodsProducer!=null">
and goods_producer like ${paramsEntity.goodsProducer}
</if>
<if test="paramsEntity.goodsRemark!=null">
and goods_remark like ${paramsEntity.goodsRemark}
</if>
<if test="paramsEntity.goodsSelPrice!=null">
and goods_sel_price like ${paramsEntity.goodsSelPrice}
</if>
<if test="paramsEntity.goodsBuyPrice!=null">
and goods_buy_price like ${paramsEntity.goodsBuyPrice}
</if>
</where>
limit #{start},#{rows}
</select>
但是运行报错了!!!
错误信息You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%% limit 0,3' at line 3
然后我就给单引号添加上了,然后居然就成了,代码这样子
<!-- 动态分页多条件查询(找内容) -->
<select id="searchPageUseDyc" parameterType="page" resultMap="goodsResultMap">
select goods_Id,goods_name,goods_unit,goods_type,goods_color,goods_store,goods_limit,goods_commission,goods_producer,goods_remark,goods_sel_price,goods_buy_price
from goods
<where>
<if test="paramsEntity.goodsId!=null">
and goods_Id like '${paramsEntity.goodsId}'
</if>
<if test="paramsEntity.goodsName!=null">
and goods_name like '${paramsEntity.goodsName}'
</if>
<if test="paramsEntity.goodsUnit!=null">
and goods_unit like '${paramsEntity.goodsUnit}'
</if>
<if test="paramsEntity.goodsType!=null">
and goods_type like '${paramsEntity.goodsType}'
</if>
<if test="paramsEntity.goodsColor!=null">
and goods_color like '${paramsEntity.goodsColor}'
</if>
<if test="paramsEntity.goodsStore!=null">
and goods_store like '${paramsEntity.goodsStore}'
</if>
<if test="paramsEntity.goodsLimit!=null">
and goods_limit like '${paramsEntity.goodsLimit}'
</if>
<if test="paramsEntity.goodsCommission!=null">
and goods_commission like '${paramsEntity.goodsCommission}'
</if>
<if test="paramsEntity.goodsProducer!=null">
and goods_producer like '${paramsEntity.goodsProducer}'
</if>
<if test="paramsEntity.goodsRemark!=null">
and goods_remark like '${paramsEntity.goodsRemark}'
</if>
<if test="paramsEntity.goodsSelPrice!=null">
and goods_sel_price like '${paramsEntity.goodsSelPrice}'
</if>
<if test="paramsEntity.goodsBuyPrice!=null">
and goods_buy_price like '${paramsEntity.goodsBuyPrice}'
</if>
</where>
limit #{start},#{rows}
</select>
然后我就去查文档,光放文档给出的也是不用加单引号的!!
<select id=”findActiveBlogLike”
parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG
<where>
<if test=”state != null”>
state = #{state}
</if>
<if test=”title != null”>
AND title like #{title}
</if>
<if test=”author != null and author.name != null”>
AND title like #{author.name}
</if>
</where>
</select>
我的问题还真不知道出在哪里!!!奇了怪了,有空再去搞清楚吧 !!!!
MyBatis做动态模糊查询时,like后面要不要加单引号??的更多相关文章
- Mybatis使用MySQL模糊查询时输入中文检索不到结果怎么办--转自http://www.jb51.net/article/88236.htm
这篇文章主要介绍了Mybatis使用MySQL模糊查询时输入中文检索不到结果的解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下 项目开发中,在做Mybatis动态查询时,遇到了 ...
- mybatis做like模糊查询
http://www.cnblogs.com/cyttina/p/3894428.html
- Mybatis使用MySQL进行模糊查询时输入中文检索不到结果
Mybatis使用MySQL进行模糊查询时输入中文检索时,需要在jdbcURL后增加参数 ?useUnicode=true&characterEncoding=UTF-8
- input动态模糊查询的实现方式
最近在用jQuery实现动态模糊查询的时候,找了挺久都没有找到像Vue.js的watch属性这么好用的动态模糊查询方法.就分享一下目前遇到的坑和可以实现动态查询的几种方式. 1.jQuery的chan ...
- Mybatis注解开发模糊查询
Mybatis注解开发模糊查询 一般在使用mybatis时都是采用xml文件保存sql语句 这篇文章讲一下在使用mybatis的注解开发时,如何进行模糊查询 模糊查询语句写法(在@Select注解中) ...
- 一个jpa动态模糊查询的实现
最近一直在是用spring data jpa,使用起来确实方便,如果是单表的操作基本上通过方法名都可以实现,下面是一个 Specification 实现动态模糊查询的例子这个查询是通过JpaSpeci ...
- Hibernate使用createSqlQuery进行模糊查询时找不到数据
1. 首先明确一点,使用createSqlQuery如下两种方式的占位符都可以使用,这个在官方的文档可以查到. 注意使用模糊查询时,赋值两边不可以添加单引号. Query query = sess.c ...
- 【mybatis】mybatis自定义动态字段查询,mybatis实现动态字段查询,如果某个条件为null,则不查询某个字段,否则就查询某个字段
mybatis实现动态字段查询,如果某个条件为null,则不查询某个字段,否则就查询某个字段 先看一下 怎么实现动态的自定义字段查询: 例如: 而field 就是数据表中的某一个字段 String f ...
- Mybatis框架的模糊查询(多种写法)、删除、添加(四)
学习Mybatis这么多天,那么我给大家分享一下我的学习成果.从最基础的开始配置. 一.创建一个web项目,看一下项目架构 二.说道项目就会想到需要什么jar 三.就是准备大配置链接Orcl数据库 & ...
随机推荐
- 【ztree】zTree节点增删改
今天遇到一个需求是对zTree的节点进行增删改,经过查阅资料总结如下: 效果: 完成增删改,要注意几个关键点: 使用 编辑功能,必须设置 setting.edit 中的各个属性 使用 编辑功能的事件回 ...
- 关于eclipse总是出现adb refused request的问题(转)
1.检查下是不是开启了手机助手之类2.打开进程管理器,结束所有的adb.exe 3.关闭所有的杀毒软件之类的东东4.检查USB连接线,USB口,把USB线连在电脑主机后面的USB口5.重启eclips ...
- C#图解教程学习笔记——方法
一.字段和本地变量.本地常量字段:隶属于类的变量,即类的成员变量.本地变量:于保存本地的或临时的计算数据,即局部变量.本地常量:必须声明在块内部,声明时必须初始化,声明后不能改变.实例字段与本地变量区 ...
- uva 1442:Cave(贪心)
题意:一个洞穴长n,告诉你每个位置的地面高度和顶部高度,让你往里灌水,要求水不能碰到天花板(但可以无限接近).求最多的水量.(洞穴两边视为封闭) 思路:如果知道一个位置向左看最高可以多高,向右看最高可 ...
- 牛客网 Wannafly挑战赛8 B.LBJX的三角形
B-LBJX的三角形 链接:https://www.nowcoder.com/acm/contest/57/B来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K, ...
- 洛谷——P1227 [JSOI2008]完美的对称
P1227 [JSOI2008]完美的对称 题目描述 在峰会期间,必须使用许多保镖保卫参加会议的各国代表.代表们除了由他自己的随身保镖保护外,组委会还指派了一些其他的特工和阻击手保护他们.为了使他们的 ...
- luogu P3147 [USACO16OPEN]262144
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- Objective-C的self.用法的一些总结
最近有人问我关于什么时候用self.赋值的问题, 我总结了一下, 发出来给大家参考. 有什么问题请大家斧正. 关于什么时间用self. , 其实是和Obj-c的存取方法有关, 不过网上很多人也都这么解 ...
- Oracle 检查表空间使用情况
--检查表空间使用情况 SELECT f.tablespace_name , a.total "total (M)" , f.free "fre ...
- flannel无法跨主机ping通容器的解决方式
前几天,出现了无法跨主机ping通容器的情况,导致一个node机网络中断,无法访问,排查过程如下. 首先确认,宿主机node2是可以ping通容器 [root@node2 ~]# ping 10.1. ...