更新条件不确定,需要根据具体的情况生成sql语句.

id是主键,一般不会去更新。

1.只更新name的值

update student set name = ? where id = ?

2.只更新sal的值

update student set sal = ? where id = ?

3.同时更新name和sal的值

update student set sal = ? , name = ? where id = ?
<?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"> <!--
namespace的取值可以是实体的全限定名,这样有好处!
-->
<mapper namespace="com.winner.entity.Student">
<resultMap id="studentMap" type="student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
</resultMap>
<!-- set标签自动判断哪个是最后一个字段,会自动去掉最后一个,号 -->
<update id="dynaUpdate" parameterType="map">
UPDATE student
<set>
<if test="pname!=null">
name = #{pname},//不要忘了,
</if>
<if test="psal!=null">
sal = #{psal},//不要忘了,
</if>
</set>
WHERE id = #{pid}
</update>
</mapper>
public class StudentDao {
/**
* 有条件更新学生
*/
public void dynaUpdate(Integer id,String name,Double sal) throws Exception{
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtil.getSqlSession();
       //map用于接收方法的参数,xml中参数的类型就是map
Map<String,Object> map = new HashMap<String, Object>();
map.put("pid",id);
map.put("pname",name);
map.put("psal",sal);
sqlSession.update(Student.class.getName() + ".dynaUpdate", map);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
MybatisUtil.closeSqlSession();
}
} public static void main(String[] args) throws Exception { StudentDao dao = new StudentDao();
//关注SQL的变化
//dao.dynaUpdate(1,null,9000D);//update student set sal=? where id=?
//dao.dynaUpdate(1,"lisi",null);//update student set name=? where id=?
dao.dynaUpdate(1,"wangwu",8000D);//update student set name=? and sal=? where id=? }
}

MyBatis的动态SQL操作--更新的更多相关文章

  1. mybatis之动态SQL操作之更新

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

  2. MyBatis的动态SQL操作--查询

    查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL,即根据不同的情况生成不同的sql语句. 模拟一个场景,在做多条件搜索的时候,

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

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

  4. mybatis之动态SQL操作之插入

    1)  根据条件,插入一个学生 /** * 持久层*/ public class StudentDao { /** * 动态SQL--插入 */ public void dynaSQLwithInse ...

  5. MyBatis的动态SQL操作--插入

    需求:向数据库中插入一条数据 //id,name,sal非空,三个字段都插入 insert into student(id,name,sal) values (?,?,?) //id,name非空,只 ...

  6. MyBatis的动态SQL操作--删除

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUYAAAC/CAIAAAANX+LCAAAYvElEQVR4nO2dWWycV9nHDyC6UEGBGy

  7. mybatis之动态SQL操作之删除

    /** * 持久层 */ public class StudentDao { /** * 动态SQL--删除 */ public void dynaSQLwithDelete(int... ids) ...

  8. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  9. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

随机推荐

  1. 我们一起学Windows Phone 8-01-开发环境搭建

    我们仅讨论Windows Phone 8的开发,不考虑兼容向下兼容.也不会提供任何盗版软件的下载.破解. 需要准备 知识:.NET相关开发经验,C#语言开发经验.如果有WPF或Silverlight开 ...

  2. 什么是MBR?(含图解)

    Mbr位于磁盘的0柱面,0磁头,1扇区. MBR       有三部分构成,主引导程序,硬盘分区表DPT和,硬盘的有效标志55AA.在512个字节的主引导扇区里. 主引导程序占446个字节,dpt占6 ...

  3. String inputStream file转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  4. CSS居中的方法整合--水平居中

    原文 CSS的居中问题,是一个老生常谈的问题,各种居中方法层出不穷.是水平居中还是垂直居中?是block还是inline? 居中对象是一个还是多个?长度宽度是否确定?等等各种因素确定. 这里就从这些方 ...

  5. Implement Custom Cache Dependencies in ASP.NET 1.x

    Code download available at:CuttingEdge0407.exe(128 KB)   Contents What's a Cache Dependency, Anyway? ...

  6. ES6学习笔记(十四)

    1.Promise的含义 Promise是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6将其写进了语言标准,统一了用法,原生提供了Prom ...

  7. android service 整理

    项目经常要跟别的项目进行交互,比如说蓝牙打印机等,或者处理一些网络状态,或者调用baidu.高德等地图的时候就会用到, 或打开了音乐播放之后,便想去看看图片,或者下载文件的时候,我们看看博客. Ser ...

  8. ENVI中利用polygon掩膜修改类到指定类

    overlay——classification——制定分类的图像 edit——polygon delete from class(选择这个掩膜模式) edit——set delete class va ...

  9. ParentChildTest.java

    public class ParentChildTest { public static void main(String[] args) { Parent parent=new Parent(); ...

  10. java第六课 oop

    java oop 1.面向过程的结构化程序设计弊端:方法和数据结构都是毫无规律的定义在程序中任何位置        方法定义和方法要处理的数据结构也都是分开定义 2.对象:每new一次,就创建1个新对 ...