Mybatis 中在xxx.mapper书写模糊查询
1.在mybatis中,书写sql,有时候会有一些不细心,如:
-
<!-- 首页商品 关键字搜索-->
-
<select id="getGoodsByLikeTitle" resultMap="BaseResultMap">
-
SELECT
-
<!-- 详情 -->
-
gd.pay_price,
-
gd.id,
-
gd.couponamount,
-
gd.sale_point,
-
gd.earn_point,
-
gd.stock_quantity,
-
gd.site_id,
-
gd.vip_level,
-
<!-- 商品 -->
-
g.id AS 'goodId',
-
g.title AS 'goodTitle',
-
g.img_url AS 'goodImgUrl',
-
g.sale_price AS 'goodSalePrice',
-
g.sale_count AS 'goodSaleCount',
-
g.link_url AS 'goodLinkUrl',
-
g.tags AS 'goodTags',
-
g.summary AS 'goodSummary',
-
g.series_no AS 'goodSeriesNo',
-
g.spec_desc AS 'goodSpecDesc',
-
g.weight AS 'goodWeight',
-
g.add_time AS 'goodAddTime'
-
FROM goods_detail AS gd
-
INNER JOIN goods AS g
-
ON gd.goods_id=g.id
-
WHERE g.satus=1
-
AND gd.site_id=
-
AND g.title like CONCAT('%',#{wkeyword},'%')
-
ORDER BY g.add_time DESC
-
</select>
如果上述中的sql like 后面用的是'%${wkeyword}%',看似没有错,但你在调用时输入“ ` ”引号间的符号,sql 就无法识别,即会报错。如果用CONCAT('%',#{wkeyword},'%') 来替代'%${wkeyword}%',则万事大吉。具体这个是怎样一个原理不是太清楚,看了很多文档都说是#{}与${}的不同,自己积累一下,避免后期遇到。
梅花香自古寒来