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
MyBatis实现模糊查询的几种方式的更多相关文章
- Mybatis的select查询的三种方式
1.首先建立一个测试的dao public interface IStudentDao { // 根据姓名查询 List<Student> selectStudentsByName(Str ...
- 五 Mybatis一对一关联查询的两种方式(基于resultType&基于resultMap)
关联查询: 一个用户对应多个订单,一个订单只有一个用户 订单关联用户:两种方式 一:基于resultTYpe,一个与表关系一样的pojo实现 主表订单,从表用户 首先要有一个与关联查询表关系一样的po ...
- 【mysql模糊查询的几种方式】
select * from activyty_code where acname like '%yj%' 1:%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分 ...
- JDBC模糊查询的4种方式
1:%放在占位符中 parameters.add("%"+familyMemberQueryBean.getFullName()+"%" ...
- java之mybatis之模糊查询
1.在 mybatis 中,模糊查询可以有以下方式 (1).第一种,直接将封装好的条件传给 sql 语句 <select id="findByName" parameterT ...
- MyBatis开发Dao层的两种方式(原始Dao层开发)
本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...
- MyBatis开发Dao层的两种方式(Mapper动态代理方式)
MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...
- MyBatis中模糊查询
接口 // 模糊查询 List<User> getUserLike(String value); Mapper.xml文件 <!-- 模糊查询 --> <select i ...
- mybatis中批量插入的两种方式(高效插入)
MyBatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用 ...
随机推荐
- SpringMVC教程1
一.SpringMVC介绍 1.MVC介绍 ==模型-视图-控制器(MVC== 是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型.视图及控制器在应用程序中的角色将业务逻辑从界面中 ...
- 深入浅出 JVM GC(1)
# 前言 初级 Java 程序员步入中级程序员的有一个无法绕过的阶段------GC(Garbage Collection).作为 Java 程序员,说实话,很幸福,不用像 C 程序员那样,时刻关心着 ...
- JS 上传图片 + 预览功能(一)
JS 上传图片 + 预览功能 <body> <input type="file" id="fileimg1" style="disp ...
- 浏览器与Tomcat交互
浏览器与Tomcat交互 Web开发者都知道在Tomcat下部署应用后启动Tomcat即可通过浏览器与Tomcat建立连接. 那么二者之间的连接建立过程是怎么样的呢?(在此,我们不具体讲述关于网络底层 ...
- 2017-11-06 日语编程语言"抚子" - 第三版特色初探
"中文编程"知乎专栏原链 原文: 日语编程语言"抚子" - 第三版特色初探 它山之石可以攻玉. 学习其他的母语编程语言, 相信对中文编程语言的设计和实践有借鉴意 ...
- C# 插件式开发
在网上找了下插件式编程的资料,这里自己先借鉴下别人的,同时发现有自己的看法,不过由于本人水平有限,不一定有参考价值,写出来一方面是为了总结自己,以求提高,另一方面也希望各为朋友看到我的不足,给我提出宝 ...
- JMeter—逻辑控制器(六)
参考<全栈性能测试修炼宝典JMeter实战>第六章 JMeter 元件详解中第一节JMeter逻辑控制器 JMeter逻辑控制器可以对元件的执行逻辑进行控制,除仅一次控制器外,其他可以嵌套 ...
- SQL注入介绍
一.SQL注入概念 1.sql注入是一种将sql代码添加到输入参数中 2.传递到sql服务器解析并执行的一种攻击手法 举例:某个网站的用户名为name=‘admin’.执行时为select ...
- mongodb 配置文件
本文档是在mongodb为3.4下编写的,仅作为参考,详细内容请参考:https://docs.mongodb.com/manual/reference/configuration-options/# ...
- Excel快捷键大全 Excel2013/2010/2007/2003常用快捷键大全
一个软件最大的用处是提高工作效率,衡量一个软件的好坏,除了是否出名之外,最主就是能否让一个新手更快的学会这个软件和提高工作速度.就拿Excel表格来说吧,平常办公中我们经常会用它来制作表格,统计数据或 ...