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 ...
随机推荐
- TVideoGrabber如何将网络摄像头影像实时发布到网络
在TVideoGrabber中如何将网络摄像头影像实时发布到网络?如何设置正在运行TVideoGrabber的一台电脑,同时通过另一台电脑在网络中实时的观看在线视频呢? 在这里称发送视频流的电脑为“m ...
- sql多表查询(out join,inner join, left join, right join)
left join以左表为基准显示所有左表的信息,在on中有符合条件的其他表也显示出来 right join则相反 inner join的只显示on中符合条件的 1 使用多个表格 在「world」资料 ...
- :has(selector) 匹配含有选择器所匹配的元素的元素
描述: 给所有包含 p 元素的 div 元素添加一个 text 类 HTML 代码: <div><p>Hello</p></div> <div&g ...
- mysql多实例部署
mysql 多实例常规来讲,主要有二种方案可以实现,这二种方案各有利弊,如下:1.基于多配置文件 通过使用多个配置文件来启动不同的进程,以此来实现多实例. 优点:逻辑简单,配置简单 缺点:管理起来不方 ...
- UserSelector兼容
1.更新到asp.net2.0或以上,将Microsoft.Web.UI.TreeView更换为新的System.Web.UI.WebControls.TreeView 2.将UserId,UserT ...
- IE11下,IIS7.5不自动生成__doPostBack事件
MS在 2013/10/8 已出补丁 http://www.microsoft.com/zh-cn/download/details.aspx?id=39257 https://support.mic ...
- expect语法
沙河西ftp上传,使用了expect语言的脚本. 我们经常会遇到一些需要与服务器程序打交道的场景,比如,从登陆某个服务器,然后进行某项工作.这很平常,但是如果把这个工作自动化进行,你就需要一个程序能自 ...
- RabbitMQ 基本概念介绍-----转载
1. 介绍 RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue )协议的开源实现.用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面都非 ...
- ACM第四站————最小生成树(克鲁斯卡尔算法)
都是生成最小生成树,库鲁斯卡尔算法与普里姆算法的不同之处在于——库鲁斯卡尔算法的思想是以边为主,找权值最小的边生成最小生成树. 主要在于构建边集数组,然后不断寻找最小的边. 同样的题目:最小生成树 题 ...
- android bluetooth蓝牙移植
http://blog.csdn.net/zhengmeifu/article/details/7705172 前段时间移植神念系统需要使用到bluetooth功能,现将移植过程中碰到的问题简要列一下 ...