首页 > 学院 > 开发设计 > 正文

MyBatis的各种模糊查询

2019-11-06 09:20:00
字体:
来源:转载
供稿:网友

在工作中,我们常用到Mybatis的四种模糊查询,前三种在工作中用之非常多,第四种为大小写匹配查询。

第一种查询:Sql中字符串拼接

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

第二种查询:使用 ${…} 代替 #{…}

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

第三种查询:程序中拼接

java中的代码:

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

SqlMap.xml

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

第四种查询:大小写匹配查询

SELECT * FROM TABLENAME WHERE UPPER(SUBSYSTEM) LIKE '%' || UPPER('jz') || '%' --或者是 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配置文件中的namespace属性配置错误造成的,没有根据命名空间的值(全称类名)找到相应映射接口。

如:<mapper namespace="com.xxx.xxx.mapper.TestMapper" >
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表