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 ...
随机推荐
- source insight技巧
(1)在Source Insight中能不能设置永久Bookmark 可以从macro方面入手 (2)source insight中添加.S文件 (3)source insight里面怎么能不让它每次 ...
- 160901、在大型项目中组织CSS
编写CSS容易. 编写可维护的CSS难. 这句话你之前可能听过100次了. 原因是CSS中的一切都默认为全局的.如果你是一个C程序员你就知道全局变量不好.如果你是任何一种程序员,你都知道隔离和可组合的 ...
- Android 标签的主题样式
Android平台定义的主题样式: android:theme="@android:style/Theme.Dialog" 将一个Activity显示为对话框模式 •andro ...
- Table Properties [AX 2012]
Table Properties [AX 2012] 1 out of 2 rated this helpful - Rate this topic Updated: July 20, 2012 Ap ...
- PHP serialize & JSON 解析
对于JSON(JavaScript Object Notation)大家应该不陌生,它是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Program ...
- <<构建之法>>略读感想
经过对构建之法这本书的快速阅读和学习,我有以下疑问. 1.对软件工程来说是应该更注重结果和功能的实现还是更注重代码的易读和完整? 2.应该怎样平衡不同用户的不同需求以达到使大多数人满意的目的? 3.应 ...
- css杂记
1,font-variant: 设置文本是否为小型的大写字母,值可以为normal,small-caps; 2,a:link: 未访问过的 a:visited: 访问过的 a:active: 活动的链 ...
- 【转】介绍设置Session失效的几种方法
转载地址:http://developer.51cto.com/art/201106/269493.htm Session对象是HttpSessionState的一个实例.该类为当前用户会话提供信息, ...
- 下载服务器端的图片和下载excel
#region 下载 /// <summary> /// 下载资源 /// </summary> public void Download() { SaveFileDialog ...
- JDBC的批量处理数据
主要用到的方法有: preparedStatement.executeBatch();//积攒的数据执行 preparedStatement.clearBatch();//积攒的清除掉 prepare ...