1. sql中字符串拼接

   SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');

2. 使用 ${...} 代替 #{...}

   SELECT * FROM tableName WHERE name LIKE '%${text}%'; 

  ${}解析过来的参数值不带单引号,#{}解析传过来参数带单引号。

  使用第二种${}的拼接存在sql注入攻击的风险,例如例2中查询的是

 1' or 1=1 or '1

就会返回所有数据,这只是个最简单的方式,而在java代码中先拼接好字符串

然后#{text}传入能避免这个问题。

3. 程序中拼接

   Java

  // or String searchText = "%" + text + "%";

   String searchText = new StringBuilder("%").append(text).append("%").toString();

   parameterMap.put("text", searchText);

 

  SqlMap.xml

   SELECT * FROM tableName WHERE name LIKE #{text};

相关文章:

  • 2022-12-23
  • 2022-01-21
  • 2021-05-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-23
  • 2021-09-14
  • 2022-12-23
  • 2021-12-02
  • 2021-11-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案