MyBatis实现模糊查询的几种方式
在学习MyBatis过程中想实现模糊查询,可惜失败了。后来上百度上查了一下,算是解决了。记录一下MyBatis实现模糊查询的几种方式。
数据库表名为test_student,初始化了几条记录,如图:
起初我在MyBatis的mapper文件中是这样写的:
<select id="searchStudents" resultType="com.example.entity.StudentEntity"parameterType="com.example.entity.StudentEntity">SELECT * FROM test_student<where><if test="age != null and age != '' and compare != null and compare != ''">age${compare}#{age}</if><if test="name != null and name != ''">AND name LIKE '%#{name}%'</if><if test="address != null and address != ''">AND address LIKE '%#{address}%'</if></where>ORDER BY id</select>
写完后自我感觉良好,很开心的就去跑程序了,结果当然是报错了:
经百度得知,这么写经MyBatis转换后(‘%#{name}%’)会变为(‘%?%’),而(‘%?%’)会被看作是一个字符串,所以Java代码在执行找不到用于匹配参数的 ‘?’ ,然后就报错了。
解决方法
1.用${…}代替#{…}
<select id="searchStudents" resultType="com.example.entity.StudentEntity"parameterType="com.example.entity.StudentEntity">SELECT * FROM test_student<where><if test="age != null and age != '' and compare != null and compare != ''">age${compare}#{age}</if><if test="name != null and name != ''">AND name LIKE '%${name}%'</if><if test="address != null and address != ''">AND address LIKE '%${address}%'</if></where>ORDER BY id</select>
查询结果如下图:
注:使用${…}不能有效防止SQL注入,所以这种方式虽然简单但是不推荐使用!!!
2.把’%#{name}%’改为”%”#{name}”%”
<select id="searchStudents" resultType="com.example.entity.StudentEntity"parameterType="com.example.entity.StudentEntity">SELECT * FROM test_student<where><if test="age != null and age != '' and compare != null and compare != ''">age${compare}#{age}</if><if test="name != null and name != ''">AND name LIKE "%"#{name}"%"</if><if test="address != null and address != ''">AND address LIKE "%"#{address}"%"</if></where>ORDER BY id</select>
查询结果:
3.使用sql中的字符串拼接函数
<select id="searchStudents" resultType="com.example.entity.StudentEntity"parameterType="com.example.entity.StudentEntity">SELECT * FROM test_student<where><if test="age != null and age != '' and compare != null and compare != ''">age${compare}#{age}</if><if test="name != null and name != ''">AND name LIKE CONCAT(CONCAT('%',#{name},'%'))</if><if test="address != null and address != ''">AND address LIKE CONCAT(CONCAT('%',#{address},'%'))</if></where>ORDER BY id</select>
查询结果:
4.使用标签
<select id="searchStudents" resultType="com.example.entity.StudentEntity"parameterType="com.example.entity.StudentEntity"><bind name="pattern1" value="'%' + _parameter.name + '%'" /><bind name="pattern2" value="'%' + _parameter.address + '%'" />SELECT * FROM test_student<where><if test="age != null and age != '' and compare != null and compare != ''">age${compare}#{age}</if><if test="name != null and name != ''">AND name LIKE #{pattern1}</if><if test="address != null and address != ''">AND address LIKE #{pattern2}</if></where>ORDER BY id</select>
查询结果:
5.在Java代码中拼接字符串
这个方法没试过,就不贴代码和结果了。
————2017.07.03
转载于:https://www.cnblogs.com/vocaloid-fan1995/p/10363833.html
MyBatis实现模糊查询的几种方式相关推荐
- MyBatis实现模糊查询的三种方式
MyBatis实现模糊查询的三种方式 准备 模糊查询 方式一 方式二 方式三 由于#{}是占位符本身,自带单引号,所以在模糊查询时需要一些技巧. 准备 数据库表 bean 模糊查询 以查询出所有用户名 ...
- Java里模糊查询的英文_MyBatis实现模糊查询的几种方式
在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: ...
- Mybatis学习记录(四)——Mybatis实现模糊查询的三种方法
2018.4.8 仅为个人理解 不足之处欢迎指正~ 数据库说明: 我们在之前的数据库中添加三列:sex major class 现有如下记录: Mybatis进行模糊查询: Mybatis进行模糊查询 ...
- MySQL模糊查询的三种方式
文章目录 一.Mybatis常用模糊查询方法 1.使用concat("%",#{name},"%") 2.使用'%${name}%' 3.使用"%&q ...
- mybatis一对多查询的两种方式
user 和order 对象 package com.itheima.mybatis.pojo;import java.io.Serializable; import java.util.Date;p ...
- MyBatis:模糊查询的4种实现方式
1.根据姓名模糊查询员工信息 1.1.方式一 步骤一:编写配置文件 步骤二:测试 步骤三:分析 此种方式需要在调用处手动的去添加"%"通配符. 1.2.方式二 说明: 使用方式一可 ...
- mybatis联表查询的几种方式,association和collection的用法
mybatis的association以及collection的用法 前言: 一.association 的三种用法: 第一种用法:association中使用select 第二种方法,嵌套 resu ...
- 使用Mybatis联表查询的几种方式
mybatis的association以及collection的用法 前言: 在项目中,某些实体类之间肯定有关联关系,比如一对一,一对多等.mybatis 中使用 association 和 coll ...
- mysql里面模糊查询sql_总结Mysql 模糊查询的几种方式
总结下开发过程中用过模糊查询功能所用的一些东西,还有就是在建表时一定要考虑完善在建表,否则后期数据量大了在改表会非常的麻烦,请不要为了能够尽快用表而匆匆创建.在这也是给自己一个警醒,以后不再犯. #s ...
- mybitis实现增,删,改,查,模糊查询的两种方式:(2)
方式二:mapper代理接口方式 这种方式只需要xml+接口(不用写实体类)但是需要符合三个规范 使用mapper'代理接口方式 在同一目录下(可以创建一个源文件夹,达到类文件和xml文件分类的作用) ...
最新文章
- java日期类的计算问题_java日期计算(Calendar类)
- qml demo分析(customgeometry-贝塞尔曲线)
- Markdown--语法说明
- 1090 Highest Price in Supply Chain (25 分)(模拟建树,找树的深度)牛客网过,pat没过...
- 程序员必须掌握的 12 个 JavaScript 技能!
- 自然语言处理︱简述四大类文本分析中的“词向量”(文本词特征提取)
- 批量创建用户(基于域)
- JS处理数据四舍五入
- win32SDK的hello,world程序(二)
- java设计模式--创建模式--单例模式
- java两天速成_JAVA速成
- 每日excel学习之查找替换和定位
- Glide使用心得——加载图片出现浅绿色背景问题和Glide加载完成的监听设置
- 永远无法实现的“诚实“
- 找不到战网服务器ip地址,wow服务器ip地址-我怎么知道战网服务器的IP地址去PING, – 手机爱问...
- lect01_codes_高阶语法
- 分布式是大数据处理的万能药?
- Docker在线文档收集
- java如何让程序暂停一会_Java如何暂停线程一段时间?
- 城堡里的一朵酷烈之花
热门文章
- 关于System.identityHashCode(obj) 与 obj.hashcode()
- javascript轻松解决前端数据排序(互换,置顶,上移,下移),快收藏吧
- jQuery设置文本框回车事件
- mysql 查看3306端口_如何查看端口(3306)被那个程序占用?
- python股票交易微信提醒_python实现秒杀商品的微信自动提醒功能(代码详解)
- 2021-01-21
- python解包裹_python-之基本语法
- 电机与拖动基础第四版_伺服电机控制
- Echarts:柱状图X轴数据隔一个显示下标
- Vue源码:mustache模板引擎学习