MyBatis实现模糊查询的几种方式
作者:行癫 发布时间:2023-06-03 17:31:43
标签: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代码中拼接字符串
public static void main(String[] args) {
try {
int count = 500;
long begin = System.currentTimeMillis();
testString(count);
long end = System.currentTimeMillis();
long time = end - begin;
System.out.println("String 方法拼接"+count+"次消耗时间:" + time + "毫秒");
begin = System.currentTimeMillis();
testStringBuilder(count);
end = System.currentTimeMillis();
time = end - begin;
System.out.println("StringBuilder 方法拼接"+count+"次消耗时间:" + time + "毫秒");
} catch (Exception e) {
e.printStackTrace();
}
}
private static String testString(int count) {
String result = "";
for (int i = 0; i < count; i++) {
result += "hello ";
}
return result;
}
private static String testStringBuilder(int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append("hello");
}
return sb.toString();
}
来源:https://blog.csdn.net/lonely_dog/article/details/74171314
0
投稿
猜你喜欢
- 这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:using System;using
- 一、封装类1.封装类概念Java中存在基础数据类型,但是在某些情况下,我们要对基础数据类型进行对象的操作,例如,集合中只能存对象,而不能存在
- 读取xml配置bean(@ImportResource)1、应用场景旧框架SSM项目移行到SpringBoot中,xml配置文件很齐全,就可
- 本文实例讲述了C#索引属性的用法。分享给大家供大家参考。具体如下:这里演示C#类如何声明索引属性以表示不同种类事物的类似数组的集合。// i
- 关于java的字符串处理我们一般使用String类和StringBuffer类那么String类和StringBuffer类的区
- 1、前言最近在用Kotlin+Spring Boot写一个后端项目,实体类习惯性地用了Kotlin中的data class,但是Spring
- kafka是什么?Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写。Kafka是一种高吞吐量的分布式
- 1.新建控制台应用程序2.新建类 EncryptHelper.cspublic static class EncryptHelper{ &n
- 之前在项目中会用到在Java在后台把数据填入Word文档的模板来提供前台下载,为了自己能随时查看当时的实现方案及方便他人学习我写了这篇博客,
- 本文实例为大家分享了Java使用MulticastSocket实现群聊应用程序的具体代码,供大家参考,具体内容如下在这篇文章中,讨论了一个使
- 获取e.printStackTrace()打印的信息某些情况下,我们需要获取应用打印的异常信息,这时就可以借助StringWriter和Pr
- 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类。可以把
- 一、系统介绍1.开发环境开发工具:Eclipse2021JDK版本:jdk1.8Mysql版本:8.0.132.技术选型Java+Swing
- 本文实例为大家分享了JS Struts2多文件上传的具体代码,供大家参考,具体内容如下1、JSP页面: JS控制增加删除多个上传文件框,代码
- 1.代码:import java.math.BigDecimal;import java.text.DecimalFormat;import
- 一、前言最近接到一个任务,需要爬取五级行政区划的所有数据(大概71万条数据在),需要爬取的网站:行政区划 - 行政区划代码查询 发
- 什么是枚举?枚举是JDK5引入的新特性。在某些情况下,一个类的对象是固定的,就可以定义为枚举。在实际使用中,枚举类型也可以作为一种规范,保障
- 一、悲观锁和乐观锁1.1. 乐观锁顾名思义,就是很乐观,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期
- using System;using System.Data;using System.Data.OleDb;namespace ZFSof
- spring in action第三版读书笔记spring3.0引入了spring expression language(spel)语言,