1)  根据条件,插入一个学生

/**
* 持久层*/
public class StudentDao {
/**
* 动态SQL--插入
*/
public void dynaSQLwithInsert(Student student) throws Exception{
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try{
sqlSession.insert("mynamespace.dynaSQLwithInsert",student);
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
sqlSession.commit();
MyBatisUtil.closeSqlSession();
}
}
public static void main(String[] args) throws Exception{
StudentDao dao = new StudentDao();
dao.dynaSQLwithInsert(new Student(1,"哈哈",7000D));
dao.dynaSQLwithInsert(new Student(2,"哈哈",null));
dao.dynaSQLwithInsert(new Student(3,null,7000D));
}
}
<?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="mynamespace">
<sql id="key">
<trim suffixOverrides=",">
<if test="id!=null">
id,
</if>
<if test="name!=null">
name,
</if>
<if test="sal!=null">
sal,
</if>
</trim>
</sql>
<sql id="value">
<trim suffixOverrides=",">
<if test="id!=null">
#{id},
</if>
<if test="name!=null">
#{name},
</if>
<if test="sal!=null">
#{sal},
</if>
</trim>
</sql>
<insert id="dynaSQLwithInsert" parameterType="loaderman.Student">
insert into students(<include refid="key"/>) values(<include refid="value"/>)
</insert>
</mapper>

mybatis之动态SQL操作之插入的更多相关文章

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

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

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

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

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

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

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

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

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

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUYAAAC/CAIAAAANX+LCAAAYvElEQVR4nO2dWWycV9nHDyC6UEGBGy

  6. MyBatis的动态SQL操作--更新

    更新条件不确定,需要根据具体的情况生成sql语句. id是主键,一般不会去更新. 1.只更新name的值 update student set name = ? where id = ? 2.只更新s ...

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

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

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

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

  9. Java-MyBatis:MyBatis 3 动态 SQL

    ylbtech-Java-MyBatis:MyBatis 3 动态 SQL 1.返回顶部 1. 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架 ...

随机推荐

  1. String s=new String("xyz");创建了几个String Object?二者之前的区别是什么?

    两个.第一个对象是字符串常量"xyz",第二个对象是new String("xyz")的时候产生的,在堆中分配内存给这个对象,只不过这个对象的内容是指向字符串常 ...

  2. MySQL开启binlog无法启动ct 10 21:27:31 postfix/pickup[4801]: warning: 6BD991A0039: message has been queue

    1 详细异常 ct 10 21:27:31 postfix/pickup[4801]: warning: 6BD991A0039: message has been queue Oct 10 21:2 ...

  3. linux操作系统中的常用命令以及快捷键(一)

    接触了linux系统一年,总结一些常用的命令,快捷键等一些尝试 1.首先查看linux内核数量,常用于编译源码包时 用 make -j 来指定内核数来编译 grep ^processor /proc/ ...

  4. Django:中间件与csrf

    一.中间件 什么是中间件 中间件有什么用 自定义中间件 中间件应用场景 二.csrf csrf token跨站请求伪造 一.中间件 什么是中间件 中间件顾名思义,是介于request与response ...

  5. vue-element-admin跟springboot+shiro部署爬坑记

    今天把前端采用vue-element-admin与springboot的项目部署到正是线上,在开发线上很OK的,一放上去我的天啊,坑是真的多阿.下面听我一一道来:我这边采用的是nginx服务器部署. ...

  6. BIO、NIO、AIO 有什么区别?(未完成)

    BIO.NIO.AIO 有什么区别?(未完成)

  7. SparkStreaming使用checkpoint存在的问题及解决方案

    sparkstreaming关于偏移量的管理 在 Direct DStream初始化的时候,需要指定一个包含每个topic的每个分区的offset用于让Direct DStream从指定位置读取数据. ...

  8. RuntimeError: can't start new thread

    明明我只是简单跑了一个数据清洗28W数据的python脚本,不知道怎么就报错如下: too many threads running within your python process The &q ...

  9. python打开excel跳转对应分页

    需求: 在python脚本层,直接打开excel,跳转特定分页,方便策划编辑 尝试了几种不同的方法,最终选择了方法4.4种方法都可以实现打开外部文件,3.4可以实现跳转特定分页.3和4的区别是,3通过 ...

  10. Filtering Approaches for Real-Time Anti-Aliasing(2011 SIGGRAPH)

    Filtering Approaches for Real-Time Anti-Aliasing(2011 SIGGRAPH) 在2011的SIGGRAPH上,NVIDA提出了FXAA3.1,本文主要 ...