示例代码:

接口定义:
package com.mybatis.dao; import com.mybatis.bean.Employee; public interface EmployeeMapper {
public void updateEmp(Employee employee);
public void updateEmployee(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">
<update id="updateEmp">
update tbl_employee
<set>
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender!=null">
gender=#{gender}
</if>
</set>
where id=#{id}
</update> <update id="updateEmployee">
update tbl_employee
<trim prefix="set" suffixOverrides=",">
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender!=null">
gender=#{gender}
</if>
</trim>
where id=#{id}
</update>
</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);
mapper.updateEmp(new Employee(1, "good", null, null));
mapper.updateEmployee(new Employee(5, "test", null, null));
} finally {
openSession.close();
}
}
}

Mybatis学习笔记13 - 动态sql之set标签的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. Mybatis学习笔记10 - 动态sql之if判断

    示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import java.util.List; public ...

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

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

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

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

随机推荐

  1. 前端基础 之JS

    浏览目录 JavaScript语法基础 JavaScript数据类型及类型查询 JavaScript运算符 JavaScript流程控制 JavaScript函数 词法分析 JavaScript内置对 ...

  2. 树莓派研究笔记(9)-- 树莓派SPI连接TFT屏幕

    HDMI连接和树莓派专用连接的接口的屏幕都太贵了,为了节约成本,现在国内大多数还是TFT屏幕. 树莓派可以激活SPI接口,通过代码驱动TFT屏幕的显示.这样利用树莓派zero 打造小型的游戏平台可以大 ...

  3. 形式化方法的逆袭——如何找出Timsort算法和玉兔月球车中的Bug?

    https://bindog.github.io/blog/2015/03/30/use-formal-method-to-find-the-bug-in-timsort-and-lunar-rove ...

  4. CF 432B :Football Kit

    hash做法: #include<stdio.h> #include<string.h> ; int home[Max],away[Max],hash[Max]; int ma ...

  5. 操作系统 Linux ex2 note

    locate filename 搜索文件 将当前用户目录下的文件清单输出到文件list1.txt(当前用户目录下)中.ls -l > list1.txt 利用管道命令将根(/)下所有修改日期在4 ...

  6. Linux下性能监控工具介绍

    本章解释如何使用适用于Linux的大量性能工具及每个工具中信息的意义.即使已经使用top或者sar,也可能从本章学到相关知识. 应该养成使用这些工具的习惯.当然要知道如何诊断性能问题,但也应该定期寻找 ...

  7. 一键结束port 5037占用

    输入cmd进入dos界面,进入android-sdk-windows\platform-tools目录,执行下面命令启动adb start-server出现下面错误* daemon not runni ...

  8. javascript window.showModalDialog不兼容goole解决方案

    window.showModalDialog不兼容goole解决方案 一.弹框方案: 1.window.open; 2.window.showModalDialog; 3.div制作窗口:(本节忽略) ...

  9. Spring Boot 学习系列(02)—使用热部署,提升开发效

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 开发调试很简单 热部署的使用非常简单,但能极大的提高我们的开发效率,像传统的web应用,我们修改后需要重新编 ...

  10. Linux_Shell_ Map 的使用和遍历

    定义初始化map declare -A map=([") 输出所有key echo ${map[@]} 输出key对应的值 "]} 遍历map for key in ${!map[ ...