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. 程序中拼接
// 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. 大小写匹配查询
- 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配置文件中<mapper>的namespace属性配置错误造成的,没有根据命名空间的值(全称类名)找到相应映射接口。
如:<mapper namespace="com.xxx.xxx.mapper.TestMapper" >
mybatis的模糊查询写法的更多相关文章
- MyBatis实现模糊查询的几种方式
在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: ...
- mybatis的模糊查询格式
mybatis的模糊查询格式: <select id="xxx" parameterType="com.model.xxx" resultMap=&quo ...
- Mybatis中文模糊查询,数据库中有数据,但无结果匹配
1.Mybatis中文模糊查询,数据库中有数据,但无结果匹配 1.1 问题描述: Mybatis采用中文关键字进行模糊查询,sql语句配置无误,数据库有该数据,且无任何报错信息,但无查询结果 1.2 ...
- Mybatis的模糊查询以及自动映射
Mybatis的模糊查询 1. 参数中直接加入%% ? 1 2 3 4 5 6 7 8 9 param.setUsername("%CD%"); param.setP ...
- mybatis中模糊查询的方式
<!--Mapper.xml中如何进行模糊查询--> <sql id="brand_columns"> id, name, firstChar,brandN ...
- mybatis中模糊查询的使用以及一些细节问题的注意事项
页面有个功能 为 根据 品牌名进行 关键字查询,对应到数据库的是brand表的name字段的模糊查询 如果用的是SSM框架,在mybatis中我们需要自己写sql语句,涉及到like的模糊查询,myb ...
- java之mybatis之模糊查询
1.在 mybatis 中,模糊查询可以有以下方式 (1).第一种,直接将封装好的条件传给 sql 语句 <select id="findByName" parameterT ...
- 用mybatis进行模糊查询总是查不到结果!
//IStudentDao.xml @Override public List<Student> selectStudentByName(String name) { SqlSession ...
- MyBatis使用模糊查询用户信息及log4j配置文件详解
1.1 根据用户名称模糊查询用户信息 根据用户名模糊查询用户信息,只需要我们更改映射文件中的sql语句.其他的内容跟上一篇的内容是一样的 1.2添加根据用户名称模糊查询用户信息的sql语句 实例中是查 ...
随机推荐
- 【label】标签组件说明
label标签组件 用来改进表单组件的可用性,使用for属性找到对应的id,或者将控件放在该标签下,当点击时,就会触发对应的控件.目前可以绑定的控件有:<button/>, <che ...
- Font Awesome 完美的图标字体
好久没来,虽说鄙人的人气不咋地,但还是很想念自己这一亩二分田地. 近期用在平台开发中,看着设计师摆开阵势,准备大画图标,想着自己将会很KUBI拼凑css-sprite图片,接着写一大堆 class^= ...
- 【Python 开发】第二篇 :Python安装
一.python3.x安装 1)由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. 官网:https://www.python.o ...
- POJ 3084 Panic Room(最大流最小割)
Description You are the lead programmer for the Securitron 9042, the latest and greatest in home sec ...
- canvas学习(一):线条,图像变换和状态保存
canvas学习(一):线条,图像变换和状态保存 一:绘制一条线段: var canvas = document.getElementById('canvas') var ctx = canvas.g ...
- 2019-1-7Xiaomi Mi5 刷全球版MIUI教程
2019-1-7Xiaomi Mi5 刷全球版MIUI教程 mi5 教程 小书匠 欢迎走进zozo的学习之旅. 前言 固件下载 刷机 刷recovery,root 试用体验 其他参考 前言 机器是老 ...
- C#之WCF入门1—简单的wcf例子
第一步:创建一个空的解决方案,新建一个WCF服务应用程序项目(使用默认名字) 来模拟服务端,新建一个控制台应用程序项目(名称改为 ConsoleApp)来模拟客户端. 第二步:简单分析WcfServi ...
- 如何用grep命令同时显示匹配行上下的n行 (美团面试题目)
标准unix/linux下的grep通过以下参数控制上下文 grep -C 5 foo file 显示file文件中匹配foo字串那行以及上下5行grep -B 5 foo file 显示foo及前5 ...
- Chrome Extensions API & options
Chrome Extensions API options https://developer.chrome.com/extensions https://developer.chrome.com/e ...
- React 16.x & Hooks
React 16.x & Hooks Hooks https://reactjs.org/docs/hooks-intro.html https://reactjs.org/docs/hook ...