Mybatis 中在xxx.mapper书写模糊查询

1.在mybatis中,书写sql,有时候会有一些不细心,如:

  1. <!-- 首页商品 关键字搜索-->
  2. <select id="getGoodsByLikeTitle" resultMap="BaseResultMap">
  3. SELECT
  4. <!-- 详情 -->
  5. gd.pay_price,
  6. gd.id,
  7. gd.couponamount,
  8. gd.sale_point,
  9. gd.earn_point,
  10. gd.stock_quantity,
  11. gd.site_id,
  12. gd.vip_level,
  13. <!-- 商品 -->
  14. g.id AS 'goodId',
  15. g.title AS 'goodTitle',
  16. g.img_url AS 'goodImgUrl',
  17. g.sale_price AS 'goodSalePrice',
  18. g.sale_count AS 'goodSaleCount',
  19. g.link_url AS 'goodLinkUrl',
  20. g.tags AS 'goodTags',
  21. g.summary AS 'goodSummary',
  22. g.series_no AS 'goodSeriesNo',
  23. g.spec_desc AS 'goodSpecDesc',
  24. g.weight AS 'goodWeight',
  25. g.add_time AS 'goodAddTime'
  26. FROM goods_detail AS gd
  27. INNER JOIN goods AS g
  28. ON gd.goods_id=g.id
  29. WHERE g.satus=1
  30. AND gd.site_id=#{siteId}
  31. AND g.title like CONCAT('%',#{wkeyword},'%')
  32. ORDER BY g.add_time DESC
  33. </select>

如果上述中的sql like 后面用的是'%${wkeyword}%',看似没有错,但你在调用时输入“ ` ”引号间的符号,sql 就无法识别,即会报错。如果用CONCAT('%',#{wkeyword},'%') 来替代'%${wkeyword}%',则万事大吉。具体这个是怎样一个原理不是太清楚,看了很多文档都说是#{}与${}的不同,自己积累一下,避免后期遇到。

posted @ 2019-08-28 16:25  那些年的代码  阅读(2196)  评论(0编辑  收藏  举报