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. 多线程理论———— threading

    什么是线程 线程也是一种多任务编程方法,可以利用计算机多核资源完成程序的并发执行.线程又被称为轻量级的进程.线程的特征 * 线程是计算机多核分配的最小单位 * 一个进程可以包含多个线程 * 线程也是一 ...

  2. Python 嵌套列表解析

    将一个3*4矩阵转为4*3矩阵Matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],]afterMatrix = [[row[i] for ro ...

  3. 《设计模式之美》 <03>面向对象、设计原则、设计模式、编程规范、重构,这五者有何关系?

    面向对象 现在,主流的编程范式或者是编程风格有三种,它们分别是面向过程.面向对象和函数式编程.面向对象这种编程风格又是这其中最主流的.现在比较流行的编程语言大部分都是面向对象编程语言.大部分项目也都是 ...

  4. UCOSII 之 任务统计

    UCOSII 使用空闲任务的计数值(OSIdleCtr)来实现CPU使用率的统计,首先统计一个固定时间内的计数值保存下来为 (MAX),然后再开启一个固定的时间段,当时间到达时得到另外一个(OSIdl ...

  5. windows BAT脚本2个服务器间传递文件

    1. 脚本功能: 实现2个服务器间文件的传递,例如从A服务器往B服务器上传文件 2. 实现步骤: 2.1 服务器连结,找到指定路径,读取所需要上传的文件,将文件名称复制到一个文件下 (此处考虑可能需要 ...

  6. C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)

    C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...

  7. python+Appium自动化:读取Yaml配置文件

    Yaml简介 Yaml:"Yet Another Markup Language"(是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名 ...

  8. zuul网管配置其他服务时 HTTP Status 500 – Internal Server Error

    1.这个错误是由三个原因导致的 (1).我在给类的接口命名的时候前面少加了一个/ (2)给zuul配置路由的时候多加了个服务名,serviceId名称就是spring的name,而不是eureka注入 ...

  9. MFC 线程的创建,线程函数在类内、类外

    [线程的创建] AfxBeginThread(类外的线程函数,NULL); AfxBeginThread(类内的线程函数,(LPVOID)this); [类外线程函数] UINT 函数名(LPVOID ...

  10. SQL Server的GO语句

    https://www.cnblogs.com/cbattle/p/8784537.html go语句经常加在create.exec.drop等前面.