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及模糊查询的更多相关文章

  1. Mybatis中动态SQL多条件查询

    Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...

  2. 动态SQL与模糊查询

    一: 1.需求 实现多条件查询用户(姓名模糊查询,年龄在指定的最小值与最大值之间) 2.结构目录 3.准备数据与建表 CREATE TABLE d_user( id int PRIMARY KEY A ...

  3. mybatis之动态SQL操作之查询

    1)  查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL /** * 持久层 * @author AdminTC */ public class StudentDao { /** * ...

  4. 动态SQL之模糊查询

    模糊查询学习了三种: DAO层 // 可以使用 List<User> wherelike01(String user_name); // 忘记 List<User> where ...

  5. MyBatis动态SQL与模糊查询

    sqlxml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...

  6. mybatis 动态Sql的模糊查询

    where teacher.tname like concat(concat(#{tName}),'%') 2:distinct的使用 下面先来看看例子: table    id name    1 ...

  7. 利用MyBatis的动态SQL特性抽象统一SQL查询接口

    1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...

  8. MyBatis中动态SQL语句完成多条件查询

    一看这标题,我都感觉到是mybatis在动态SQL语句中的多条件查询是多么的强大,不仅让我们用SQL语句完成了对数据库的操作:还通过一些条件选择语句让我们SQL的多条件.动态查询更加容易.简洁.直观. ...

  9. Mybatis学习记录(五)----Mybatis的动态SQL

    1.  什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.1 需求 用户信息综合查询列表和用户信息查询列表总数这两个statemen ...

随机推荐

  1. 外边距(padding)重叠的及解决办法

    两个或多个块级盒子的垂直相邻边界会重合.结果的边界宽度是相邻边界宽度中最大的值.如果出现负边界,则在最大的正边界中减去绝对值最大的负边界.如果没有正边界,则从零中减去绝对值最大的负边界.注意:相邻的盒 ...

  2. 特性Atrribute和枚举

    特性的简单实用!特性存放在metedata里面,它离不开反射. Program.cs class Program { static void Main(string[] args) { Console ...

  3. eclipse点不出方法

    window→preferences→java→editor→Content Assist→Advanced

  4. python 输出十六进制中文乱码

    代码中红色字体为解决方案: #-*-coding:utf-8-* import csv filename='C:\Users\yaru\Desktop\Senti_Value(1).csv' data ...

  5. 【 D3.js 入门系列 --- 5.1 】 做一个带坐标轴和标签的图表

    前面几节讲解了图标.坐标轴.比例等等,这一节整合这些内容做一个实用的图表.结果图如下: 代码如下所示: <html> <head> <meta charset=" ...

  6. [非原创]Project facet Java version 1.8 is not supported解决记录

    原博地址:http://blog.csdn.net/dingchenxixi/article/details/51496998 一看知道是因为jdk版本不一致所导致,如何解决? 方法一: 选中项目 P ...

  7. TableView分割线从顶端开始

    如果什么都不设置的话 分割线是从cell.textlabel处开始的 如果加上 [_myTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0) ...

  8. android 7.0变化

    一.加强版的低电耗模式 条件:1未插电源:2屏幕关闭:(N与6.0的区别就在于N在手机非静止时也可进入低电耗模式) 作用过程及方式:1充电状态:2屏幕关闭一定时间:进入一级低电限制:关闭网络,推迟CP ...

  9. ubuntu更新命令点点滴滴

    ubuntu更新命令点点滴滴      一些非root的更新命令:              sudo: sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一 ...

  10. linux命令之三

    0102 文档查阅指令 cat tac nl 简单查阅,-n  可显示行 more, less less is more 查询大文件,可分页. head tail 从头尾看.-n 限制行数. taif ...