示例代码:

接口定义:
package com.mybatis.dao; import com.mybatis.bean.Employee; import java.util.List; public interface EmployeeMapper {
//携带了哪个字段查询条件就带上这个字段的值
public List<Employee> getEmpsByConditionIf(Employee employee); public List<Employee> getEmpsByConditionIfWhere(Employee employee);
} 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.mybatis.dao.EmployeeMapper">
<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
<!-- public List<Employee> getEmpsByConditionIf(Employee employee); -->
<select id="getEmpsByConditionIf" resultType="com.mybatis.bean.Employee">
select * from tbl_employee
where 1=1
<if test="id!=null">
and id=#{id}
</if>
<if test="lastName!=null && lastName!=''">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=""">
and email=#{email}
</if>
<!-- ognl会进行字符串与数字的转换判断 "0"==0 -->
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</select> <!--public List<Employee> getEmpsByConditionIfWhere(Employee employee);-->
<select id="getEmpsByConditionIfWhere" resultType="com.mybatis.bean.Employee">
select * from tbl_employee
<where>
<if test="id!=null">
id=#{id}
</if>
<if test="lastName!=null && lastName!=''">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=""">
and email=#{email}
</if>
<!-- ognl会进行字符串与数字的转换判断 "0"==0 -->
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</where>
</select>
</mapper> 测试代码:
package com.mybatis.demo; import com.mybatis.bean.Employee;
import com.mybatis.dao.EmployeeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; public class MyTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
} @Test
public void test() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession(true);
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//查询的时候如果某些条件没带可能sql拼装会有问题
//1、给where后面加上1=1,以后的条件都and xxx.
//2、mybatis使用where标签来将所有的查询条件包括在内。mybatis就会将where标签中拼装的sql,多出来的and或者or去掉
//where只会去掉第一个多出来的and或者or。
List<Employee> emps = mapper.getEmpsByConditionIf(new Employee(null, "jetty", "jetty@126.com", 1));
for (Employee emp : emps) {
System.out.println(emp);
}
System.out.println("-------------------");
List<Employee> employees = mapper.getEmpsByConditionIfWhere(new Employee(null, "jetty", "jetty@126.com", 1));
for (Employee emp : employees) {
System.out.println(emp);
}
} finally {
openSession.close();
}
}
}

Mybatis学习笔记10 - 动态sql之if判断的更多相关文章

  1. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  2. Mybatis学习笔记(六) —— 动态sql

    通过mybatis提供的各种标签方法实现动态拼接sql. 需求:根据性别和名字查询用户 查询sql: SELECT id, username, birthday, sex, address FROM ...

  3. Mybatis学习笔记之---动态sql中标签的使用

    动态Sql语句中标签的使用 (一)常用标签 1.<if> if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段 ...

  4. 1.3(Mybatis学习笔记)动态SQL

    一.<if> 使用<if>可以根据具体情况来拼接SQL语句,使其更加灵活更加适应我们的需求. <if>的标签体中是需要拼接的语句,满足条件才会将其进行拼接. < ...

  5. Mybatis学习笔记14 - 动态sql之foreach标签

    一.查询给定集合中员工id对应的所有员工信息 示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import ...

  6. Mybatis学习笔记13 - 动态sql之set标签

    示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; public interface EmployeeMapp ...

  7. Mybatis学习笔记12 - 动态sql之choose(when otherwise)标签

    choose (when, otherwise):分支选择:带了break的swtich-case 示例代码: 接口定义: package com.mybatis.dao; import com.my ...

  8. Mybatis学习笔记11 - 动态sql之trim标签

    trim标签体中是整个字符串拼串后的结果.prefix="" 前缀: prefix给拼串后的整个字符串加一个前缀prefixOverrides="" 前缀覆盖: ...

  9. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...

随机推荐

  1. Luogu 3676 小清新数据结构题

    推荐博客: http://www.cnblogs.com/Mychael/p/9257242.html 感觉还挺好玩的 首先考虑以1为根,把每一个点子树的权值和都算出来,记为$val_{i}$,那么在 ...

  2. Mac安装破解版Office 2016办公软件

    一.相关软件 Microsoft Office 2016 For Mac Cracker 破解工具 资源地址(链接:https://pan.baidu.com/s/1Z5CIv-XbxS08MniYN ...

  3. map集合的应用

    分析以下需求,并用代码实现: (1)利用键盘录入,输入一个字符串 (2)统计该字符串中各个字符的数量例如 用户输入字符串"If~you-want~to~change-your_fate_I_ ...

  4. 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(四)

    ViewPagerIndicator+ViewPager 要想使用ViewPagerIndicator,要使用到viewPagerlibrary开源库 top.xml <?xml version ...

  5. C++后台服务如何高效实现多个定时器任务

    大部分云端的后台服务,经常会使用到定时器功能来检测一些状态值的变化,且当定时器较多时,就需要设计统一的定时器管理模块来维护所有的定时器资源.然而要设计性能良好的定时器和管理模块,是需要一定的经验和技巧 ...

  6. web安全-接入层注入

    web安全-接入层注入 1.关系型数据库 mysql 存放结构化数据 高效操作大量数据 方便处理数据之间的关联关系 2.SQL注入 select * from table where id=${id} ...

  7. git配置项目

    1.下载安装完git 2.在git oschina上发布项目 3.管理-公匙管理 4.git上面生成公匙 $ cat ~/.ssh/id_rsa.pub 5.将公匙复制进 git oschina 管理 ...

  8. 用ES6的class模仿Vue写一个双向绑定

    原文地址:用ES6的class模仿Vue写一个双向绑定 点击在线尝试一下 最终效果如下: 构造器(constructor) 构造一个TinyVue对象,包含基本的el,data,methods cla ...

  9. 8、OpenCV Python 图像直方图

    __author__ = "WSX" import cv2 as cv import numpy as np from matplotlib import pyplot as pl ...

  10. typeof 和 instanceof

    typeof 和 instanceof 都是用来判断类型的函数 typeof 对于原始类型来说,除了 null 都可以显示正确的类型 typeof 1 // 'number' typeof '1' / ...