mybatis的动态sql及模糊查询
1、动态sql
使用类似于jstl表达式来实现
2、模糊查找
用一个对象来封装条件
步骤:
1)新建一个条件实体
package com.hy.mybatis.entity;
public class ConditionUser {
private String name;
private int minAge;
private int maxAge;
@Override
public String toString() {
return "ConditionUser [name=" + name + ", minAge=" + minAge
+ ", maxAge=" + maxAge + "]";
}
public ConditionUser(String name, int minAge, int maxAge) {
super();
this.name = name;
this.minAge = minAge;
this.maxAge = maxAge;
}
public ConditionUser() {
super();
}
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;
}
}
2)写sql:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hy.mybatis.mapper.User"> <select id="getUser" parameterType="ConditionUser" resultType="DUser">
select * from d_user where 1=1
<if test="name != '%null%'">
and name like ${name}
</if>
and age between #{minAge} and #{maxAge}
</select> </mapper><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hy.mybatis.mapper.User"> <select id="getUser" parameterType="ConditionUser" resultType="DUser">
select * from d_user where 1=1
<if test="name != '%null%'">
and name like ${name}
</if>
and age between #{minAge} and #{maxAge}
</select> </mapper>
3)测试:
@Test
public void testGetUsers() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.mapper.User.getUser";
List<DUser> users = session.selectList(statement, new ConditionUser("%"+null+"%", 13, 18));
System.out.println(users);
}
mybatis的动态sql及模糊查询的更多相关文章
- Mybatis中动态SQL多条件查询
Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...
- 动态SQL与模糊查询
一: 1.需求 实现多条件查询用户(姓名模糊查询,年龄在指定的最小值与最大值之间) 2.结构目录 3.准备数据与建表 CREATE TABLE d_user( id int PRIMARY KEY A ...
- mybatis之动态SQL操作之查询
1) 查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层 * @author AdminTC */ public class StudentDao { /** * ...
- 动态SQL之模糊查询
模糊查询学习了三种: DAO层 // 可以使用 List<User> wherelike01(String user_name); // 忘记 List<User> where ...
- MyBatis动态SQL与模糊查询
sqlxml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...
- mybatis 动态Sql的模糊查询
where teacher.tname like concat(concat(#{tName}),'%') 2:distinct的使用 下面先来看看例子: table id name 1 ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
- MyBatis中动态SQL语句完成多条件查询
一看这标题,我都感觉到是mybatis在动态SQL语句中的多条件查询是多么的强大,不仅让我们用SQL语句完成了对数据库的操作:还通过一些条件选择语句让我们SQL的多条件.动态查询更加容易.简洁.直观. ...
- Mybatis学习记录(五)----Mybatis的动态SQL
1. 什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.1 需求 用户信息综合查询列表和用户信息查询列表总数这两个statemen ...
随机推荐
- android 判断是否设置了锁屏密码
方式1:在小米note手机上测试,只能判断是否设置了图形解锁. android.provider.Settings.System.getInt(getContentResolver(), androi ...
- 使用MySQL制作SNP146数据库
SNP数据(txt)文件可以在此下载:http://hgdownload.cse.ucsc.edu/goldenPath/hg19/database/ 下载.解压data之后,启动MySQL serv ...
- iOS后台挂起程序 当程序到后台后,继续完成Long-Running Task 任务
我们知道,到我们程序从前台退到后台(安home)键后,将执行程序的委托方法. // 当应用程序掉到后台时,执行该方法 - (void)applicationDidEnterBackground:(UI ...
- (整理) Json语法规则
{ "staff":[ {"name":"haha1", "age":20}, {"name":&q ...
- XJOI‘s story (不定期连载)
王城双基的力量.... 我也不知道写写这种东西会不会被查大表..尴尬 . 我为什么要写这东西: 为了蛤鸡 为了红太阳 xj 人物列传 2017 1,6 Friday 得到大新闻 机房后面的监 ...
- rails使用bootstrap3-wysiwyg可视化编辑器并实现自定义图片上传插入功能
之前在rails开发中使用了ckeditor作为可视化编辑器,不过感觉ckeditor过于庞大,有很多不需要的功能,而且图片上传功能不好控制不同用户可以互相删除图片,感觉很不好.于是考虑更改可视化编辑 ...
- HDU 5966 Guessing the Dice Roll
题意有 N≤10 个人,每个猜一个长度为L≤10的由1−6构成的序列,保证序列两两不同.不断地掷骰子,直到后缀与某人的序列匹配,则对应的人获胜.求每个人获胜的概率. 思路:建立trie图,跑高斯消元. ...
- pwnable echo1
最近忙的好久没有更新了,有空把之前拿来练手的CTF pwn题逐渐整理一下放出来 题目是 linux 64位程序 ,流程很简单,大致思路就是先把一个跳转的机器指令写进name的地址,然后溢出覆盖eip, ...
- WIN7 WIN8 WIN10你们的WmiPrvSE.exe系统占用资源厉害吗?
大家一起来做个实验吧.把你的支付宝安全控件和支付宝数字证书两个东东都卸掉.试试看电脑是否年轻几岁? 出处:百度贴吧原帖
- php 一些经常用到的方法
获取当前url地址 /** * 获取当前url * @author Red * @date * @return string */ function getRequestURL() { if (!is ...