在学习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实现模糊查询的几种方式的更多相关文章

  1. Mybatis的select查询的三种方式

    1.首先建立一个测试的dao public interface IStudentDao { // 根据姓名查询 List<Student> selectStudentsByName(Str ...

  2. 五 Mybatis一对一关联查询的两种方式(基于resultType&基于resultMap)

    关联查询: 一个用户对应多个订单,一个订单只有一个用户 订单关联用户:两种方式 一:基于resultTYpe,一个与表关系一样的pojo实现 主表订单,从表用户 首先要有一个与关联查询表关系一样的po ...

  3. 【mysql模糊查询的几种方式】

    select * from activyty_code where acname like '%yj%' 1:%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分 ...

  4. JDBC模糊查询的4种方式

    1:%放在占位符中              parameters.add("%"+familyMemberQueryBean.getFullName()+"%" ...

  5. java之mybatis之模糊查询

    1.在 mybatis 中,模糊查询可以有以下方式 (1).第一种,直接将封装好的条件传给 sql 语句 <select id="findByName" parameterT ...

  6. MyBatis开发Dao层的两种方式(原始Dao层开发)

    本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...

  7. MyBatis开发Dao层的两种方式(Mapper动态代理方式)

    MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...

  8. MyBatis中模糊查询

    接口 // 模糊查询 List<User> getUserLike(String value); Mapper.xml文件 <!-- 模糊查询 --> <select i ...

  9. mybatis中批量插入的两种方式(高效插入)

    MyBatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用 ...

随机推荐

  1. SpringCloud学习1-服务注册与发现(Eureka)

    由于样式兼容性问题,本文后半部分被截断,可到个人博客找到本文: https://blog.rmiao.top/springcloud-eureka/ 前言 Oracle转让Java,各种动态语言的曝光 ...

  2. 信号为E时,如何让语音识别脱“网”而出?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯教育云发表于云+社区专栏 一般没有网络时,语音识别是这样的 ▽ 而同等环境下,嵌入式语音识别,是这样的 ▽ 不仅可以帮您边说边识. ...

  3. 将文件内容隐藏在bmp位图中

    首先要实现这个功能,你必须知道bmp位图文件的格式,这里我就不多说了,请看:http://www.cnblogs.com/xiehy/archive/2011/06/07/2074405.html 接 ...

  4. Maven教程(2)--Maven的配置,MyEclipse与Eclipse的配置

    1.修改Maven的 repository的路径 2.找到mirrors 使用阿里云镜像 <mirror> <id>alimaven</id> <name&g ...

  5. Java中的静态变量、静态方法问题

    由关键字static所定义的变量与方法,分别称为静态变量和静态方法,它们又都被称为静态成员 1.静态方法 无需本类的对象也可以调用此方法,调用形式为“类名.方法名”,静态方法常常为其他类提供一些方法而 ...

  6. Hive基础之Hive与关系型数据库的比较

    Hive与关系型数据库的比较     使用Hive的CTL(命令行接口)时,你会感觉它很像是在操作关系型数据库,但是实际上,Hive和关系型数据库有很大的不同.       1)Hive和关系型数据库 ...

  7. Java集合之Hashtable源码分析

    概述 Hashtable也是基于哈希表实现的, 与map相似, 不过Hashtable是线程安全的, Hashtable不允许 key或value为null. 成员变量 Hashtable的数据结构和 ...

  8. canvas-4createPattern.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Python 利用字典实现类似 java switch case 功能

    def add(): print('add') def sub(): print('sub') def exit(): print('exit') choice = { '1' : add, '2' ...

  10. HTML中令人惊喜的全局属性

    1.accesskey 属性 : 规定激活元素的快捷键. 浏览器支持:几乎所有浏览器均 accesskey 属性,除了 Opera. 定义和用法 accesskey 属性规定激活(使元素获得焦点)元素 ...