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 ...
随机推荐
- SILVERLIGHT 应急卫生模拟演练项目之childwindow
项目中经常要用到childwindow 默认SL提供的界面很不好看 也很难适应系统里的要求 单调的界面 木关系 可以我们可以通过BLEND自定义成我们想要的 首先新建立一个SILVERLIGHT 子窗 ...
- linux 服务的操作
启动和停止服务service 命令用于启动及停止某个服务,例如:service camsd stop 停止 camsd 服务service oracled start 启动 oracled ...
- button标签和input button
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
- linux grep,sed,awk和diff的使用
1:grep//显示行 # grep 'main' /home/myhome/a.c//将a.c含有main的行显示出来 # grep -v 'main' /home/myhome/a.c //显示除 ...
- MVCC PostgreSQL实现事务和多版本并发控制的精华
原创文章,同步发自作者个人博客,http://www.jasongj.com/sql/mvcc/ PostgreSQL针对ACID的实现机制 事务的实现原理可以解读为RDBMS采取何种技术确保事务的A ...
- css的学习
第一天. css 1.知道 内联 内部 外部 的优先权 2.css的语法 3.id 选择器 以及 类选择器 和属性选择器 4.对图片 长 宽 的编辑 调整图片 5.通过内部 对整个页面 文字 颜 ...
- LeetCode() Basic Calculator 不知道哪里错了
class Solution {public: int calculate(string s) { stack<int> num; stack<ch ...
- LeetCode 【347. Top K Frequent Elements】
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [BZOJ 3681]Ariettad
终于是打完了 CH 上的数据结构专场了…… 不过看样子还有一套 5555 传送门: http://ch.ezoj.tk/contest/CH%20Round%20%2351%20-%20Shinrei ...
- UIScrollView在AutoLayout下的滚动问题
使用Storyboard编写UI,设置支持AutoLayout. 在其中的一个场景上,添加一个UIScrollView,在对应的代码里增加 - (void)viewDidLoad { [super v ...