8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)
多参数查询,使用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)的更多相关文章
- MyBatis(十一):Mybatis 动态SQL语句完成多条件查询
之前文章中对in的用法做过讲解:<MyBatis(四):mybatis中使用in查询时的注意事项> 实际上对于多个参数的用法也是这是注意的: 多参&if判空&List集合判 ...
- MyBatis动态SQL使用,传入参数Map中的Key判断
<select id="" parameterType="Map" resultMap="commodityResultMap" &g ...
- Mybatis动态sql及分页、特殊符号
目的: mybatis动态sql(案例:万能查询) 查询返回结果集的处理 mybatis的分页运用 mybatis的特殊符号 mybatis动态sql(案例:万能查询) 根据id查询 模糊查询 (参数 ...
- mybatis 动态sql和参数
mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...
- mybatis动态sql中的两个内置参数(_parameter和_databaseId)
mybatis动态sql中的两个内置参数(_parameter和_databaseId) <!-- mybatis动态sql的两个内置参数 不只是方法传递过来的参数可以被 ...
- mybatis在动态 SQL 中使用了参数作为变量,必须要用 @Param 注解
如果在动态 SQL 中使用了参数作为变量,那么就要用 @Param 注解,即使你只有一个参数.如果我们在动态 SQL 中用到了 参数作为判断条件,那么也是一定要加 @Param 注解的,例如如下方法: ...
- 超全MyBatis动态SQL详解!( 看完SQL爽多了)
MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...
- 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 ...
- MyBatis从入门到精通(第4章):MyBatis动态SQL【if、choose 和 where、set、trim】
(第4章):MyBatis动态SQL[if.choose 和 where.set.trim] MyBatis 的强大特性之一便是它的动态 SQL.MyBatis 3.4.6版本采用了功能强大的OGNL ...
随机推荐
- Install Sogou IM 2.0 in Ubuntu14.04+/Xfce
Ubuntu14.04+ 安装搜狗输入法 搜狗输入法是一款非常友好的输入法产品,从Ubuntu14.04开始对Linux支持,不过只是Debian系的,是Ubuntu优麒麟组引入的.优麒麟是针对国人设 ...
- export
export export PATH=$PATH:/ROOT
- PHP自定义函数格式化json数据怎么调用?
<?php/*** Formats a JSON string for pretty printing** @param string $json The JSON to make pretty ...
- python 补充-decode和encode
1. decode与encode转码 在Python3中默认编码就是uncode,encode转成Byte类型 在Python2中默认编码就是ascii window下默认编码是GBK decode( ...
- jdbc连接集合
JDBC里统一的使用方法: Class.for(jdbcDriverName); Connection conn=DriverManager.getConnection(url,u ...
- 跨站点脚本编制实例(AppScan扫描结果)
最近工作要求解决下web的项目的漏洞问题,扫描漏洞是用的AppScan工具,其中有很多是关于跨站点脚本编制问题的.下面就把这块东西分享出来. 原创文章,转载请注明 ------------------ ...
- C# 加密解密
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Secur ...
- C#string常用函数总结
补充: 1:在C语言里 char占1个字节 而在C#,Java里char占两个字节 数据库里char 中汉占两个字节 字母数字占一个字 2:string ...
- Unity-Animator深入系列---剪辑播放后位置预判(Animator.Target)
回到 Animator深入系列总目录 animator.SetTarget(...);可以在播放前预判剪辑播放后的位置,但只限于人形动画 参数1是预判的关节,参数2是映射的剪辑时间 调用后通过targ ...
- TSP问题
之前写过一道类似的题目,Uva 1347. http://www.cnblogs.com/TreeDream/p/5981535.html 这个题目和TSP问题已经很接近了,只是描述的奇奇怪怪的,从最 ...