当前位置: 首页 > news >正文

mybatis的模糊查询写法

mybatis做like模糊查询

 

 

1.  参数中直接加入%%

  param.setUsername("%CD%");
      param.setPassword("%11%");

	<select  id="selectPersons" resultType="person" parameterType="person">
		select id,sex,age,username,password from person where true 
			<if test="username!=null"> AND username LIKE #{username}</if>
			<if test="password!=null">AND password LIKE #{password}</if>
	
	</select>

2.  bind标签

<select id="selectPersons" resultType="person" parameterType="person">
  <bind name="pattern" value="'%' + _parameter.username + '%'" />
  select id,sex,age,username,password 
  from person
  where username LIKE #{pattern}
</select>

 

 

3. CONCAT

where username LIKE concat(concat('%',#{username}),'%')

模糊查询:

工作中用到,写三种用法吧,第四种为大小写匹配查询

 

1. sql中字符串拼接

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

 

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

   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. 大小写匹配查询

 

[sql]  view plain  copy
 
  1. SELECT *  FROM TABLENAME  WHERE UPPER(SUBSYSTEM) LIKE '%' || UPPER('jz') || '%'  
  2.  --或者是  
  3. 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" >

 
 

转载于:https://www.cnblogs.com/fengli9998/p/7474258.html

相关文章:

  • 布局云计算:OpenFabric数据中心显身手
  • 更多存储:Facebook公司革新设计提高磁盘密度
  • 成都规划建设统一大数据中心 非涉密政务信息上“云”
  • IntPtr 转 string
  • Shiro的记住我功能失效原因
  • Django内置Admin
  • 东软发布RealSight大数据高级分析应用平台 三大产品组合融合分析人事物
  • 我是这样搞懂一个神奇的BUG
  • C++ 内存泄漏
  • 张星宇:技术人如何打造影响力
  • post方式请求数据
  • 机器学习入门之认知
  • webpack项目中使用grunt监听文件变动自动打包编译
  • jmeter tcp取样器使用方法
  • (六)软件测试分工
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • Date型的使用
  • Javascript Math对象和Date对象常用方法详解
  • VuePress 静态网站生成
  • Web Storage相关
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • 从零开始在ubuntu上搭建node开发环境
  • 给初学者:JavaScript 中数组操作注意点
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 前端知识点整理(待续)
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 应用生命周期终极 DevOps 工具包
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • 【云吞铺子】性能抖动剖析(二)
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • $jQuery 重写Alert样式方法
  • %@ page import=%的用法
  • (Java数据结构)ArrayList
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .NET 8.0 中有哪些新的变化?
  • .NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试
  • .sh 的运行
  • :=
  • [2010-8-30]
  • [3D游戏开发实践] Cocos Cyberpunk 源码解读-高中低端机性能适配策略
  • [4.9福建四校联考]
  • [C/C++]数据结构 循环队列
  • [C++]unordered系列关联式容器
  • [LeetCode]Max Points on a Line
  • [Matlab有限元分析] 2.杆单元有限元分析
  • [office] excel如何计算毛重和皮重的时间间隔 excel计算毛重和皮重时间间隔方法 #笔记#学习方法
  • [Qt]QMainWindow
  • [RK3566-Android11] 关于 a2dpsink -蓝牙支持接收播放/无PIN码连接
  • [Swift]LeetCode856. 括号的分数 | Score of Parentheses
  • [Toolschain cpp ros cmakelist python vscode] 记录写每次项目重复的设置和配置 不断更新