多参数查询,使用parameterType。实例:

用户User[id, name, age]

1.mysql建表并插入数据

2.Java实体类

public class User {

    public User() {
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
} }

3.创建查询参数实体类

/**
* 模糊查询User的类
* @author Administrator
*
*/
public class SelectUserCondition { private String name;//姓名
private int minAge;//最小年龄
private int maxAge;//最大年龄 public SelectUserCondition() {
super();
}
public SelectUserCondition(String name, int minAge, int maxAge) {
super();
this.name = name;
this.minAge = minAge;
this.maxAge = maxAge;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMinAge() {
return minAge;
}
public void setMinAge(int minAge) {
this.minAge = minAge;
}
public int getMaxAge() {
return maxAge;
}
public void setMaxAge(int maxAge) {
this.maxAge = maxAge;
} }

4.sql查询的配置文件selectUserMapper.xml

<mapper namespace="com.mlxs.mybatis.test7.selectUserMapper">
<!--
动态sql,模糊查询  parameterType为查询条件实体
  -->
  <select id="getConditionUser" parameterType="SelectUserCondition" resultType="User">
    SELECT * FROM users WHERE <if test='name != "%null%"'> NAME LIKE #{name} AND </if> age BETWEEN #{minAge} AND #{maxAge}
  </select> </mapper>

5.测试类:

/**
* 动态sql模糊查询
*
* 注意:当name=null时,"%"+name+"%"=%null%
* @author 魅力_小生
*
*/
public class _Test7SelectConditionUser { @Test
public void getConditionUser(){
//创建session,设置事务为true
SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
String statement = "com.mlxs.mybatis.test7.selectUserMapper.getConditionUser";
//定义查询条件实体
SelectUserCondition condition = new SelectUserCondition("%a%", 20, 100);
List<User> ulist = session.selectList(statement, condition);
System.out.println("userList--->"+ulist);
session.close();
}
}

6.结果:

userList--->[User [id=3, name=update2, age=100], User [id=5, name=add1, age=23]]

8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)的更多相关文章

  1. MyBatis(十一):Mybatis 动态SQL语句完成多条件查询

    之前文章中对in的用法做过讲解:<MyBatis(四):mybatis中使用in查询时的注意事项> 实际上对于多个参数的用法也是这是注意的: 多参&if判空&List集合判 ...

  2. MyBatis动态SQL使用,传入参数Map中的Key判断

    <select id="" parameterType="Map" resultMap="commodityResultMap" &g ...

  3. Mybatis动态sql及分页、特殊符号

    目的: mybatis动态sql(案例:万能查询) 查询返回结果集的处理 mybatis的分页运用 mybatis的特殊符号 mybatis动态sql(案例:万能查询) 根据id查询 模糊查询 (参数 ...

  4. mybatis 动态sql和参数

    mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...

  5. mybatis动态sql中的两个内置参数(_parameter和_databaseId)

    mybatis动态sql中的两个内置参数(_parameter和_databaseId)   <!-- mybatis动态sql的两个内置参数           不只是方法传递过来的参数可以被 ...

  6. mybatis在动态 SQL 中使用了参数作为变量,必须要用 @Param 注解

    如果在动态 SQL 中使用了参数作为变量,那么就要用 @Param 注解,即使你只有一个参数.如果我们在动态 SQL 中用到了 参数作为判断条件,那么也是一定要加 @Param 注解的,例如如下方法: ...

  7. 超全MyBatis动态SQL详解!( 看完SQL爽多了)

    MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...

  8. MyBatis动态SQL(认真看看, 以后写SQL就爽多了)

    目录 0 一起来学习 mybatis 1 数据准备 2 if 标签 2.1 在 WHERE 条件中使用 if 标签 2.1.1 查询条件 2.1.2 动态 SQL 2.1.3 测试 2.2 在 UPD ...

  9. MyBatis从入门到精通(第4章):MyBatis动态SQL【if、choose 和 where、set、trim】

    (第4章):MyBatis动态SQL[if.choose 和 where.set.trim] MyBatis 的强大特性之一便是它的动态 SQL.MyBatis 3.4.6版本采用了功能强大的OGNL ...

随机推荐

  1. 视频处理控件TVideoGrabber如何对屏幕进行录制/压缩

    TVideoGrabber可以对屏幕进行录制和压缩,本文来详细的说明在多种情况下TVideoGrabber是如何实现屏幕的录制和压缩. 屏幕录制 当VideoSource = vs_ScreenRec ...

  2. scala 隐式转换

    先参考这篇文章:http://www.jianshu.com/p/a344914de895 package com.test.scalaw.test /** * scala隐式转换 */ object ...

  3. Java生产者和消费者问题

    容器类Box.java public class Box { private int num = 0; public void put(){ if(num==10){ try { System.out ...

  4. linux配置java环境变量(详细)【转】

    转自:http://www.cnblogs.com/samcn/archive/2011/03/16/1986248.html 一. 解压安装jdk 在shell终端下进入jdk-6u14-linux ...

  5. Pending Statistics

    Starting with the 11g Release 1 (11.1), when gathering statistics, you have the option to automatica ...

  6. Eclipse如何设置字体

    Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,是学习java和开发java最常用的IDE之一.有时候会遇到这种情况,刚刚下载了新的Eclipse,字体显示英文没问题,但是显示中文就 ...

  7. 在Java中导出word、excel格式文件时JSP页面头的设置

    我们在JSP中往往会把一些表格里的东西需要导出到本地,一般都是导成word.excel格式的文件.这只需要在JSP页面头设置及在<head></head>标签中添加下面的代码: ...

  8. 关于IllegalMonitorStateException异常

    关于IllegalMonitorStateException异常: api中的解释  另请参见: Object.notify(), Object.notifyAll(), Object.wait(), ...

  9. preparedStatement和Statement 有什么不一样

    1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象.    2.作 ...

  10. c# XML省市联动

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...