mybatis模糊查询(转载)

时间:2023-03-08 22:25:37
mybatis模糊查询(转载)

原文地址:http://blog.csdn.net/luqin1988/article/details/7865643

模糊查询:

1. sql中字符串拼接

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

2. 使用 ${...} 代替 #{...}—— 该方式不推荐使用,存在sql注入风险,如果要传递表名这样的参数,事先做好参数校验及转换

SELECT * FROM tableName WHERE name LIKE '%${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};

4. 大小写匹配查询(如果数据库及字段的collation属性设置为utf8_bin,模糊查询时也会区分大小写,如果是utf8_general_ci 则模糊查询时不区分大小写

[sql] view plain copy

  1. --或者是
  2. SELECT *   FROM TABLENAME  WHERE LOWER(SUBSYSTEM) LIKE '%' || LOWER('jz') || '%'  

异常:

java.lang.IllegalArgumentException: modify is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)
 at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:466)
 at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:349)
 at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:137)
 at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:46)
 at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:34)
 at $Proxy10.modify(Unknown Source)

抛出这个异常的原因可能是因为mapper.xml配置文件中<mapper>的namespace属性配置错误造成的,没有根据命名空间的值(全称类名)找到相应映射接口。

如:<mapper namespace="com.xxx.xxx.mapper.TestMapper" >

补充一点:

sql映射DAO接口中,如果使用了@Param注解,一定要注意不能导错包!!!注意查看,导入的是  org.apache.ibatis.annotations.Param; 这个包,如果不小心导成 org.jboss.logging.Param; 这个包,后果你懂!例如;

import org.apache.ibatis.annotations.Param;

/**
* 删除新闻----(需要事务控制,同时删除评论)
* @param id
* @return
*/
Integer delteDetail(@Param("newsId") Integer id);

mysql新建数据库时的collation选择(转)

https://www.cnblogs.com/sonofelice/p/6432986.html